Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect warning for arguments to static methods that return "new static()" #272

Closed
rickdenhaan opened this issue Aug 15, 2022 · 3 comments · Fixed by #273
Closed

Incorrect warning for arguments to static methods that return "new static()" #272

rickdenhaan opened this issue Aug 15, 2022 · 3 comments · Fixed by #273

Comments

@rickdenhaan
Copy link

After running composer up we received an updated version of phpcs-variable-analysis from v2.11.4 to v2.11.5 (no other PHP_CodeSniffer-related updates). Since then we're receiving warnings in custom services in our Drupal application:

FILE: modules/custom/some_module/src/Controller/ResultResponseController.php
--------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 22 WARNINGS AFFECTING 22 LINES
--------------------------------------------------------------------------------------------------------
 343 | WARNING | Unused static variable $container.
 345 | WARNING | Redeclaration of function parameter $container as static variable.
 346 | WARNING | Redeclaration of static variable $container as static variable.
 347 | WARNING | Redeclaration of static variable $container as static variable.
...etc

These warnings are about this code inside a PHP class:

public static function create(ContainerInterface $container): ResultResponseController {
 return new static(
   $container->get('entity_type.manager'),
   $container->get('form_builder'),
   $container->get('messenger'),
   $container->get('request_stack'),
   // ...etc
 );
}

We can work around the errors by explicitly naming the class we want to return (i.e. return new ResultResponseController() but this will be inaccurate for some locations where this static ::create method exists in an abstract base class.

Could this be caused by the recent change to static declaration checks (#267)?

@mkalkbrenner
Copy link

mkalkbrenner commented Aug 15, 2022

Same here. The proposed workaround helps.
But I agree that it is not a good solution to move and copy all create() functions from abstract base classes to all children.

@KoenvanMeijeren
Copy link

Same here. But I get also false positives for code pieces like this:

$definitions = $plugin_manager->getDefinitions();

return array_map(static function ($definition) {
   return $definition['label'];
}, $definitions);

@sirbrillig
Copy link
Owner

Sorry for the regression! Thanks for the fix, @arkener! I'll put out a patch release shortly as soon as I check out #271

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants