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

Do not report constructor unused parameter if class implements an Interface that defines a constructor #3777

Merged

Conversation

carlos-granados
Copy link
Contributor

If the class implements an interface that defines a constructor, then the class constructor needs to match the signature of the constructor in the interface. In this case we should not report any unused constructor parameters and this is what this PR achieves

Fixes phpstan/phpstan#11454

@carlos-granados
Copy link
Contributor Author

Again there is a failing check which seems unrelated to this PR

@@ -45,6 +45,12 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

foreach ($node->getClassReflection()->getInterfaces() as $interface) {
if ($interface->hasMethod('__construct')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasConstructor would be more precise

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And above instead of strtolower($method->getName()) !== '__construct' we should use $method->isConstructor().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes rebased and updated

@staabm
Copy link
Contributor

staabm commented Mar 1, 2025

this PR made me think of classes which implement more then one interface and multiple __construct inheritance

https://3v4l.org/easGk
https://phpstan.org/r/dfe421ff-5ff8-44fc-8f5d-8782ef4ffb57

maybe thats something to loot at next (followup PR) @carlos-granados :) - as we are missing a reported error

@carlos-granados carlos-granados force-pushed the interface-unused-constructor-param branch from 14e0d1e to d989c87 Compare March 2, 2025 11:01
@staabm
Copy link
Contributor

staabm commented Mar 2, 2025

we need a similar fix for abstract base-classes

https://phpstan.org/r/2652fd08-bb25-431a-8f0f-b28b3c096dc6
https://3v4l.org/hdDcZ

@carlos-granados carlos-granados force-pushed the interface-unused-constructor-param branch from d989c87 to fa389ed Compare March 15, 2025 12:33

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
…erface that defines a constructor
@carlos-granados carlos-granados force-pushed the interface-unused-constructor-param branch from fa389ed to 1698c7f Compare March 15, 2025 12:34
@carlos-granados
Copy link
Contributor Author

I rebased this branch and fixed the linting issue

@ondrejmirtes ondrejmirtes merged commit b5f1cae into phpstan:2.1.x Mar 18, 2025
415 of 417 checks passed
@ondrejmirtes
Copy link
Member

Thank you.

For completeness it should also look for abstract constructor in parent classes and implemented traits.

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

Successfully merging this pull request may close these issues.

Constructor reports unused parameter when inherited from interface
3 participants