From f2bf43c213819cc59f64f86c4421a664d1f75905 Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Fri, 10 Jan 2025 12:08:49 +0100 Subject: [PATCH] Do not report constructor unused parameter if class is an Attribute class --- .../Classes/UnusedConstructorParametersRule.php | 3 +++ .../Classes/UnusedConstructorParametersRuleTest.php | 5 +++++ tests/PHPStan/Rules/Classes/data/bug-7165.php | 12 ++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 tests/PHPStan/Rules/Classes/data/bug-7165.php diff --git a/src/Rules/Classes/UnusedConstructorParametersRule.php b/src/Rules/Classes/UnusedConstructorParametersRule.php index 8b38392470..d87581f69c 100644 --- a/src/Rules/Classes/UnusedConstructorParametersRule.php +++ b/src/Rules/Classes/UnusedConstructorParametersRule.php @@ -44,6 +44,9 @@ public function processNode(Node $node, Scope $scope): array if (count($originalNode->params) === 0) { return []; } + if ($node->getClassReflection()->isAttributeClass()) { + return []; + } $message = sprintf( 'Constructor of class %s has an unused parameter $%%s.', diff --git a/tests/PHPStan/Rules/Classes/UnusedConstructorParametersRuleTest.php b/tests/PHPStan/Rules/Classes/UnusedConstructorParametersRuleTest.php index beb402c267..cf547b909c 100644 --- a/tests/PHPStan/Rules/Classes/UnusedConstructorParametersRuleTest.php +++ b/tests/PHPStan/Rules/Classes/UnusedConstructorParametersRuleTest.php @@ -61,6 +61,11 @@ public function testBug1917(): void $this->analyse([__DIR__ . '/data/bug-1917.php'], []); } + public function testBug7165(): void + { + $this->analyse([__DIR__ . '/data/bug-7165.php'], []); + } + public function testBug10865(): void { $this->analyse([__DIR__ . '/data/bug-10865.php'], []); diff --git a/tests/PHPStan/Rules/Classes/data/bug-7165.php b/tests/PHPStan/Rules/Classes/data/bug-7165.php new file mode 100644 index 0000000000..3cbf90ed6d --- /dev/null +++ b/tests/PHPStan/Rules/Classes/data/bug-7165.php @@ -0,0 +1,12 @@ += 8.0 + +namespace Bug7165; + +#[\Attribute] +class MyAttribute +{ + public function __construct(string $name) + { + } +} +