From 2fa539a39e06bcc3155b109fd8d246703ceb176d Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Fri, 23 Aug 2024 09:47:25 +0200 Subject: [PATCH] Bleeding edge - check existing classes in `@param-closure-this` --- src/Rules/FunctionDefinitionCheck.php | 13 +++++--- .../ExistingClassesInTypehintsRuleTest.php | 18 +++++++++++ .../data/param-closure-this-classes.php | 27 ++++++++++++++++ .../ExistingClassesInTypehintsRuleTest.php | 18 +++++++++++ .../data/param-closure-this-classes.php | 32 +++++++++++++++++++ 5 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 tests/PHPStan/Rules/Functions/data/param-closure-this-classes.php create mode 100644 tests/PHPStan/Rules/Methods/data/param-closure-this-classes.php diff --git a/src/Rules/FunctionDefinitionCheck.php b/src/Rules/FunctionDefinitionCheck.php index 50a0c99e85..c49d6ce4ea 100644 --- a/src/Rules/FunctionDefinitionCheck.php +++ b/src/Rules/FunctionDefinitionCheck.php @@ -584,15 +584,20 @@ private function getParameterReferencedClasses(ParameterReflection $parameter): return $parameter->getNativeType()->getReferencedClasses(); } - $outTypeClasses = []; - if ($parameter->getOutType() !== null && $this->absentTypeChecks) { - $outTypeClasses = $parameter->getOutType()->getReferencedClasses(); + $moreClasses = []; + if ($this->absentTypeChecks) { + if ($parameter->getOutType() !== null) { + $moreClasses = array_merge($moreClasses, $parameter->getOutType()->getReferencedClasses()); + } + if ($parameter->getClosureThisType() !== null) { + $moreClasses = array_merge($moreClasses, $parameter->getClosureThisType()->getReferencedClasses()); + } } return array_merge( $parameter->getNativeType()->getReferencedClasses(), $parameter->getPhpDocType()->getReferencedClasses(), - $outTypeClasses, + $moreClasses, ); } diff --git a/tests/PHPStan/Rules/Functions/ExistingClassesInTypehintsRuleTest.php b/tests/PHPStan/Rules/Functions/ExistingClassesInTypehintsRuleTest.php index 5cbe1c95eb..6dc1acf00c 100644 --- a/tests/PHPStan/Rules/Functions/ExistingClassesInTypehintsRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ExistingClassesInTypehintsRuleTest.php @@ -470,4 +470,22 @@ public function testParamOutClasses(): void ]); } + public function testParamClosureThisClasses(): void + { + $this->analyse([__DIR__ . '/data/param-closure-this-classes.php'], [ + [ + 'Parameter $a of method ParamClosureThisClasses\Bar::doFoo() has invalid type ParamClosureThisClasses\Nonexistent.', + 24, + ], + [ + 'Parameter $b of method ParamClosureThisClasses\Bar::doFoo() has invalid type ParamClosureThisClasses\FooTrait.', + 25, + ], + [ + 'Class ParamClosureThisClasses\Foo referenced with incorrect case: ParamClosureThisClasses\fOO.', + 26, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Functions/data/param-closure-this-classes.php b/tests/PHPStan/Rules/Functions/data/param-closure-this-classes.php new file mode 100644 index 0000000000..d1652c132b --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/param-closure-this-classes.php @@ -0,0 +1,27 @@ +analyse([__DIR__ . '/data/param-closure-this-classes.php'], [ + [ + 'Parameter $a of method ParamClosureThisClasses\Bar::doFoo() has invalid type ParamClosureThisClasses\Nonexistent.', + 24, + ], + [ + 'Parameter $b of method ParamClosureThisClasses\Bar::doFoo() has invalid type ParamClosureThisClasses\FooTrait.', + 25, + ], + [ + 'Class ParamClosureThisClasses\Foo referenced with incorrect case: ParamClosureThisClasses\fOO.', + 26, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/param-closure-this-classes.php b/tests/PHPStan/Rules/Methods/data/param-closure-this-classes.php new file mode 100644 index 0000000000..f36ffbfb1f --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/param-closure-this-classes.php @@ -0,0 +1,32 @@ +