After I ran updates for core and contrib modules, i tested the custom coding using PHPCS.

The test received multiple errors for the create function:

The code:

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('image.factory')
    );
  }

Warnings received:

     [exec]  59 | WARNING | Unused static variable $container.
     [exec]  59 | WARNING | Unused static variable $configuration.
     [exec]  59 | WARNING | Unused static variable $plugin_id.
     [exec]  59 | WARNING | Unused static variable $plugin_definition.
     [exec]  61 | WARNING | Redeclaration of function parameter $configuration as static variable.
     [exec]  62 | WARNING | Redeclaration of function parameter $plugin_id as static variable.
     [exec]  63 | WARNING | Redeclaration of function parameter $plugin_definition as static variable.
     [exec]  64 | WARNING | Redeclaration of function parameter $container as static variable.

These warnings can be fixed if you return a new self object instead of a static object, but this will lead to a PHPSTAN error - as the create() function does only accept a static object.

I tried to find information about the reason why this happends - but I was out of luck. Does anyone has more information about this issue?

Comments

arkener’s picture

StatusFileSize
new955 bytes

This issue is caused by an issue upstream in the latest release of the PHP_CodeSniffer VariableAnalysis package.

As a workaround you can lock this package to version 2.11.4 using:
composer require sirbrillig/phpcs-variable-analysis "<=2.11.4"

Or applying the attached patch to sirbrillig/phpcs-variable-analysis

marcellinostroosnijder’s picture

@Arkener

Thanks! Your patch works!

arkener’s picture

Status: Active » Fixed

This issue has been fixed upstream in sirbrillig/phpcs-variable-analysis version 2.11.6

Thanks @MarcellinoStroosnijder for creating this issue!

marcellinostroosnijder’s picture

Thanks for the fix @Arkener!

br0ken’s picture

Still having this problem with sirbrillig/phpcs-variable-analysis:2.11.6. Am I missing something?

Coder 8.3.15
PHP_CodeSniffer version 3.7.1

arkener’s picture

@BR0kEN, are you able to provide an example of the code where this issue occurs for you and the output of the sniff?

br0ken’s picture

Status: Fixed » Needs work

Basically, any anonymous static function with parameters produces this.

<?php

static fn (string $value) => $value;
FILE: /home/catuser/app/public/test.php
-------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AND 2 WARNINGS AFFECTING 2 LINES
-------------------------------------------------------------------------------------------------------------------------------------------------------
 1 | ERROR   | [x] Missing file doc comment (Drupal.Commenting.FileComment.Missing)
 3 | WARNING | [ ] Unused static variable $value. (DrupalPractice.CodeAnalysis.VariableAnalysis.UnusedVariable)
 3 | WARNING | [ ] Redeclaration of function parameter $value as static variable.
   |         |     (DrupalPractice.CodeAnalysis.VariableAnalysis.VariableRedeclaration)
arkener’s picture

Status: Needs work » Needs review
StatusFileSize
new1.07 KB

You're right, it seems to still be broken on static arrow functions. I've created an issue upstream and created a PR to fix this issue. The attached patch, applied on sirbrillig/phpcs-variable-analysis:2.11.6 should fix this issue.

br0ken’s picture

Status: Needs review » Reviewed & tested by the community

Yup, the patch works. Thanks.

        "patches": {
            "sirbrillig/phpcs-variable-analysis": {
                "#3303720: Unused static variables and redeclaration of function parameter as static variable": "https://github.com/sirbrillig/phpcs-variable-analysis/commit/b065eafa1fb9ae4de8b7b978e8bdb5b1a88e6938.diff"
            }

Btw, did you know you can add .diff or .patch to any Github PR or even commit to get the applicable patch file?

arkener’s picture

@BR0kEN Yes, I tend to copy the patch here as well for users that aren't used to this feature, though you're correct that we can just as well place the link to the GitHub patch here.

This issue has been fixed upstream in sirbrillig/phpcs-variable-analysis:2.11.7

arkener’s picture

Status: Reviewed & tested by the community » Fixed

  • klausi committed d6f6112 on 8.3.x
    chore(composer): Bump sirbrillig/phpcs-variable-analysis for static...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.