From f19db20293e07ac0aa8c55a16090918b9dd3ee0d Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Wed, 2 Dec 2026 17:18:43 +0000 Subject: [PATCH 1/2] Report the witness type of mixed as the given type When mixed is accepted but its witness type is not, rules now describe the witness as the given type: `return $c ? $mixed : 1;` in a function returning string reports "should return string but returns int." from level 7 instead of "returns mixed" with a tip saying mixed is int on some paths. RuleLevelHelper::accepts() checks the mixed itself first, so a mixed that is rejected on its own - explicit mixed from level 9, implicit mixed on level 10 - is still reported as mixed. RuleLevelHelperAcceptsResult carries the rejected witness type for the rules describing the given type. The native extension mirrors it. Amp-Thread-ID: https://ampcode.com/threads/T-01a237b0-3e9f-77ea-bd9b-d9a06b69686b --- src/Rules/FunctionCallParametersCheck.php | 5 +- src/Rules/FunctionReturnTypeCheck.php | 5 +- .../Functions/ReturnTypeAfterFinallyRule.php | 5 +- src/Rules/Generators/YieldFromTypeRule.php | 10 ++-- src/Rules/Generators/YieldTypeRule.php | 10 ++-- ...aultValueTypesAssignedToPropertiesRule.php | 5 +- .../TypesAssignedToPropertiesRule.php | 5 +- src/Rules/RuleLevelHelper.php | 34 +++++------ src/Rules/RuleLevelHelperAcceptsResult.php | 6 +- .../Analyser/AnalyserIntegrationTest.php | 2 +- .../PHPStan/Levels/LevelsIntegrationTest.php | 1 + .../Levels/data/mixedWitness-10-missing.json | 42 +++++++++++++ .../PHPStan/Levels/data/mixedWitness-10.json | 22 +++++++ tests/PHPStan/Levels/data/mixedWitness-6.json | 22 +++++++ tests/PHPStan/Levels/data/mixedWitness-7.json | 37 ++++++++++++ tests/PHPStan/Levels/data/mixedWitness-8.json | 7 +++ .../Levels/data/mixedWitness-9-missing.json | 22 +++++++ tests/PHPStan/Levels/data/mixedWitness-9.json | 27 +++++++++ tests/PHPStan/Levels/data/mixedWitness.php | 59 +++++++++++++++++++ .../CallToFunctionParametersRuleTest.php | 3 +- turbo-ext/src/RuleLevelHelper.cpp | 58 +++++++----------- 21 files changed, 308 insertions(+), 79 deletions(-) create mode 100644 tests/PHPStan/Levels/data/mixedWitness-10-missing.json create mode 100644 tests/PHPStan/Levels/data/mixedWitness-10.json create mode 100644 tests/PHPStan/Levels/data/mixedWitness-6.json create mode 100644 tests/PHPStan/Levels/data/mixedWitness-7.json create mode 100644 tests/PHPStan/Levels/data/mixedWitness-8.json create mode 100644 tests/PHPStan/Levels/data/mixedWitness-9-missing.json create mode 100644 tests/PHPStan/Levels/data/mixedWitness-9.json create mode 100644 tests/PHPStan/Levels/data/mixedWitness.php diff --git a/src/Rules/FunctionCallParametersCheck.php b/src/Rules/FunctionCallParametersCheck.php index 4b9b3d8b6d4..99637cb3f32 100644 --- a/src/Rules/FunctionCallParametersCheck.php +++ b/src/Rules/FunctionCallParametersCheck.php @@ -505,12 +505,13 @@ public function check( } if (!$accepts->result) { - $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $argumentValueType); + $givenType = $accepts->witnessType ?? $argumentValueType; + $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $givenType); $errors[] = RuleErrorBuilder::message(sprintf( $wrongArgumentTypeMessage, $this->describeParameter($parameter, $argumentName ?? $i + 1), $parameterType->describe($verbosityLevel), - $argumentValueType->describe($verbosityLevel), + $givenType->describe($verbosityLevel), )) ->identifier('argument.type') ->line($argumentLine) diff --git a/src/Rules/FunctionReturnTypeCheck.php b/src/Rules/FunctionReturnTypeCheck.php index e205930591a..375c0dc2868 100644 --- a/src/Rules/FunctionReturnTypeCheck.php +++ b/src/Rules/FunctionReturnTypeCheck.php @@ -92,13 +92,14 @@ public function checkReturnType( $accepts = $this->ruleLevelHelper->accepts($returnType, $returnValueType, $scope->isDeclareStrictTypes()); if (!$accepts->result) { - $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType, $returnValueType); + $givenType = $accepts->witnessType ?? $returnValueType; + $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType, $givenType); return [ RuleErrorBuilder::message(sprintf( $typeMismatchMessage, $returnType->describe($verbosityLevel), - $returnValueType->describe($verbosityLevel), + $givenType->describe($verbosityLevel), )) ->line($returnNode->getStartLine()) ->identifier('return.type') diff --git a/src/Rules/Functions/ReturnTypeAfterFinallyRule.php b/src/Rules/Functions/ReturnTypeAfterFinallyRule.php index 28271dff5df..1684b089459 100644 --- a/src/Rules/Functions/ReturnTypeAfterFinallyRule.php +++ b/src/Rules/Functions/ReturnTypeAfterFinallyRule.php @@ -90,12 +90,13 @@ public function processNode(Node $node, Scope $scope): array continue; } - $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType, $typeAfterFinally); + $givenType = $accepts->witnessType ?? $typeAfterFinally; + $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType, $givenType); $errors[] = RuleErrorBuilder::message(sprintf( '%s should return %s but returns %s because the finally block modifies the value returned by reference.', $description, $returnType->describe($verbosityLevel), - $typeAfterFinally->describe($verbosityLevel), + $givenType->describe($verbosityLevel), )) ->line($returnNode->getStartLine()) ->identifier('return.byRefFinally') diff --git a/src/Rules/Generators/YieldFromTypeRule.php b/src/Rules/Generators/YieldFromTypeRule.php index a0102c5c9e1..492c3b41f82 100644 --- a/src/Rules/Generators/YieldFromTypeRule.php +++ b/src/Rules/Generators/YieldFromTypeRule.php @@ -84,11 +84,12 @@ public function processNode(Node $node, Scope $scope): array $messages = []; $acceptsKey = $this->ruleLevelHelper->accepts($returnType->getIterableKeyType(), $exprType->getIterableKeyType(), $scope->isDeclareStrictTypes()); if (!$acceptsKey->result) { - $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $exprType->getIterableKeyType()); + $givenKeyType = $acceptsKey->witnessType ?? $exprType->getIterableKeyType(); + $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $givenKeyType); $messages[] = RuleErrorBuilder::message(sprintf( 'Generator expects key type %s, %s given.', $returnType->getIterableKeyType()->describe($verbosityLevel), - $exprType->getIterableKeyType()->describe($verbosityLevel), + $givenKeyType->describe($verbosityLevel), )) ->line($node->expr->getStartLine()) ->identifier('generator.keyType') @@ -98,11 +99,12 @@ public function processNode(Node $node, Scope $scope): array $acceptsValue = $this->ruleLevelHelper->accepts($returnType->getIterableValueType(), $exprType->getIterableValueType(), $scope->isDeclareStrictTypes()); if (!$acceptsValue->result) { - $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $exprType->getIterableValueType()); + $givenValueType = $acceptsValue->witnessType ?? $exprType->getIterableValueType(); + $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $givenValueType); $messages[] = RuleErrorBuilder::message(sprintf( 'Generator expects value type %s, %s given.', $returnType->getIterableValueType()->describe($verbosityLevel), - $exprType->getIterableValueType()->describe($verbosityLevel), + $givenValueType->describe($verbosityLevel), )) ->line($node->expr->getStartLine()) ->identifier('generator.valueType') diff --git a/src/Rules/Generators/YieldTypeRule.php b/src/Rules/Generators/YieldTypeRule.php index 0f42eecd120..a39d9c1e3a8 100644 --- a/src/Rules/Generators/YieldTypeRule.php +++ b/src/Rules/Generators/YieldTypeRule.php @@ -57,11 +57,12 @@ public function processNode(Node $node, Scope $scope): array $messages = []; $acceptsKey = $this->ruleLevelHelper->accepts($returnType->getIterableKeyType(), $keyType, $scope->isDeclareStrictTypes()); if (!$acceptsKey->result) { - $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $keyType); + $givenKeyType = $acceptsKey->witnessType ?? $keyType; + $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $givenKeyType); $messages[] = RuleErrorBuilder::message(sprintf( 'Generator expects key type %s, %s given.', $returnType->getIterableKeyType()->describe($verbosityLevel), - $keyType->describe($verbosityLevel), + $givenKeyType->describe($verbosityLevel), )) ->acceptsReasonsTip($acceptsKey->reasons) ->identifier('generator.keyType') @@ -76,11 +77,12 @@ public function processNode(Node $node, Scope $scope): array $acceptsValue = $this->ruleLevelHelper->accepts($returnType->getIterableValueType(), $valueType, $scope->isDeclareStrictTypes()); if (!$acceptsValue->result) { - $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $valueType); + $givenValueType = $acceptsValue->witnessType ?? $valueType; + $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $givenValueType); $messages[] = RuleErrorBuilder::message(sprintf( 'Generator expects value type %s, %s given.', $returnType->getIterableValueType()->describe($verbosityLevel), - $valueType->describe($verbosityLevel), + $givenValueType->describe($verbosityLevel), )) ->acceptsReasonsTip($acceptsValue->reasons) ->identifier('generator.valueType') diff --git a/src/Rules/Properties/DefaultValueTypesAssignedToPropertiesRule.php b/src/Rules/Properties/DefaultValueTypesAssignedToPropertiesRule.php index ed947b1a67a..c340878e6b2 100644 --- a/src/Rules/Properties/DefaultValueTypesAssignedToPropertiesRule.php +++ b/src/Rules/Properties/DefaultValueTypesAssignedToPropertiesRule.php @@ -50,7 +50,8 @@ public function processNode(Node $node, Scope $scope): array return []; } - $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $defaultValueType); + $givenType = $accepts->witnessType ?? $defaultValueType; + $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $givenType); return [ RuleErrorBuilder::message(sprintf( @@ -59,7 +60,7 @@ public function processNode(Node $node, Scope $scope): array $classReflection->getDisplayName(), $node->getName(), $propertyType->describe($verbosityLevel), - $defaultValueType->describe($verbosityLevel), + $givenType->describe($verbosityLevel), )) ->identifier('property.defaultValue') ->acceptsReasonsTip($accepts->reasons) diff --git a/src/Rules/Properties/TypesAssignedToPropertiesRule.php b/src/Rules/Properties/TypesAssignedToPropertiesRule.php index 6e464bdd1d8..09d8ad1bb24 100644 --- a/src/Rules/Properties/TypesAssignedToPropertiesRule.php +++ b/src/Rules/Properties/TypesAssignedToPropertiesRule.php @@ -90,14 +90,15 @@ private function processSingleProperty( $accepts = $this->ruleLevelHelper->accepts($propertyType, $assignedValueType, $scope->isDeclareStrictTypes()); if (!$accepts->result) { $propertyDescription = $this->describePropertyByName($propertyReflection, $propertyReflection->getName()); - $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $assignedValueType); + $givenType = $accepts->witnessType ?? $assignedValueType; + $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $givenType); return [ RuleErrorBuilder::message(sprintf( '%s (%s) does not accept %s.', $propertyDescription, $propertyType->describe($verbosityLevel), - $assignedValueType->describe($verbosityLevel), + $givenType->describe($verbosityLevel), )) ->line($assignedExpr->getStartLine()) ->identifier('assign.propertyType') diff --git a/src/Rules/RuleLevelHelper.php b/src/Rules/RuleLevelHelper.php index 0c926c52cb0..cac6d2f6d7c 100644 --- a/src/Rules/RuleLevelHelper.php +++ b/src/Rules/RuleLevelHelper.php @@ -27,7 +27,6 @@ use PHPStan\Type\TypeTraverser; use PHPStan\Type\TypeUtils; use PHPStan\Type\UnionType; -use PHPStan\Type\VerbosityLevel; use function count; use function sprintf; @@ -248,20 +247,17 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType): /** @api */ public function accepts(Type $acceptingType, Type $acceptedType, bool $strictTypes): RuleLevelHelperAcceptsResult { - $witnessResult = $this->acceptsWitnessType($acceptingType, $acceptedType, $strictTypes); - if ($witnessResult !== null) { - return $witnessResult; - } - - [$acceptedType, $checkForUnion] = $this->transformAcceptedType($acceptingType, $acceptedType); - $acceptingType = $this->transformCommonType($acceptingType); + [$transformedAcceptedType, $checkForUnion] = $this->transformAcceptedType($acceptingType, $acceptedType); + $transformedAcceptingType = $this->transformCommonType($acceptingType); - $accepts = $acceptingType->accepts($acceptedType, $strictTypes); + $accepts = $transformedAcceptingType->accepts($transformedAcceptedType, $strictTypes); + $result = $checkForUnion ? $accepts->yes() : !$accepts->no(); + if (!$result) { + return new RuleLevelHelperAcceptsResult(false, $accepts->reasons); + } - return new RuleLevelHelperAcceptsResult( - $checkForUnion ? $accepts->yes() : !$accepts->no(), - $accepts->reasons, - ); + return $this->acceptsWitnessType($acceptingType, $acceptedType, $strictTypes) + ?? new RuleLevelHelperAcceptsResult(true, $accepts->reasons); } /** @@ -279,22 +275,20 @@ private function acceptsWitnessType(Type $acceptingType, Type $acceptedType, boo return null; } + $checkedWitnessType = $witnessType; if (!$this->checkNullables && !TypeCombinator::containsNull($acceptingType)) { - $witnessType = TypeCombinator::removeNull($witnessType); - if ($witnessType instanceof NeverType) { + $checkedWitnessType = TypeCombinator::removeNull($witnessType); + if ($checkedWitnessType instanceof NeverType) { return null; } } - $accepts = $this->accepts($acceptingType, $witnessType, $strictTypes); + $accepts = $this->accepts($acceptingType, $checkedWitnessType, $strictTypes); if ($accepts->result) { return null; } - return new RuleLevelHelperAcceptsResult(false, [ - sprintf('Type %s is %s on some paths.', $acceptedType->describe(VerbosityLevel::typeOnly()), $witnessType->describe(VerbosityLevel::value())), - ...$accepts->reasons, - ]); + return new RuleLevelHelperAcceptsResult(false, $accepts->reasons, $witnessType); } /** diff --git a/src/Rules/RuleLevelHelperAcceptsResult.php b/src/Rules/RuleLevelHelperAcceptsResult.php index 3151ce18740..6b2592aa867 100644 --- a/src/Rules/RuleLevelHelperAcceptsResult.php +++ b/src/Rules/RuleLevelHelperAcceptsResult.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules; use PHPStan\Turbo\ReferencedByTurboExtension; +use PHPStan\Type\Type; use function array_merge; /** @@ -14,10 +15,12 @@ final class RuleLevelHelperAcceptsResult /** * @param list $reasons + * @param Type|null $witnessType Set when the accepted type is a mixed accepted on its own, but its witness type, what some path is known to produce, is not. Rules report it as the given type. */ public function __construct( public readonly bool $result, public readonly array $reasons, + public readonly ?Type $witnessType = null, ) { } @@ -27,6 +30,7 @@ public function and(self $other): self return new self( $this->result && $other->result, array_merge($this->reasons, $other->reasons), + $this->witnessType ?? $other->witnessType, ); } @@ -40,7 +44,7 @@ public function decorateReasons(callable $cb): self $reasons[] = $cb($reason); } - return new self($this->result, $reasons); + return new self($this->result, $reasons, $this->witnessType); } } diff --git a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php index 04ffce774b0..65a84e9d0d0 100644 --- a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php +++ b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php @@ -378,7 +378,7 @@ public function testBug11649(): void // crash $errors = $this->runAnalyse(__DIR__ . '/data/bug-11649.php'); $this->assertCount(1, $errors); - $this->assertSame('Anonymous function should return string but returns mixed.', $errors[0]->getMessage()); + $this->assertSame('Anonymous function should return string but returns string|null.', $errors[0]->getMessage()); $this->assertSame(27, $errors[0]->getLine()); } diff --git a/tests/PHPStan/Levels/LevelsIntegrationTest.php b/tests/PHPStan/Levels/LevelsIntegrationTest.php index 885dd5d5588..8e79d1a543e 100644 --- a/tests/PHPStan/Levels/LevelsIntegrationTest.php +++ b/tests/PHPStan/Levels/LevelsIntegrationTest.php @@ -45,6 +45,7 @@ public static function dataTopics(): array ['missingTypes'], ['arrayOffsetAccess'], ['arraySum'], + ['mixedWitness'], ]; if (PHP_VERSION_ID >= 80300) { $topics[] = ['constantAccesses83']; diff --git a/tests/PHPStan/Levels/data/mixedWitness-10-missing.json b/tests/PHPStan/Levels/data/mixedWitness-10-missing.json new file mode 100644 index 00000000000..9320be2ebf2 --- /dev/null +++ b/tests/PHPStan/Levels/data/mixedWitness-10-missing.json @@ -0,0 +1,42 @@ +[ + { + "message": "Method Levels\\MixedWitness\\Foo::returnExplicit() should return string but returns int.", + "line": 12, + "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnImplicit() should return string but returns int.", + "line": 17, + "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnImplicitOrUnion() should return string but returns int|null.", + "line": 27, + "ignorable": true + }, + { + "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, int given.", + "line": 32, + "ignorable": true + }, + { + "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, int given.", + "line": 37, + "ignorable": true + }, + { + "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept int.", + "line": 42, + "ignorable": true + }, + { + "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept int.", + "line": 47, + "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNull() should return string but returns null.", + "line": 22, + "ignorable": true + } +] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness-10.json b/tests/PHPStan/Levels/data/mixedWitness-10.json new file mode 100644 index 00000000000..48d719c553e --- /dev/null +++ b/tests/PHPStan/Levels/data/mixedWitness-10.json @@ -0,0 +1,22 @@ +[ + { + "message": "Method Levels\\MixedWitness\\Foo::returnImplicit() should return string but returns mixed.", + "line": 17, + "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnImplicitOrUnion() should return string but returns mixed.", + "line": 27, + "ignorable": true + }, + { + "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, mixed given.", + "line": 37, + "ignorable": true + }, + { + "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept mixed.", + "line": 47, + "ignorable": true + } +] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness-6.json b/tests/PHPStan/Levels/data/mixedWitness-6.json new file mode 100644 index 00000000000..ef304cb4ea1 --- /dev/null +++ b/tests/PHPStan/Levels/data/mixedWitness-6.json @@ -0,0 +1,22 @@ +[ + { + "message": "Method Levels\\MixedWitness\\Foo::returnImplicit() has parameter $implicit with no type specified.", + "line": 15, + "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnImplicitOrUnion() has parameter $implicit with no type specified.", + "line": 25, + "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::argumentImplicit() has parameter $implicit with no type specified.", + "line": 35, + "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::propertyImplicit() has parameter $implicit with no type specified.", + "line": 45, + "ignorable": true + } +] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness-7.json b/tests/PHPStan/Levels/data/mixedWitness-7.json new file mode 100644 index 00000000000..7ed9c828c78 --- /dev/null +++ b/tests/PHPStan/Levels/data/mixedWitness-7.json @@ -0,0 +1,37 @@ +[ + { + "message": "Method Levels\\MixedWitness\\Foo::returnExplicit() should return string but returns int.", + "line": 12, + "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnImplicit() should return string but returns int.", + "line": 17, + "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnImplicitOrUnion() should return string but returns int|null.", + "line": 27, + "ignorable": true + }, + { + "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, int given.", + "line": 32, + "ignorable": true + }, + { + "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, int given.", + "line": 37, + "ignorable": true + }, + { + "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept int.", + "line": 42, + "ignorable": true + }, + { + "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept int.", + "line": 47, + "ignorable": true + } +] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness-8.json b/tests/PHPStan/Levels/data/mixedWitness-8.json new file mode 100644 index 00000000000..9a50c116f45 --- /dev/null +++ b/tests/PHPStan/Levels/data/mixedWitness-8.json @@ -0,0 +1,7 @@ +[ + { + "message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNull() should return string but returns null.", + "line": 22, + "ignorable": true + } +] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness-9-missing.json b/tests/PHPStan/Levels/data/mixedWitness-9-missing.json new file mode 100644 index 00000000000..b9bf39179ec --- /dev/null +++ b/tests/PHPStan/Levels/data/mixedWitness-9-missing.json @@ -0,0 +1,22 @@ +[ + { + "message": "Method Levels\\MixedWitness\\Foo::returnExplicit() should return string but returns int.", + "line": 12, + "ignorable": true + }, + { + "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, int given.", + "line": 32, + "ignorable": true + }, + { + "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept int.", + "line": 42, + "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNull() should return string but returns null.", + "line": 22, + "ignorable": true + } +] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness-9.json b/tests/PHPStan/Levels/data/mixedWitness-9.json new file mode 100644 index 00000000000..a4ba7ccf8ac --- /dev/null +++ b/tests/PHPStan/Levels/data/mixedWitness-9.json @@ -0,0 +1,27 @@ +[ + { + "message": "Method Levels\\MixedWitness\\Foo::returnExplicit() should return string but returns mixed.", + "line": 12, + "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNull() should return string but returns mixed.", + "line": 22, + "ignorable": true + }, + { + "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, mixed given.", + "line": 32, + "ignorable": true + }, + { + "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept mixed.", + "line": 42, + "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::accepted() should return string but returns mixed.", + "line": 52, + "ignorable": true + } +] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness.php b/tests/PHPStan/Levels/data/mixedWitness.php new file mode 100644 index 00000000000..5a738502b69 --- /dev/null +++ b/tests/PHPStan/Levels/data/mixedWitness.php @@ -0,0 +1,59 @@ +doString($c ? $explicit : 1); + } + + public function argumentImplicit(bool $c, $implicit): void + { + $this->doString($c ? $implicit : 1); + } + + public function propertyExplicit(bool $c, mixed $explicit): void + { + $this->string = $c ? $explicit : 1; + } + + public function propertyImplicit(bool $c, $implicit): void + { + $this->string = $c ? $implicit : 1; + } + + public function accepted(bool $c, mixed $explicit): string + { + return $c ? $explicit : 'foo'; + } + + public function doString(string $s): void + { + } + +} diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index a043b865ba6..0fe152bfb29 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -3952,9 +3952,8 @@ public function testBug11281(): void $this->checkMixedWitnessTypes = true; $this->analyse([__DIR__ . '/data/bug-11281.php'], [ [ - 'Parameter #1 $value of function Bug11281\\world expects int, mixed given.', + 'Parameter #1 $value of function Bug11281\\world expects int, string given.', 8, - "Type mixed is 'a string' on some paths.", ], ]); } diff --git a/turbo-ext/src/RuleLevelHelper.cpp b/turbo-ext/src/RuleLevelHelper.cpp index 165022f9ecc..1e3125189c1 100644 --- a/turbo-ext/src/RuleLevelHelper.cpp +++ b/turbo-ext/src/RuleLevelHelper.cpp @@ -79,19 +79,19 @@ class RuleLevelHelper zval *witnessSlot = OBJ_PROP_NUM(Z_OBJ_P(acceptedType), ptdecl::MixedType::slot::witnessType); if (Z_TYPE_P(witnessSlot) != IS_OBJECT) return zv::Val(); - zv::Val witness = zv::Val::copyOf(zv::Ref(witnessSlot)); + zv::Val checkedWitness = zv::Val::copyOf(zv::Ref(witnessSlot)); if (!flag(slots::checkNullables)) { bool containsNull; if (UNEXPECTED(!pt_type_combinator_contains_null(acceptingType, containsNull))) return zv::Val(); if (!containsNull) { - witness = pt_type_combinator_remove_null(witness.raw()); - if (UNEXPECTED(witness.isUndef())) return zv::Val(); - if (instanceof_function(Z_OBJCE_P(witness.raw()), pt_ce_never_type)) return zv::Val(); + checkedWitness = pt_type_combinator_remove_null(checkedWitness.raw()); + if (UNEXPECTED(checkedWitness.isUndef())) return zv::Val(); + if (instanceof_function(Z_OBJCE_P(checkedWitness.raw()), pt_ce_never_type)) return zv::Val(); } } - zv::Val accepts = this->accepts(acceptingType, witness.raw(), strictTypes); + zv::Val accepts = this->accepts(acceptingType, checkedWitness.raw(), strictTypes); if (UNEXPECTED(accepts.isUndef())) return zv::Val(); zval rv; zval *result = zend_read_property(Z_OBJCE_P(accepts.raw()), Z_OBJ_P(accepts.raw()), "result", sizeof("result") - 1, 0, &rv); @@ -102,32 +102,12 @@ class RuleLevelHelper if (UNEXPECTED(EG(exception))) return zv::Val(); ZVAL_DEREF(reasons); - /* sprintf('Type %s is %s on some paths.', $acceptedType->describe(typeOnly), $witnessType->describe(value)) */ - zv::Val typeOnly = pt_type_verbosity_level(PT_VERBOSITY_LEVEL_TYPE_ONLY); - if (UNEXPECTED(typeOnly.isUndef())) return zv::Val(); - zv::Val value = pt_type_verbosity_level(PT_VERBOSITY_LEVEL_VALUE); - if (UNEXPECTED(value.isUndef())) return zv::Val(); - zv::Val acceptedDescription = pt_type_call(Z_OBJ_P(acceptedType), PT_LC("describe"), 1, typeOnly.raw()); - if (UNEXPECTED(acceptedDescription.isUndef())) return zv::Val(); - zv::Val witnessDescription = pt_type_call(Z_OBJ_P(witness.raw()), PT_LC("describe"), 1, value.raw()); - if (UNEXPECTED(witnessDescription.isUndef())) return zv::Val(); - if (UNEXPECTED(Z_TYPE_P(acceptedDescription.raw()) != IS_STRING || Z_TYPE_P(witnessDescription.raw()) != IS_STRING || Z_TYPE_P(reasons) != IS_ARRAY)) { - pt_throw_should_not_happen(); - return zv::Val(); - } - zend_string *message = zend_strpprintf(0, "Type %s is %s on some paths.", Z_STRVAL_P(acceptedDescription.raw()), Z_STRVAL_P(witnessDescription.raw())); - - zval resultArgs[2]; + /* new RuleLevelHelperAcceptsResult(false, $accepts->reasons, $witnessType) */ + zval resultArgs[3]; ZVAL_FALSE(&resultArgs[0]); - array_init_size(&resultArgs[1], zend_hash_num_elements(Z_ARRVAL_P(reasons)) + 1); - add_next_index_str(&resultArgs[1], message); - zval *reason; - ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(reasons), reason) { - Z_TRY_ADDREF_P(reason); - add_next_index_zval(&resultArgs[1], reason); - } ZEND_HASH_FOREACH_END(); - zv::Val built = pt_type_new(PT_CLASS_RULE_LEVEL_HELPER_ACCEPTS_RESULT, 2, resultArgs); - zval_ptr_dtor(&resultArgs[1]); + ZVAL_COPY_VALUE(&resultArgs[1], reasons); + ZVAL_COPY_VALUE(&resultArgs[2], witnessSlot); + zv::Val built = pt_type_new(PT_CLASS_RULE_LEVEL_HELPER_ACCEPTS_RESULT, 3, resultArgs); if (UNEXPECTED(built.isUndef())) return zv::Val(); decided = true; return built; @@ -136,12 +116,7 @@ class RuleLevelHelper /* Mirrors accepts(). */ zv::Val accepts(zval *acceptingType, zval *acceptedType, bool strictTypes) const { - bool witnessDecided; - zv::Val witnessResult = acceptsWitnessType(acceptingType, acceptedType, strictTypes, witnessDecided); - if (witnessDecided) return witnessResult; - if (UNEXPECTED(EG(exception))) return zv::Val(); - - /* [$acceptedType, $checkForUnion] = $this->transformAcceptedType($acceptingType, $acceptedType); */ + /* [$transformedAcceptedType, $checkForUnion] = $this->transformAcceptedType($acceptingType, $acceptedType); */ bool checkForUnion; zv::Val transformedAccepted = transformAcceptedType(acceptingType, acceptedType, checkForUnion); if (UNEXPECTED(transformedAccepted.isUndef())) return zv::Val(); @@ -158,7 +133,7 @@ class RuleLevelHelper return zv::Val(); } - /* new RuleLevelHelperAcceptsResult($checkForUnion ? $accepts->yes() : !$accepts->no(), $accepts->reasons) */ + /* $result = $checkForUnion ? $accepts->yes() : !$accepts->no(); */ zval resultArgs[2]; zv::Val reasonsHold; if (EXPECTED(Z_OBJCE_P(accepts.raw()) == pt_ce_accepts_result)) { @@ -177,6 +152,15 @@ class RuleLevelHelper reasonsHold = zv::Val::copyOf(zv::Ref(reasons).deref()); ZVAL_COPY_VALUE(&resultArgs[1], reasonsHold.raw()); } + + /* if (!$result) return new RuleLevelHelperAcceptsResult(false, $accepts->reasons); */ + if (Z_TYPE(resultArgs[0]) == IS_TRUE) { + /* return $this->acceptsWitnessType(...) ?? new RuleLevelHelperAcceptsResult(true, $accepts->reasons); */ + bool witnessDecided; + zv::Val witnessResult = acceptsWitnessType(acceptingType, acceptedType, strictTypes, witnessDecided); + if (witnessDecided) return witnessResult; + if (UNEXPECTED(EG(exception))) return zv::Val(); + } return pt_type_new(PT_CLASS_RULE_LEVEL_HELPER_ACCEPTS_RESULT, 2, resultArgs); } From bd7b7da1b6860f0b449a72256055f84a2eb253a4 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Wed, 2 Dec 2026 19:26:07 +0000 Subject: [PATCH 2/2] Cover a nullable witness type of mixed on levels 7 and 8 Amp-Thread-ID: https://ampcode.com/threads/T-01a237b0-3e9f-77ea-bd9b-d9a06b69686b --- .../Levels/data/mixedWitness-10-missing.json | 15 ++++++++++----- tests/PHPStan/Levels/data/mixedWitness-10.json | 6 +++--- tests/PHPStan/Levels/data/mixedWitness-6.json | 6 +++--- tests/PHPStan/Levels/data/mixedWitness-7.json | 10 +++++----- tests/PHPStan/Levels/data/mixedWitness-8.json | 5 +++++ .../Levels/data/mixedWitness-9-missing.json | 9 +++++++-- tests/PHPStan/Levels/data/mixedWitness-9.json | 11 ++++++++--- tests/PHPStan/Levels/data/mixedWitness.php | 5 +++++ 8 files changed, 46 insertions(+), 21 deletions(-) diff --git a/tests/PHPStan/Levels/data/mixedWitness-10-missing.json b/tests/PHPStan/Levels/data/mixedWitness-10-missing.json index 9320be2ebf2..75ceb0abafb 100644 --- a/tests/PHPStan/Levels/data/mixedWitness-10-missing.json +++ b/tests/PHPStan/Levels/data/mixedWitness-10-missing.json @@ -11,32 +11,37 @@ }, { "message": "Method Levels\\MixedWitness\\Foo::returnImplicitOrUnion() should return string but returns int|null.", - "line": 27, + "line": 32, "ignorable": true }, { "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, int given.", - "line": 32, + "line": 37, "ignorable": true }, { "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, int given.", - "line": 37, + "line": 42, "ignorable": true }, { "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept int.", - "line": 42, + "line": 47, "ignorable": true }, { "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept int.", - "line": 47, + "line": 52, "ignorable": true }, { "message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNull() should return string but returns null.", "line": 22, "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNullableString() should return string but returns string|null.", + "line": 27, + "ignorable": true } ] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness-10.json b/tests/PHPStan/Levels/data/mixedWitness-10.json index 48d719c553e..5355b52ae01 100644 --- a/tests/PHPStan/Levels/data/mixedWitness-10.json +++ b/tests/PHPStan/Levels/data/mixedWitness-10.json @@ -6,17 +6,17 @@ }, { "message": "Method Levels\\MixedWitness\\Foo::returnImplicitOrUnion() should return string but returns mixed.", - "line": 27, + "line": 32, "ignorable": true }, { "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, mixed given.", - "line": 37, + "line": 42, "ignorable": true }, { "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept mixed.", - "line": 47, + "line": 52, "ignorable": true } ] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness-6.json b/tests/PHPStan/Levels/data/mixedWitness-6.json index ef304cb4ea1..2a925ff77b3 100644 --- a/tests/PHPStan/Levels/data/mixedWitness-6.json +++ b/tests/PHPStan/Levels/data/mixedWitness-6.json @@ -6,17 +6,17 @@ }, { "message": "Method Levels\\MixedWitness\\Foo::returnImplicitOrUnion() has parameter $implicit with no type specified.", - "line": 25, + "line": 30, "ignorable": true }, { "message": "Method Levels\\MixedWitness\\Foo::argumentImplicit() has parameter $implicit with no type specified.", - "line": 35, + "line": 40, "ignorable": true }, { "message": "Method Levels\\MixedWitness\\Foo::propertyImplicit() has parameter $implicit with no type specified.", - "line": 45, + "line": 50, "ignorable": true } ] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness-7.json b/tests/PHPStan/Levels/data/mixedWitness-7.json index 7ed9c828c78..27953fbf3c4 100644 --- a/tests/PHPStan/Levels/data/mixedWitness-7.json +++ b/tests/PHPStan/Levels/data/mixedWitness-7.json @@ -11,27 +11,27 @@ }, { "message": "Method Levels\\MixedWitness\\Foo::returnImplicitOrUnion() should return string but returns int|null.", - "line": 27, + "line": 32, "ignorable": true }, { "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, int given.", - "line": 32, + "line": 37, "ignorable": true }, { "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, int given.", - "line": 37, + "line": 42, "ignorable": true }, { "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept int.", - "line": 42, + "line": 47, "ignorable": true }, { "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept int.", - "line": 47, + "line": 52, "ignorable": true } ] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness-8.json b/tests/PHPStan/Levels/data/mixedWitness-8.json index 9a50c116f45..0aba0d051f1 100644 --- a/tests/PHPStan/Levels/data/mixedWitness-8.json +++ b/tests/PHPStan/Levels/data/mixedWitness-8.json @@ -3,5 +3,10 @@ "message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNull() should return string but returns null.", "line": 22, "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNullableString() should return string but returns string|null.", + "line": 27, + "ignorable": true } ] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness-9-missing.json b/tests/PHPStan/Levels/data/mixedWitness-9-missing.json index b9bf39179ec..4d669b3dbdc 100644 --- a/tests/PHPStan/Levels/data/mixedWitness-9-missing.json +++ b/tests/PHPStan/Levels/data/mixedWitness-9-missing.json @@ -6,17 +6,22 @@ }, { "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, int given.", - "line": 32, + "line": 37, "ignorable": true }, { "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept int.", - "line": 42, + "line": 47, "ignorable": true }, { "message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNull() should return string but returns null.", "line": 22, "ignorable": true + }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNullableString() should return string but returns string|null.", + "line": 27, + "ignorable": true } ] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness-9.json b/tests/PHPStan/Levels/data/mixedWitness-9.json index a4ba7ccf8ac..4b05baad567 100644 --- a/tests/PHPStan/Levels/data/mixedWitness-9.json +++ b/tests/PHPStan/Levels/data/mixedWitness-9.json @@ -9,19 +9,24 @@ "line": 22, "ignorable": true }, + { + "message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNullableString() should return string but returns mixed.", + "line": 27, + "ignorable": true + }, { "message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, mixed given.", - "line": 32, + "line": 37, "ignorable": true }, { "message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept mixed.", - "line": 42, + "line": 47, "ignorable": true }, { "message": "Method Levels\\MixedWitness\\Foo::accepted() should return string but returns mixed.", - "line": 52, + "line": 57, "ignorable": true } ] \ No newline at end of file diff --git a/tests/PHPStan/Levels/data/mixedWitness.php b/tests/PHPStan/Levels/data/mixedWitness.php index 5a738502b69..80b6842a783 100644 --- a/tests/PHPStan/Levels/data/mixedWitness.php +++ b/tests/PHPStan/Levels/data/mixedWitness.php @@ -22,6 +22,11 @@ public function returnExplicitOrNull(bool $c, mixed $explicit): string return $c ? $explicit : null; } + public function returnExplicitOrNullableString(bool $c, mixed $explicit, ?string $nullableString): string + { + return $c ? $explicit : $nullableString; + } + public function returnImplicitOrUnion(bool $c, bool $d, $implicit): string { return $c ? $implicit : ($d ? 1 : null);