From 5a4789339e5768628bf9bb2abdf38eff135553cd Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 13 Mar 2024 12:51:11 +0100 Subject: [PATCH] Too wide `@param-out` - fix for conditional types --- .../TooWideFunctionParameterOutTypeRule.php | 2 ++ .../TooWideMethodParameterOutTypeRule.php | 2 ++ .../TooWideFunctionParameterOutTypeRuleTest.php | 4 ++++ .../TooWideMethodParameterOutTypeRuleTest.php | 4 ++++ .../data/too-wide-function-parameter-out.php | 11 +++++++++++ .../data/too-wide-method-parameter-out.php | 12 ++++++++++++ 6 files changed, 35 insertions(+) diff --git a/src/Rules/TooWideTypehints/TooWideFunctionParameterOutTypeRule.php b/src/Rules/TooWideTypehints/TooWideFunctionParameterOutTypeRule.php index 5da3dd8131..38db248175 100644 --- a/src/Rules/TooWideTypehints/TooWideFunctionParameterOutTypeRule.php +++ b/src/Rules/TooWideTypehints/TooWideFunctionParameterOutTypeRule.php @@ -11,6 +11,7 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; +use PHPStan\Type\TypeUtils; use PHPStan\Type\UnionType; use PHPStan\Type\VerbosityLevel; use function sprintf; @@ -85,6 +86,7 @@ private function processSingleParameter( $outType = $parameter->getType(); } + $outType = TypeUtils::resolveLateResolvableTypes($outType); if (!$outType instanceof UnionType) { return []; } diff --git a/src/Rules/TooWideTypehints/TooWideMethodParameterOutTypeRule.php b/src/Rules/TooWideTypehints/TooWideMethodParameterOutTypeRule.php index 412f2a28dc..d3124bfbf6 100644 --- a/src/Rules/TooWideTypehints/TooWideMethodParameterOutTypeRule.php +++ b/src/Rules/TooWideTypehints/TooWideMethodParameterOutTypeRule.php @@ -11,6 +11,7 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; +use PHPStan\Type\TypeUtils; use PHPStan\Type\UnionType; use PHPStan\Type\VerbosityLevel; use function sprintf; @@ -85,6 +86,7 @@ private function processSingleParameter( $outType = $parameter->getType(); } + $outType = TypeUtils::resolveLateResolvableTypes($outType); if (!$outType instanceof UnionType) { return []; } diff --git a/tests/PHPStan/Rules/TooWideTypehints/TooWideFunctionParameterOutTypeRuleTest.php b/tests/PHPStan/Rules/TooWideTypehints/TooWideFunctionParameterOutTypeRuleTest.php index 64f27816ce..c2a620eb33 100644 --- a/tests/PHPStan/Rules/TooWideTypehints/TooWideFunctionParameterOutTypeRuleTest.php +++ b/tests/PHPStan/Rules/TooWideTypehints/TooWideFunctionParameterOutTypeRuleTest.php @@ -33,6 +33,10 @@ public function testRule(): void 23, 'You can narrow the parameter out type with @param-out PHPDoc tag.', ], + [ + 'Function TooWideFunctionParameterOut\bug10699() never assigns 20 to &$out so it can be removed from the @param-out type.', + 48, + ], ]); } diff --git a/tests/PHPStan/Rules/TooWideTypehints/TooWideMethodParameterOutTypeRuleTest.php b/tests/PHPStan/Rules/TooWideTypehints/TooWideMethodParameterOutTypeRuleTest.php index 9d336ffacf..38b080efda 100644 --- a/tests/PHPStan/Rules/TooWideTypehints/TooWideMethodParameterOutTypeRuleTest.php +++ b/tests/PHPStan/Rules/TooWideTypehints/TooWideMethodParameterOutTypeRuleTest.php @@ -33,6 +33,10 @@ public function testRule(): void 26, 'You can narrow the parameter out type with @param-out PHPDoc tag.', ], + [ + 'Method TooWideMethodParameterOut\Foo::bug10699() never assigns 20 to &$out so it can be removed from the @param-out type.', + 37, + ], ]); } diff --git a/tests/PHPStan/Rules/TooWideTypehints/data/too-wide-function-parameter-out.php b/tests/PHPStan/Rules/TooWideTypehints/data/too-wide-function-parameter-out.php index 5daea8b962..74130b87e2 100644 --- a/tests/PHPStan/Rules/TooWideTypehints/data/too-wide-function-parameter-out.php +++ b/tests/PHPStan/Rules/TooWideTypehints/data/too-wide-function-parameter-out.php @@ -38,3 +38,14 @@ function ipCheckData(string $host, ?\stdClass &$ipdata): bool return true; } + +/** + * @param int $flags + * @param 10 $out + * + * @param-out ($flags is 2 ? 20 : 10) $out + */ +function bug10699(int $flags, &$out): void +{ + +} diff --git a/tests/PHPStan/Rules/TooWideTypehints/data/too-wide-method-parameter-out.php b/tests/PHPStan/Rules/TooWideTypehints/data/too-wide-method-parameter-out.php index c097e6fced..f79bba8d8e 100644 --- a/tests/PHPStan/Rules/TooWideTypehints/data/too-wide-method-parameter-out.php +++ b/tests/PHPStan/Rules/TooWideTypehints/data/too-wide-method-parameter-out.php @@ -28,4 +28,16 @@ public function doLorem(?string &$p): void $p = 'foo'; } + /** + * @param int $flags + * @param 10 $out + * + * @param-out ($flags is 2 ? 20 : 10) $out + */ + function bug10699(int $flags, &$out): void + { + + } + + }