Skip to content

Commit e995150

Browse files
committed
Prevent "Unsafe usage of new static() in abstract class" with final constructor
1 parent 8bb2a20 commit e995150

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Rules/Classes/NewStaticInAbstractClassStaticMethodRule.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ public function processNode(Node $node, Scope $scope): array
4949
return [];
5050
}
5151

52+
if ($classReflection->hasConstructor()) {
53+
$constructor = $classReflection->getConstructor();
54+
if (
55+
$constructor->isFinal()->yes()
56+
) {
57+
return [];
58+
}
59+
}
60+
5261
return [
5362
RuleErrorBuilder::message(sprintf(
5463
'Unsafe usage of new static() in abstract class %s in static method %s().',

tests/PHPStan/Rules/Classes/data/new-static-in-abstract-class-static-method.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,23 @@ public static function staticDoFoo(): void
3131
}
3232

3333
}
34+
35+
abstract class FinalConstructFoo
36+
{
37+
final function __construct() {
38+
39+
}
40+
41+
public function doFoo(): void
42+
{
43+
new static();
44+
}
45+
46+
public static function staticDoFoo(): void
47+
{
48+
new static();
49+
}
50+
51+
}
52+
53+
class Subclass extends FinalConstructFoo {}

0 commit comments

Comments
 (0)