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

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Rules/Classes/UnusedConstructorParametersRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

foreach ($node->getClassReflection()->getInterfaces() as $interface) {
if ($interface->hasMethod('__construct')) {
return [];
}
}

$message = sprintf(
'Constructor of class %s has an unused parameter $%%s.',
SprintfHelper::escapeFormatString($node->getClassReflection()->getDisplayName()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ public function testBug10865(): void
$this->analyse([__DIR__ . '/data/bug-10865.php'], []);
}

public function testBug11454(): void
{
$this->analyse([__DIR__ . '/data/bug-11454.php'], []);
}

}
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-11454.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types = 1);

namespace Bug11454;

interface ConstructorInterface
{
public function __construct(string $a);
}

class Foo implements ConstructorInterface {
public function __construct(string $a)
{
}
}
Loading