From 4db44f3a3b58ff53ad29828775a093cc7851fa0a Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Apr 2025 08:51:37 +0200 Subject: [PATCH 1/7] Scope should not remeber the function scope --- src/Analyser/MutatingScope.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index b15018fbc8..22a895e03c 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -348,7 +348,7 @@ public function rememberConstructorScope(): self return $this->scopeFactory->create( $this->context, $this->isDeclareStrictTypes(), - $this->getFunction(), + null, $this->getNamespace(), $this->rememberConstructorExpressions($this->expressionTypes), $this->rememberConstructorExpressions($this->nativeExpressionTypes), From 5fb9c9bd8ddec7a386613f044490b16707871c93 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Apr 2025 09:09:06 +0200 Subject: [PATCH 2/7] added test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Michal Špaček <1966648+spaze@users.noreply.github.com> Co-Authored-By: Ruud Kamphuis --- .../Analyser/InstanceMethodsParameterRule.php | 29 +++++++++++++ .../InstanceMethodsParameterScopeTest.php | 43 +++++++++++++++++++ .../data/instance-methods-parameter-scope.php | 19 ++++++++ 3 files changed, 91 insertions(+) create mode 100644 tests/PHPStan/Analyser/InstanceMethodsParameterRule.php create mode 100644 tests/PHPStan/Analyser/InstanceMethodsParameterScopeTest.php create mode 100644 tests/PHPStan/Analyser/data/instance-methods-parameter-scope.php diff --git a/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php b/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php new file mode 100644 index 0000000000..c56daeb834 --- /dev/null +++ b/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php @@ -0,0 +1,29 @@ + + */ +class InstanceMethodsParameterRule implements Rule +{ + + public function getNodeType(): string + { + return FullyQualified::class; + } + + public function processNode(Node $node, Scope $scope): array + { + return [ + RuleErrorBuilder::message(sprintf('Name %s found in method %s', $node->toString(), $scope->getFunction()?->getName() ?? 'null'))->identifier('test.instanceOfMethodsParameterRule')->build(), + ]; + } + +} diff --git a/tests/PHPStan/Analyser/InstanceMethodsParameterScopeTest.php b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeTest.php new file mode 100644 index 0000000000..99f5d584f0 --- /dev/null +++ b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeTest.php @@ -0,0 +1,43 @@ + + */ +class InstanceMethodsParameterScopeTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new InstanceMethodsParameterRule(); + } + + protected function shouldNarrowMethodScopeFromConstructor(): bool + { + return true; + } + + public function testRule(): void + { + if (PHP_VERSION_ID < 80000) { + $this->markTestSkipped('Test requires PHP 8.0.'); + } + + $this->analyse([__DIR__ . '/data/instance-methods-parameter-scope.php'], [ + [ + 'Name DateTime found in method null', + 12, + ], + [ + 'Name Baz\Waldo found in method null', + 16, + ], + ]); + } + +} diff --git a/tests/PHPStan/Analyser/data/instance-methods-parameter-scope.php b/tests/PHPStan/Analyser/data/instance-methods-parameter-scope.php new file mode 100644 index 0000000000..a861ac8edd --- /dev/null +++ b/tests/PHPStan/Analyser/data/instance-methods-parameter-scope.php @@ -0,0 +1,19 @@ + Date: Fri, 18 Apr 2025 09:27:39 +0200 Subject: [PATCH 3/7] Update InstanceMethodsParameterRule.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Michal Špaček <1966648+spaze@users.noreply.github.com> Co-Authored-By: Ruud Kamphuis --- tests/PHPStan/Analyser/InstanceMethodsParameterRule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php b/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php index c56daeb834..2f8070902e 100644 --- a/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php +++ b/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php @@ -1,4 +1,4 @@ -= 8.0 namespace PHPStan\Analyser; From 3ab246a891869b29aa5a43bb8359aa9df8722459 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Apr 2025 09:39:23 +0200 Subject: [PATCH 4/7] fix build --- tests/PHPStan/Analyser/InstanceMethodsParameterRule.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php b/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php index 2f8070902e..7ecdfd5fc5 100644 --- a/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php +++ b/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php @@ -1,4 +1,4 @@ -= 8.0 +getFunction() !== null) { + throw new ShouldNotHappenException('All names in the tests should not have a scope function.'); + } + return [ - RuleErrorBuilder::message(sprintf('Name %s found in method %s', $node->toString(), $scope->getFunction()?->getName() ?? 'null'))->identifier('test.instanceOfMethodsParameterRule')->build(), + RuleErrorBuilder::message(sprintf('Name %s found in method null', $node->toString()))->identifier('test.instanceOfMethodsParameterRule')->build(), ]; } From 54cf75ebd7cf96474c8d3ecb09e14ac3988d05c7 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Apr 2025 09:40:37 +0200 Subject: [PATCH 5/7] better names --- ...le.php => InstanceMethodsParameterScopeFunctionRule.php} | 2 +- ...st.php => InstanceMethodsParameterScopeFunctionTest.php} | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) rename tests/PHPStan/Analyser/{InstanceMethodsParameterRule.php => InstanceMethodsParameterScopeFunctionRule.php} (92%) rename tests/PHPStan/Analyser/{InstanceMethodsParameterScopeTest.php => InstanceMethodsParameterScopeFunctionTest.php} (76%) diff --git a/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionRule.php similarity index 92% rename from tests/PHPStan/Analyser/InstanceMethodsParameterRule.php rename to tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionRule.php index 7ecdfd5fc5..0e3220771c 100644 --- a/tests/PHPStan/Analyser/InstanceMethodsParameterRule.php +++ b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionRule.php @@ -12,7 +12,7 @@ /** * @implements Rule */ -class InstanceMethodsParameterRule implements Rule +class InstanceMethodsParameterScopeFunctionRule implements Rule { public function getNodeType(): string diff --git a/tests/PHPStan/Analyser/InstanceMethodsParameterScopeTest.php b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionTest.php similarity index 76% rename from tests/PHPStan/Analyser/InstanceMethodsParameterScopeTest.php rename to tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionTest.php index 99f5d584f0..b1f2cdbb5e 100644 --- a/tests/PHPStan/Analyser/InstanceMethodsParameterScopeTest.php +++ b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionTest.php @@ -7,14 +7,14 @@ use const PHP_VERSION_ID; /** - * @extends RuleTestCase + * @extends RuleTestCase */ -class InstanceMethodsParameterScopeTest extends RuleTestCase +class InstanceMethodsParameterScopeFunctionTest extends RuleTestCase { protected function getRule(): Rule { - return new InstanceMethodsParameterRule(); + return new InstanceMethodsParameterScopeFunctionRule(); } protected function shouldNarrowMethodScopeFromConstructor(): bool From b9220404b0ca3cfb287c6d39212e18e3ff134c9d Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Apr 2025 09:41:36 +0200 Subject: [PATCH 6/7] clearer message --- .../Analyser/InstanceMethodsParameterScopeFunctionRule.php | 2 +- .../Analyser/InstanceMethodsParameterScopeFunctionTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionRule.php b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionRule.php index 0e3220771c..804cdcebfd 100644 --- a/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionRule.php +++ b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionRule.php @@ -27,7 +27,7 @@ public function processNode(Node $node, Scope $scope): array } return [ - RuleErrorBuilder::message(sprintf('Name %s found in method null', $node->toString()))->identifier('test.instanceOfMethodsParameterRule')->build(), + RuleErrorBuilder::message(sprintf('Name %s found in scope-function null', $node->toString()))->identifier('test.instanceOfMethodsParameterRule')->build(), ]; } diff --git a/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionTest.php b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionTest.php index b1f2cdbb5e..ecb5b856c9 100644 --- a/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionTest.php +++ b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionTest.php @@ -30,11 +30,11 @@ public function testRule(): void $this->analyse([__DIR__ . '/data/instance-methods-parameter-scope.php'], [ [ - 'Name DateTime found in method null', + 'Name DateTime found in scope-function null', 12, ], [ - 'Name Baz\Waldo found in method null', + 'Name Baz\Waldo found in scope-function null', 16, ], ]); From f70f0719651fc548f83bce62bc3d481249c49525 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Apr 2025 10:31:45 +0200 Subject: [PATCH 7/7] function scope --- .../Analyser/InstanceMethodsParameterScopeFunctionRule.php | 4 ++-- .../Analyser/InstanceMethodsParameterScopeFunctionTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionRule.php b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionRule.php index 804cdcebfd..f3fce8fd9d 100644 --- a/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionRule.php +++ b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionRule.php @@ -23,11 +23,11 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { if ($scope->getFunction() !== null) { - throw new ShouldNotHappenException('All names in the tests should not have a scope function.'); + throw new ShouldNotHappenException('All names in the tests should not have a function scope.'); } return [ - RuleErrorBuilder::message(sprintf('Name %s found in scope-function null', $node->toString()))->identifier('test.instanceOfMethodsParameterRule')->build(), + RuleErrorBuilder::message(sprintf('Name %s found in function scope null', $node->toString()))->identifier('test.instanceOfMethodsParameterRule')->build(), ]; } diff --git a/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionTest.php b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionTest.php index ecb5b856c9..7cbae1c8be 100644 --- a/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionTest.php +++ b/tests/PHPStan/Analyser/InstanceMethodsParameterScopeFunctionTest.php @@ -30,11 +30,11 @@ public function testRule(): void $this->analyse([__DIR__ . '/data/instance-methods-parameter-scope.php'], [ [ - 'Name DateTime found in scope-function null', + 'Name DateTime found in function scope null', 12, ], [ - 'Name Baz\Waldo found in scope-function null', + 'Name Baz\Waldo found in function scope null', 16, ], ]);