Skip to content

Commit b6ad10a

Browse files
committed
Fix ConsistentConstructorRule
1 parent 66af941 commit b6ad10a

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

src/Rules/Methods/ConsistentConstructorRule.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\RegisteredRule;
88
use PHPStan\Node\InClassMethodNode;
9+
use PHPStan\Reflection\ClassReflection;
910
use PHPStan\Reflection\Dummy\DummyConstructorReflection;
11+
use PHPStan\Reflection\ExtendedMethodReflection;
1012
use PHPStan\Rules\Rule;
1113
use function array_merge;
1214
use function strtolower;
@@ -36,18 +38,12 @@ public function processNode(Node $node, Scope $scope): array
3638
}
3739

3840
$parent = $method->getDeclaringClass()->getParentClass();
39-
4041
if ($parent === null) {
4142
return [];
4243
}
4344

44-
if ($parent->hasConstructor()) {
45-
$parentConstructor = $parent->getConstructor();
46-
} else {
47-
$parentConstructor = new DummyConstructorReflection($parent);
48-
}
49-
50-
if (! $parentConstructor->getDeclaringClass()->hasConsistentConstructor()) {
45+
$parentConstructor = $this->findConsistentParentConstructor($parent);
46+
if ($parentConstructor === null) {
5147
return [];
5248
}
5349

@@ -57,4 +53,22 @@ public function processNode(Node $node, Scope $scope): array
5753
);
5854
}
5955

56+
private function findConsistentParentConstructor(ClassReflection $classReflection): ?ExtendedMethodReflection
57+
{
58+
if ($classReflection->hasConsistentConstructor()) {
59+
if ($classReflection->hasConstructor()) {
60+
return $classReflection->getConstructor();
61+
}
62+
63+
return new DummyConstructorReflection($classReflection);
64+
}
65+
66+
$parent = $classReflection->getParentClass();
67+
if ($parent === null) {
68+
return null;
69+
}
70+
71+
return $this->findConsistentParentConstructor($parent);
72+
}
73+
6074
}

tests/PHPStan/Rules/Methods/ConsistentConstructorRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function testRule(): void
3737
'Method ConsistentConstructor\FakeConnection::__construct() overrides method ConsistentConstructor\Connection::__construct() but misses parameter #1 $i.',
3838
78,
3939
],
40+
[
41+
'Parameter #1 $i of method ConsistentConstructor\ChildTwo::__construct() is not optional.',
42+
102,
43+
],
4044
]);
4145
}
4246

tests/PHPStan/Rules/Methods/data/consistent-constructor.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,28 @@ public function __construct()
8080
}
8181
}
8282

83+
class ParentOne
84+
{
85+
public function __construct()
86+
{
87+
88+
}
89+
}
90+
91+
/**
92+
* @phpstan-consistent-constructor
93+
*/
94+
class ChildOne extends ParentOne
95+
{
96+
97+
}
98+
99+
class ChildTwo extends ChildOne
100+
{
101+
102+
public function __construct(int $i)
103+
{
104+
105+
}
106+
107+
}

0 commit comments

Comments
 (0)