Skip to content

Commit b374e26

Browse files
committed
IllegalConstructorStaticCallRule - allow call parent of a parent
1 parent 69b936b commit b374e26

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/Rules/Methods/IllegalConstructorStaticCallRule.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Rules\Rule;
88
use PHPStan\Rules\RuleErrorBuilder;
9+
use function array_map;
10+
use function in_array;
11+
use function strtolower;
912

1013
/**
1114
* @implements Rule<Node\Expr\StaticCall>
@@ -52,7 +55,9 @@ private function isCollectCallingConstructor(Node $node, Scope $scope): bool
5255
return false;
5356
}
5457

55-
return $node->class->toLowerString() === 'parent';
58+
$parentClasses = array_map(static fn (string $name) => strtolower($name), $scope->getClassReflection()->getParentClassesNames());
59+
60+
return in_array(strtolower($scope->resolveName($node->class)), $parentClasses, true);
5661
}
5762

5863
}

tests/PHPStan/Rules/Methods/IllegalConstructorStaticCallRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function testMethods(): void
3939
'Static call to __construct() is only allowed on a parent class in the constructor.',
4040
50,
4141
],
42+
[
43+
'Static call to __construct() is only allowed on a parent class in the constructor.',
44+
100,
45+
],
4246
]);
4347
}
4448

tests/PHPStan/Rules/Methods/data/illegal-constructor-call-rule-test.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,43 @@ public function doFoo()
6161
}
6262

6363
}
64+
65+
abstract class Presenter
66+
{
67+
68+
public function __construct()
69+
{
70+
71+
}
72+
73+
}
74+
75+
abstract class BasePresenter extends Presenter
76+
{
77+
78+
public function __construct()
79+
{
80+
Presenter::__construct();
81+
}
82+
83+
}
84+
85+
class CatPresenter extends BasePresenter
86+
{
87+
88+
public function __construct()
89+
{
90+
Presenter::__construct();
91+
}
92+
93+
}
94+
95+
class DogPresenter extends BasePresenter
96+
{
97+
98+
public function __construct()
99+
{
100+
CatPresenter::__construct();
101+
}
102+
103+
}

0 commit comments

Comments
 (0)