diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000000000000000000000000000000000000..07873f2fe114b2cf3f1c98cf9c3ca1e3aa596b3c --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,2 @@ +parameters: + level: 9 diff --git a/responsive_image_link_formatter/src/Plugin/Field/FieldFormatter/ResponsiveImageLinkFormatter.php b/responsive_image_link_formatter/src/Plugin/Field/FieldFormatter/ResponsiveImageLinkFormatter.php index 2c25ad7b10fb0f5bfa44c7400a6d3b079baf76d6..c4a8a0afc4be13037b2f8dd319b7c4005c5a7d30 100644 --- a/responsive_image_link_formatter/src/Plugin/Field/FieldFormatter/ResponsiveImageLinkFormatter.php +++ b/responsive_image_link_formatter/src/Plugin/Field/FieldFormatter/ResponsiveImageLinkFormatter.php @@ -50,13 +50,13 @@ class ResponsiveImageLinkFormatter extends ResponsiveImageFormatter implements C * The plugin implementation definition. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition * The definition of the field to which the formatter is associated. - * @param array $settings + * @param array<mixed> $settings * The formatter settings. * @param string $label * The formatter label display setting. * @param string $view_mode * The view mode. - * @param array $third_party_settings + * @param array<mixed> $third_party_settings * Any third party settings. * @param \Drupal\Core\Entity\EntityStorageInterface $responsive_image_style_storage * The responsive image style storage. @@ -75,9 +75,19 @@ class ResponsiveImageLinkFormatter extends ResponsiveImageFormatter implements C } /** - * {@inheritdoc} - * * Adds the 'entity_field.manager' service to parent's container. + * + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + * The container to pull out services used in the plugin. + * @param array{field_definition: \Drupal\Core\Field\FieldDefinitionInterface, settings: array<string>, label: string, view_mode: string, third_party_settings: array<string>} $configuration + * The configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin ID for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * + * @return static + * Returns an instance of this plugin. */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( diff --git a/responsive_image_link_formatter/tests/src/Functional/ResponsiveImageLinkFormatterTest.php b/responsive_image_link_formatter/tests/src/Functional/ResponsiveImageLinkFormatterTest.php index e0fd82d34072b40ca5dc012881995ab8b129b10d..a6f0564a1934ceb5c1f1950e9a39ee000389a4af 100644 --- a/responsive_image_link_formatter/tests/src/Functional/ResponsiveImageLinkFormatterTest.php +++ b/responsive_image_link_formatter/tests/src/Functional/ResponsiveImageLinkFormatterTest.php @@ -55,6 +55,9 @@ class ResponsiveImageLinkFormatterTest extends ResponsiveImageFieldDisplayTest { * the responsive image link formatter with and without a link and checks * whether the expected HTML is found on the page. * + * @return void + * Nothing to return. + * * @see \Drupal\Tests\image_link_formatter\Functional\ImageLinkFormatterTest::testImageLinkFormatterWrappedImage() * @see \Drupal\Tests\image_link_formatter\Traits\Functional\ImageLinkFormatterTestTrait::doTestImageLinkFormatterWrappedImage() * @see \Drupal\Tests\image_link_formatter\Traits\Functional\ImageLinkFormatterTestTrait::assertImageLinkFormatter() diff --git a/responsive_image_link_formatter/tests/src/Kernel/ResponsiveImageLinkFormatterTest.php b/responsive_image_link_formatter/tests/src/Kernel/ResponsiveImageLinkFormatterTest.php index 7dbe96858f90d2e49df9bfc50ddb1fb70b58d92e..1d3ee5a110f7ce3637cb68a3ae44234c8d4dd5d3 100644 --- a/responsive_image_link_formatter/tests/src/Kernel/ResponsiveImageLinkFormatterTest.php +++ b/responsive_image_link_formatter/tests/src/Kernel/ResponsiveImageLinkFormatterTest.php @@ -48,6 +48,9 @@ class ResponsiveImageLinkFormatterTest extends ImageFormatterTest { * link and checks whether the expected HTML is found with the correct * attributes options. * + * @return void + * Nothing to return. + * * @see \Drupal\Tests\image_link_formatter\Kernel\ImageLinkFormatterTest::testImageLinkFormatterUrlOptions() * @see \Drupal\Tests\image_link_formatter\Traits\Kernel\ImageLinkFormatterTestTrait::doTestImageLinkFormatterUrlOptions() */ diff --git a/src/Plugin/Field/FieldFormatter/ImageLinkFormatter.php b/src/Plugin/Field/FieldFormatter/ImageLinkFormatter.php index f64c05022bcc7a53368e0f03d1706e951baec517..130e0197b74ef713506881505bef74a4041c7b28 100644 --- a/src/Plugin/Field/FieldFormatter/ImageLinkFormatter.php +++ b/src/Plugin/Field/FieldFormatter/ImageLinkFormatter.php @@ -48,13 +48,13 @@ class ImageLinkFormatter extends ImageFormatter implements ContainerFactoryPlugi * The plugin implementation definition. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition * The definition of the field to which the formatter is associated. - * @param array $settings + * @param array<string> $settings * The formatter settings. * @param string $label * The formatter label display setting. * @param string $view_mode * The view mode. - * @param array $third_party_settings + * @param array<string> $third_party_settings * Any third party settings. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. @@ -71,9 +71,19 @@ class ImageLinkFormatter extends ImageFormatter implements ContainerFactoryPlugi } /** - * {@inheritdoc} - * * Adds the 'entity_field.manager' service to parent's container. + * + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + * The container to pull out services used in the plugin. + * @param array{field_definition: \Drupal\Core\Field\FieldDefinitionInterface, settings: array<string>, label: string, view_mode: string, third_party_settings: array<string>} $configuration + * The configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin ID for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * + * @return static + * Returns an instance of this plugin. */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( diff --git a/src/Plugin/Field/FieldFormatter/ImageLinkFormatterTrait.php b/src/Plugin/Field/FieldFormatter/ImageLinkFormatterTrait.php index 4b1ae6161877bc7fb8a8a262fa85b8d6fdbf24af..0148825b76b5b5044927788c8c5710fc89ef15fd 100644 --- a/src/Plugin/Field/FieldFormatter/ImageLinkFormatterTrait.php +++ b/src/Plugin/Field/FieldFormatter/ImageLinkFormatterTrait.php @@ -34,10 +34,18 @@ trait ImageLinkFormatterTrait { private $entityFieldManager; /** - * {@inheritdoc} + * Returns a form to configure settings for the formatter. * * Adds entity type's custom link fields to the image field formatter's list * of options in the 'image_link' select dropdown. + * + * @param array<mixed> $form + * The form where the settings form is being included in. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @return array<mixed> + * The form elements for the formatter settings. */ public function settingsForm(array $form, FormStateInterface $form_state) { $element = parent::settingsForm($form, $form_state); @@ -66,11 +74,20 @@ trait ImageLinkFormatterTrait { } /** - * {@inheritdoc} + * Builds a renderable array for a field value. * * Upon display of each value of the image field, corresponding custom link * field value is added to the render array as a URL object ('#url' key of the * image's render array). + * + * @param \Drupal\Core\Field\FieldItemListInterface<\Drupal\Core\Field\FieldItemInterface> $items + * The field values to be rendered. + * @param string $langcode + * The language that should be used to render the field. + * + * @return array<mixed> + * A renderable array for $items, as an array of child elements keyed by + * consecutive numeric indexes starting from 0. */ public function viewElements(FieldItemListInterface $items, $langcode) { // Extend core image formatter with a URL provided by a custom link field. @@ -79,12 +96,12 @@ trait ImageLinkFormatterTrait { $image_link_setting = $this->getSetting('image_link'); $link_fields = $this->getLinkFieldsOptions(); if (isset($link_fields[$image_link_setting])) { - /** @var \Drupal\link\Plugin\Field\FieldType\LinkItemList $link_items */ + /** @var \Drupal\Core\Field\FieldItemList<\Drupal\link\Plugin\Field\FieldType\LinkItem> $link_items */ $link_items = $items->getEntity()->get($image_link_setting); // If a link field is selected in formatter's settings, iterate through // each rendered image and set its '#url' value to matching link URL. foreach ($elements as $delta => $element) { - /** @var \Drupal\link\Plugin\Field\FieldType\LinkItem $link_item_value */ + /** @var \Drupal\link\Plugin\Field\FieldType\LinkItem|null $link_item_value */ $link_item_value = $link_items->get($delta); if (!empty($link_item_value)) { $elements[$delta]['#url'] = $link_item_value->getUrl(); @@ -98,7 +115,7 @@ trait ImageLinkFormatterTrait { /** * Helper function to get a list of link fields attached to entity and bundle. * - * @return array + * @return array<string, string> * An options array of all link fields attached to this entity and bundle. */ private function getLinkFieldsOptions() { @@ -106,7 +123,7 @@ trait ImageLinkFormatterTrait { // Filter all link fields attached to this entity with this bundle. $link_fields = array_filter($this->entityFieldManager->getFieldDefinitions( $this->fieldDefinition->getTargetEntityTypeId(), - $this->fieldDefinition->getTargetBundle() + $this->fieldDefinition->getTargetBundle() ?? '' ), function (FieldDefinitionInterface $element) { return $element->getType() === 'link'; diff --git a/tests/src/Functional/ImageLinkFormatterTest.php b/tests/src/Functional/ImageLinkFormatterTest.php index 7c228580fe0eac117f6d55b8459695a3b9d05fe4..d5d8fb955f78487ed245c62ba5adab94ef9ebb70 100644 --- a/tests/src/Functional/ImageLinkFormatterTest.php +++ b/tests/src/Functional/ImageLinkFormatterTest.php @@ -54,6 +54,9 @@ class ImageLinkFormatterTest extends ImageFieldTestBase { * the image link formatter with and without a link and checks whether the * expected HTML is found on the page. * + * @return void + * Nothing to return. + * * @see \Drupal\Tests\image_link_formatter\Traits\Functional\ImageLinkFormatterTestTrait::doTestImageLinkFormatterWrappedImage() * @see \Drupal\Tests\image_link_formatter\Traits\Functional\ImageLinkFormatterTestTrait::assertImageLinkFormatter() * @see \Drupal\Tests\responsive_image_link_formatter\Functional\ResponsiveImageLinkFormatterTest::testResponsiveImageLinkFormatterWrappedImage() diff --git a/tests/src/Kernel/ImageLinkFormatterTest.php b/tests/src/Kernel/ImageLinkFormatterTest.php index 8219297b3af13e8a9ade2ed7a36094ac94148d96..6982693da9a30b4993f51adc8ee7730ac500517b 100644 --- a/tests/src/Kernel/ImageLinkFormatterTest.php +++ b/tests/src/Kernel/ImageLinkFormatterTest.php @@ -43,6 +43,9 @@ class ImageLinkFormatterTest extends ImageFormatterTest { * node, with the image link formatter with a link and checks whether the * expected HTML is found with the correct attributes options. * + * @return void + * Nothing to return. + * * @see \Drupal\Tests\image_link_formatter\Traits\Kernel\ImageLinkFormatterTestTrait::doTestImageLinkFormatterUrlOptions() * @see \Drupal\Tests\responsive_image_link_formatter\Kernel\ResponsiveImageLinkFormatterTest::testImageLinkFormatterUrlOptions() */ diff --git a/tests/src/Traits/Functional/ImageLinkFormatterTestTrait.php b/tests/src/Traits/Functional/ImageLinkFormatterTestTrait.php index 538dfa6f14b3c70a0b1d2b260b2a55c2f313b941..ed21e0909be045b10efa74a687092a31ca81242c 100644 --- a/tests/src/Traits/Functional/ImageLinkFormatterTestTrait.php +++ b/tests/src/Traits/Functional/ImageLinkFormatterTestTrait.php @@ -9,6 +9,7 @@ namespace Drupal\Tests\image_link_formatter\Traits\Functional; use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Field\FieldStorageDefinitionInterface; +use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\file\Entity\File; @@ -56,17 +57,17 @@ trait ImageLinkFormatterTestTrait { /** * Render a field on a given entity with the given settings. * - * @param Object $entity + * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * The entity to render the field from. * @param string $field_name * The field to render. - * @param array $settings + * @param array<mixed> $settings * An array of field settings or a view mode. * * @return string * Rendered field HTML. */ - public function renderField(Object $entity, string $field_name, array $settings) { + public function renderField(FieldableEntityInterface $entity, string $field_name, array $settings) { $build = $entity->get($field_name)->view($settings); \Drupal::service('renderer')->renderRoot($build[0]); $field_output = trim($build[0]['#markup']); @@ -76,6 +77,9 @@ trait ImageLinkFormatterTestTrait { /** * Create and configure link field attached to the 'article' node type. + * + * @return void + * Nothing to return. */ protected function createLinkField() { // Functional tests with node type 'article' created in parent's 'setUp'. @@ -123,10 +127,13 @@ trait ImageLinkFormatterTestTrait { * @param string $formatter_name * The name of the formatter to be tested. A module can implement multiple * formatters and could need different test classes or methods. - * @param array $expected_result + * @param array<string, string> $expected_result * An associative array for tests with ('link') or without ('no_link') a * link, containing the HTML result the formatter is expected to output. * + * @return void + * Nothing to return. + * * @see \Drupal\Tests\image_link_formatter\Functional\ImageLinkFormatterTest::testImageLinkFormatterWrappedImage() * @see \Drupal\Tests\responsive_image_link_formatter\Functional\ResponsiveImageLinkFormatterTest::testResponsiveImageLinkFormatterWrappedImage() */ @@ -143,10 +150,13 @@ trait ImageLinkFormatterTestTrait { ); // Upload a test image to a new node of type 'article' without a link value. + /** @var \stdClass $test_image */ $test_image = current($this->drupalGetTestFiles('image')); $nid = $this->uploadNodeImage($test_image, $image_field_name, 'article'); $node_storage = $this->container->get('entity_type.manager')->getStorage('node'); + /** @var \Drupal\Core\Entity\FieldableEntityInterface $node */ $node = $node_storage->load($nid); + /** @var \Drupal\file\FileInterface $file */ $file = File::load($node->$image_field_name->get(0)->getValue()['target_id']); // Configure image field formatter's settings to point to the link field @@ -181,7 +191,7 @@ trait ImageLinkFormatterTestTrait { * * @param string $rendered_output * The output of the field formatter's rendering process. - * @param array $expected_result + * @param array<string, string> $expected_result * An associative array keyed for tests with ('link' key) or without * ('no_link' key) a link, containing the HTML result the formatter is * expected to generate. @@ -194,6 +204,9 @@ trait ImageLinkFormatterTestTrait { * Additional HTML attributes options to be added to the image or link, such * as 'alt', 'title', 'rel' (@TODO: currently unused - not yet implemented). * + * @return void + * Nothing to return. + * * @see \Drupal\Tests\image_link_formatter\Functional\ImageLinkFormatterTest::testImageLinkFormatterWrappedImage() * @see \Drupal\Tests\image_link_formatter\Functional\ResponsiveImageLinkFormatterTest::testResponsiveImageLinkFormatterWrappedImage() */ diff --git a/tests/src/Traits/Kernel/ImageLinkFormatterTestTrait.php b/tests/src/Traits/Kernel/ImageLinkFormatterTestTrait.php index b238c3ca8a33a23b42f175e1636dae4f6bafd689..eb75d2596d55014b7a4064de48ff613b7f137d4e 100644 --- a/tests/src/Traits/Kernel/ImageLinkFormatterTestTrait.php +++ b/tests/src/Traits/Kernel/ImageLinkFormatterTestTrait.php @@ -56,6 +56,9 @@ trait ImageLinkFormatterTestTrait { /** * Create a link field attached to the test entity type used for the tests. + * + * @return void + * Nothing to return. */ protected function createLinkField() { $this->fieldNameLink = mb_strtolower($this->randomMachineName()); @@ -102,10 +105,13 @@ trait ImageLinkFormatterTestTrait { * @param string $formatter_name * The name of the formatter to be tested. A module can implement multiple * formatters and could need different test classes or methods. - * @param array $link_attribute_options + * @param array<string, string> $link_attribute_options * A standard Drupal options array, with the attribute as key associated to * its value. * + * @return void + * Nothing to return. + * * @see \Drupal\Tests\image\Kernel\ImageFormatterTest::testImageFormatterUrlOptions() * @see \Drupal\Tests\image_link_formatter\Kernel\ImageLinkFormatterTest::testImageLinkFormatterUrlOptions() * @see \Drupal\Tests\responsive_image_link_formatter\Kernel\ResponsiveImageLinkFormatterTest::testImageLinkFormatterUrlOptions()