From 30c4b9e80f51af8b5f166ba3aae93d8409c9c0ea Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 22 Aug 2024 10:22:23 +0200 Subject: [PATCH] Bleeding edge - check existing classes in `@param-out` --- conf/bleedingEdge.neon | 1 + conf/config.neon | 2 ++ conf/parametersSchema.neon | 1 + src/Rules/FunctionDefinitionCheck.php | 7 +++++ ...lassesInArrowFunctionTypehintsRuleTest.php | 1 + ...stingClassesInClosureTypehintsRuleTest.php | 1 + .../ExistingClassesInTypehintsRuleTest.php | 19 ++++++++++++ .../Functions/data/param-out-classes.php | 23 +++++++++++++++ .../ExistingClassesInTypehintsRuleTest.php | 19 ++++++++++++ .../Rules/Methods/data/param-out-classes.php | 29 +++++++++++++++++++ 10 files changed, 103 insertions(+) create mode 100644 tests/PHPStan/Rules/Functions/data/param-out-classes.php create mode 100644 tests/PHPStan/Rules/Methods/data/param-out-classes.php diff --git a/conf/bleedingEdge.neon b/conf/bleedingEdge.neon index 00e7b08f1..a9f20432b 100644 --- a/conf/bleedingEdge.neon +++ b/conf/bleedingEdge.neon @@ -61,5 +61,6 @@ parameters: noImplicitWildcard: true tooWidePropertyType: true explicitThrow: true + absentTypeChecks: true stubFiles: - ../stubs/bleedingEdge/Rule.stub diff --git a/conf/config.neon b/conf/config.neon index bb384e951..4097e0d85 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -97,6 +97,7 @@ parameters: narrowPregMatches: true tooWidePropertyType: false explicitThrow: false + absentTypeChecks: false fileExtensions: - php checkAdvancedIsset: false @@ -970,6 +971,7 @@ services: arguments: checkClassCaseSensitivity: %checkClassCaseSensitivity% checkThisOnly: %checkThisOnly% + absentTypeChecks: %featureToggles.absentTypeChecks% - class: PHPStan\Rules\FunctionReturnTypeCheck diff --git a/conf/parametersSchema.neon b/conf/parametersSchema.neon index 3518d5ba4..2f3ef3b66 100644 --- a/conf/parametersSchema.neon +++ b/conf/parametersSchema.neon @@ -92,6 +92,7 @@ parametersSchema: narrowPregMatches: bool() tooWidePropertyType: bool() explicitThrow: bool() + absentTypeChecks: bool() ]) fileExtensions: listOf(string()) checkAdvancedIsset: bool() diff --git a/src/Rules/FunctionDefinitionCheck.php b/src/Rules/FunctionDefinitionCheck.php index fbedaffbd..50a0c99e8 100644 --- a/src/Rules/FunctionDefinitionCheck.php +++ b/src/Rules/FunctionDefinitionCheck.php @@ -52,6 +52,7 @@ public function __construct( private PhpVersion $phpVersion, private bool $checkClassCaseSensitivity, private bool $checkThisOnly, + private bool $absentTypeChecks, ) { } @@ -583,9 +584,15 @@ private function getParameterReferencedClasses(ParameterReflection $parameter): return $parameter->getNativeType()->getReferencedClasses(); } + $outTypeClasses = []; + if ($parameter->getOutType() !== null && $this->absentTypeChecks) { + $outTypeClasses = $parameter->getOutType()->getReferencedClasses(); + } + return array_merge( $parameter->getNativeType()->getReferencedClasses(), $parameter->getPhpDocType()->getReferencedClasses(), + $outTypeClasses, ); } diff --git a/tests/PHPStan/Rules/Functions/ExistingClassesInArrowFunctionTypehintsRuleTest.php b/tests/PHPStan/Rules/Functions/ExistingClassesInArrowFunctionTypehintsRuleTest.php index 2b2cec639..2417944d1 100644 --- a/tests/PHPStan/Rules/Functions/ExistingClassesInArrowFunctionTypehintsRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ExistingClassesInArrowFunctionTypehintsRuleTest.php @@ -34,6 +34,7 @@ protected function getRule(): Rule new PhpVersion($this->phpVersionId), true, false, + true, ), new PhpVersion(PHP_VERSION_ID), ); diff --git a/tests/PHPStan/Rules/Functions/ExistingClassesInClosureTypehintsRuleTest.php b/tests/PHPStan/Rules/Functions/ExistingClassesInClosureTypehintsRuleTest.php index 439f5bdd7..86f872557 100644 --- a/tests/PHPStan/Rules/Functions/ExistingClassesInClosureTypehintsRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ExistingClassesInClosureTypehintsRuleTest.php @@ -34,6 +34,7 @@ protected function getRule(): Rule new PhpVersion($this->phpVersionId), true, false, + true, ), ); } diff --git a/tests/PHPStan/Rules/Functions/ExistingClassesInTypehintsRuleTest.php b/tests/PHPStan/Rules/Functions/ExistingClassesInTypehintsRuleTest.php index 227044f2d..5cbe1c95e 100644 --- a/tests/PHPStan/Rules/Functions/ExistingClassesInTypehintsRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ExistingClassesInTypehintsRuleTest.php @@ -34,6 +34,7 @@ protected function getRule(): Rule new PhpVersion($this->phpVersionId), true, false, + true, ), ); } @@ -451,4 +452,22 @@ public function testTemplateInParamOut(): void ]); } + public function testParamOutClasses(): void + { + $this->analyse([__DIR__ . '/data/param-out-classes.php'], [ + [ + 'Parameter $p of function ParamOutClasses\doFoo() has invalid type ParamOutClasses\Nonexistent.', + 20, + ], + [ + 'Parameter $q of function ParamOutClasses\doFoo() has invalid type ParamOutClasses\FooTrait.', + 20, + ], + [ + 'Class ParamOutClasses\Foo referenced with incorrect case: ParamOutClasses\fOO.', + 20, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Functions/data/param-out-classes.php b/tests/PHPStan/Rules/Functions/data/param-out-classes.php new file mode 100644 index 000000000..8bb5c9da9 --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/param-out-classes.php @@ -0,0 +1,23 @@ +phpVersionId), true, false, + true, ), ); } @@ -471,4 +472,22 @@ public function testTemplateInParamOut(): void ]); } + public function testParamOutClasses(): void + { + $this->analyse([__DIR__ . '/data/param-out-classes.php'], [ + [ + 'Parameter $p of method ParamOutClassesMethods\Bar::doFoo() has invalid type ParamOutClassesMethods\Nonexistent.', + 23, + ], + [ + 'Parameter $q of method ParamOutClassesMethods\Bar::doFoo() has invalid type ParamOutClassesMethods\FooTrait.', + 23, + ], + [ + 'Class ParamOutClassesMethods\Foo referenced with incorrect case: ParamOutClassesMethods\fOO.', + 23, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/param-out-classes.php b/tests/PHPStan/Rules/Methods/data/param-out-classes.php new file mode 100644 index 000000000..8141c6b1f --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/param-out-classes.php @@ -0,0 +1,29 @@ +