From 6fa97eee650095d6a208200cc0dc4ea424bf43f0 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sun, 17 Apr 2022 13:00:08 +0200 Subject: [PATCH] Behave the same way with "self" like with the actual class name --- .../IllegalConstructorStaticCallRule.php | 18 ++++++++++++++++-- .../IllegalConstructorMethodCallRuleTest.php | 2 +- .../IllegalConstructorStaticCallRuleTest.php | 6 +++++- .../illegal-constructor-call-rule-test.php | 2 ++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Rules/Methods/IllegalConstructorStaticCallRule.php b/src/Rules/Methods/IllegalConstructorStaticCallRule.php index 8ff692bb5f7..353dc13913e 100644 --- a/src/Rules/Methods/IllegalConstructorStaticCallRule.php +++ b/src/Rules/Methods/IllegalConstructorStaticCallRule.php @@ -44,8 +44,22 @@ private function isCollectCallingConstructor(Node $node, Scope $scope): bool if ($scope->getFunction() !== null && $scope->getFunction()->getName() !== '__construct') { return false; } - // In constructor, static call is allowed with 'self' or 'parent; - return $node->class instanceof Node\Name && in_array($node->class->getFirst(), ['self', 'parent'], true); + + if (!$scope->isInClass()) { + return false; + } + + if (!$node->class instanceof Node\Name) { + return false; + } + + if ($node->class->toLowerString() === 'parent') { + return true; + } + + $className = $scope->resolveName($node->class); + + return $className === $scope->getClassReflection()->getName(); } } diff --git a/tests/PHPStan/Rules/Methods/IllegalConstructorMethodCallRuleTest.php b/tests/PHPStan/Rules/Methods/IllegalConstructorMethodCallRuleTest.php index d78bf3d1cb4..a609162a707 100644 --- a/tests/PHPStan/Rules/Methods/IllegalConstructorMethodCallRuleTest.php +++ b/tests/PHPStan/Rules/Methods/IllegalConstructorMethodCallRuleTest.php @@ -25,7 +25,7 @@ public function testMethods(): void ], [ 'Call to __construct() on an existing object is not allowed.', - 56, + 58, ], ]); } diff --git a/tests/PHPStan/Rules/Methods/IllegalConstructorStaticCallRuleTest.php b/tests/PHPStan/Rules/Methods/IllegalConstructorStaticCallRuleTest.php index 70a1f0fa777..6ec761d7a69 100644 --- a/tests/PHPStan/Rules/Methods/IllegalConstructorStaticCallRuleTest.php +++ b/tests/PHPStan/Rules/Methods/IllegalConstructorStaticCallRuleTest.php @@ -25,7 +25,11 @@ public function testMethods(): void ], [ 'Static call to __construct() is only allowed on a parent class in the constructor.', - 46, + 47, + ], + [ + 'Static call to __construct() is only allowed on a parent class in the constructor.', + 48, ], ]); } diff --git a/tests/PHPStan/Rules/Methods/data/illegal-constructor-call-rule-test.php b/tests/PHPStan/Rules/Methods/data/illegal-constructor-call-rule-test.php index e902002e315..8df51b725f2 100644 --- a/tests/PHPStan/Rules/Methods/data/illegal-constructor-call-rule-test.php +++ b/tests/PHPStan/Rules/Methods/data/illegal-constructor-call-rule-test.php @@ -39,11 +39,13 @@ public function __construct(string $datetime = "now", ?DateTimeZone $timezone = return; } self::__construct($datetime, $timezone); + ExtendedDateTimeWithSelfCall::__construct($datetime, $timezone); } public function mutate(string $datetime = "now", ?DateTimeZone $timezone = null): void { self::__construct($datetime, $timezone); + ExtendedDateTimeWithSelfCall::__construct($datetime, $timezone); } }