From 56b1476d2976614110fe6cb0559040b2aa98d648 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 30 Mar 2026 03:56:53 +0000 Subject: [PATCH] Fix phpstan/phpstan#10076: Union of string literal and class-string loses literal members - GenericClassStringType::isSuperTypeOf() returned Yes for non-class-string constants when the generic type was MixedType (including TemplateMixedType) - This caused TypeCombinator::union to absorb string literals like 'object' and 'array' into class-string, losing them from the union - Added a check: when generic type is MixedType but the constant string is not a class-string, return Maybe instead of Yes - New regression test in tests/PHPStan/Rules/Functions/data/bug-10076.php --- src/Type/Generic/GenericClassStringType.php | 3 +++ .../CallToFunctionParametersRuleTest.php | 5 +++++ .../Rules/Functions/data/bug-10076.php | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 tests/PHPStan/Rules/Functions/data/bug-10076.php diff --git a/src/Type/Generic/GenericClassStringType.php b/src/Type/Generic/GenericClassStringType.php index b4ac3eff88c..1d0cf1d5892 100644 --- a/src/Type/Generic/GenericClassStringType.php +++ b/src/Type/Generic/GenericClassStringType.php @@ -94,6 +94,9 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult if ($type instanceof ConstantStringType) { $genericType = $this->type; if ($genericType instanceof MixedType) { + if (!$type->isClassString()->yes()) { + return IsSuperTypeOfResult::createMaybe(); + } return IsSuperTypeOfResult::createYes(); } diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index ee754a40d90..fb855592c83 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -2817,4 +2817,9 @@ public function testBug14312b(): void $this->analyse([__DIR__ . '/data/bug-14312b.php'], []); } + public function testBug10076(): void + { + $this->analyse([__DIR__ . '/data/bug-10076.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Functions/data/bug-10076.php b/tests/PHPStan/Rules/Functions/data/bug-10076.php new file mode 100644 index 00000000000..ca4a9b404b6 --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-10076.php @@ -0,0 +1,20 @@ + $type + * + * @return list + */ +function result($type = 'object'): array +{ + return []; +} + +result(); +result('object'); +result('array'); +result(\DateTime::class);