From 50fc99105cb535b08ffd2ad3f57002071d19a8da Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 11 Nov 2025 16:35:08 +0100 Subject: [PATCH] Do not instantiate Accessory without string type --- .../Php/LtrimFunctionReturnTypeExtension.php | 2 +- ...TrimFunctionDynamicReturnTypeExtension.php | 2 +- .../CallToFunctionParametersRuleTest.php | 5 +++++ .../Rules/Functions/data/bug-13784.php | 22 +++++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/PHPStan/Rules/Functions/data/bug-13784.php diff --git a/src/Type/Php/LtrimFunctionReturnTypeExtension.php b/src/Type/Php/LtrimFunctionReturnTypeExtension.php index 2db0c7be4e..24db9d1304 100644 --- a/src/Type/Php/LtrimFunctionReturnTypeExtension.php +++ b/src/Type/Php/LtrimFunctionReturnTypeExtension.php @@ -72,7 +72,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, } elseif ($trimConstantString->getValue() === '\\' && $string->isClassString()->yes()) { $result[] = new ClassStringType(); } elseif (preg_match('/\d/', $trimConstantString->getValue()) === 0 && $string->isNumericString()->yes()) { - $result[] = new AccessoryNumericStringType(); + $result[] = new IntersectionType([new StringType(), new AccessoryNumericStringType()]); } else { return $defaultType; } diff --git a/src/Type/Php/TrimFunctionDynamicReturnTypeExtension.php b/src/Type/Php/TrimFunctionDynamicReturnTypeExtension.php index fd016b7650..c7df164334 100644 --- a/src/Type/Php/TrimFunctionDynamicReturnTypeExtension.php +++ b/src/Type/Php/TrimFunctionDynamicReturnTypeExtension.php @@ -78,7 +78,7 @@ public function getTypeFromFunctionCall( ); } } elseif (preg_match('/\d/', $trimConstantString->getValue()) === 0 && $stringType->isNumericString()->yes()) { - $result[] = new AccessoryNumericStringType(); + $result[] = new IntersectionType([new StringType(), new AccessoryNumericStringType()]); } else { return $defaultType; } diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index 5f4bb1bf7c..690a7615ce 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -2380,6 +2380,11 @@ public function testBug11863(): void $this->analyse([__DIR__ . '/data/bug-11863.php'], []); } + public function testBug13784(): void + { + $this->analyse([__DIR__ . '/data/bug-13784.php'], []); + } + public function testBug13556(): void { $this->checkExplicitMixed = true; diff --git a/tests/PHPStan/Rules/Functions/data/bug-13784.php b/tests/PHPStan/Rules/Functions/data/bug-13784.php new file mode 100644 index 0000000000..84c6be36ee --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-13784.php @@ -0,0 +1,22 @@ +