From 58e55afae582ab86319753ef1fbc6e95b8eef3ed Mon Sep 17 00:00:00 2001 From: DYdave <DYdave@467284.no-reply.drupal.org> Date: Sat, 29 Mar 2025 03:12:11 +0100 Subject: [PATCH] Issue #3516147 by dydave: PHPSTAN: Raised validation level from 0 (min) to 9 (max). --- phpstan.neon | 2 ++ .../ResponsiveImageLinkFormatter.php | 18 +++++++++--- .../ResponsiveImageLinkFormatterTest.php | 3 ++ .../ResponsiveImageLinkFormatterTest.php | 3 ++ .../FieldFormatter/ImageLinkFormatter.php | 18 +++++++++--- .../ImageLinkFormatterTrait.php | 29 +++++++++++++++---- .../src/Functional/ImageLinkFormatterTest.php | 3 ++ tests/src/Kernel/ImageLinkFormatterTest.php | 3 ++ .../ImageLinkFormatterTestTrait.php | 23 +++++++++++---- .../Kernel/ImageLinkFormatterTestTrait.php | 8 ++++- 10 files changed, 90 insertions(+), 20 deletions(-) create mode 100644 phpstan.neon diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..07873f2 --- /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 2c25ad7..c4a8a0a 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 e0fd82d..a6f0564 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 7dbe968..1d3ee5a 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 f64c050..130e019 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 4b1ae61..0148825 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 7c22858..d5d8fb9 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 8219297..6982693 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 538dfa6..ed21e09 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 b238c3c..eb75d25 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() -- GitLab