From 18930cca0e2eb932733a78d3e22b703bad4f33da Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sun, 17 Apr 2022 13:06:58 +0200 Subject: [PATCH] All of these should be a violation --- .../IllegalConstructorMethodCallRule.php | 17 ----------------- .../IllegalConstructorStaticCallRule.php | 8 +------- .../IllegalConstructorMethodCallRuleTest.php | 4 ++++ .../IllegalConstructorStaticCallRuleTest.php | 8 ++++++++ 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/Rules/Methods/IllegalConstructorMethodCallRule.php b/src/Rules/Methods/IllegalConstructorMethodCallRule.php index c99e5814ba..94eb726ab3 100644 --- a/src/Rules/Methods/IllegalConstructorMethodCallRule.php +++ b/src/Rules/Methods/IllegalConstructorMethodCallRule.php @@ -24,27 +24,10 @@ public function processNode(Node $node, Scope $scope): array return []; } - if ($this->isCollectCallingConstructor($node, $scope)) { - return []; - } - return [ RuleErrorBuilder::message('Call to __construct() on an existing object is not allowed.') ->build(), ]; } - private function isCollectCallingConstructor(Node $node, Scope $scope): bool - { - if (!$node instanceof Node\Expr\MethodCall) { - return true; - } - // __construct should be called from inside constructor - if ($scope->getFunction() !== null && $scope->getFunction()->getName() !== '__construct') { - return false; - } - // In constructor, method call is allowed with 'this'; - return $node->var instanceof Node\Expr\Variable && $node->var->name === 'this'; - } - } diff --git a/src/Rules/Methods/IllegalConstructorStaticCallRule.php b/src/Rules/Methods/IllegalConstructorStaticCallRule.php index f1fe067c99..42647f1de8 100644 --- a/src/Rules/Methods/IllegalConstructorStaticCallRule.php +++ b/src/Rules/Methods/IllegalConstructorStaticCallRule.php @@ -52,13 +52,7 @@ private function isCollectCallingConstructor(Node $node, Scope $scope): bool return false; } - if ($node->class->toLowerString() === 'parent') { - return true; - } - - $className = $scope->resolveName($node->class); - - return $className === $scope->getClassReflection()->getName(); + return $node->class->toLowerString() === 'parent'; } } diff --git a/tests/PHPStan/Rules/Methods/IllegalConstructorMethodCallRuleTest.php b/tests/PHPStan/Rules/Methods/IllegalConstructorMethodCallRuleTest.php index 0fc10a5e81..8b0f957e85 100644 --- a/tests/PHPStan/Rules/Methods/IllegalConstructorMethodCallRuleTest.php +++ b/tests/PHPStan/Rules/Methods/IllegalConstructorMethodCallRuleTest.php @@ -19,6 +19,10 @@ protected function getRule(): Rule public function testMethods(): void { $this->analyse([__DIR__ . '/data/illegal-constructor-call-rule-test.php'], [ + [ + 'Call to __construct() on an existing object is not allowed.', + 13, + ], [ 'Call to __construct() on an existing object is not allowed.', 18, diff --git a/tests/PHPStan/Rules/Methods/IllegalConstructorStaticCallRuleTest.php b/tests/PHPStan/Rules/Methods/IllegalConstructorStaticCallRuleTest.php index eb1232cb9a..922c497fa0 100644 --- a/tests/PHPStan/Rules/Methods/IllegalConstructorStaticCallRuleTest.php +++ b/tests/PHPStan/Rules/Methods/IllegalConstructorStaticCallRuleTest.php @@ -23,6 +23,14 @@ public function testMethods(): void 'Static call to __construct() is only allowed on a parent class in the constructor.', 31, ], + [ + 'Static call to __construct() is only allowed on a parent class in the constructor.', + 43, + ], + [ + 'Static call to __construct() is only allowed on a parent class in the constructor.', + 44, + ], [ 'Static call to __construct() is only allowed on a parent class in the constructor.', 49,