From 3b1c05c5ea6760bd562f66cd78c5a64676823265 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Fri, 13 Nov 2026 23:04:00 +0000 Subject: [PATCH] Narrow a mixed subject of a loose comparison to mixed~null When the values that can loosely equal the other side include objects, a mixed subject stays mixed, but it still cannot be null when null does not equal the value. Narrowing it to mixed~null again marks the offset and the variable as set, which an early return without specified types had stopped doing. Closes https://github.com/phpstan/phpstan/issues/15725 Amp-Thread-ID: https://ampcode.com/threads/T-01a1d722-2ae4-761a-bc23-3426a9af3740 --- .../Helper/IdenticalNarrowingHelper.php | 9 ++++-- .../data/loose-equality-narrowing-php7.php | 2 +- tests/PHPStan/Analyser/nsrt/bug-15725.php | 32 +++++++++++++++++++ .../nsrt/loose-equality-narrowing.php | 15 +++++---- .../Variables/DefinedVariableRuleTest.php | 20 ++++++++++++ .../Rules/Variables/data/bug-15725.php | 12 +++++++ turbo-ext/src/IdenticalNarrowingHelper.cpp | 15 +++++++-- 7 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 tests/PHPStan/Analyser/nsrt/bug-15725.php create mode 100644 tests/PHPStan/Rules/Variables/data/bug-15725.php diff --git a/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php b/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php index 0daa060b7c6..c26d1c66514 100644 --- a/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php +++ b/src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php @@ -661,14 +661,19 @@ private function createLooselyEqualTypes(MutatingScope $evaluationScope, Expr $s return new SpecifiedTypes([], []); } // a mixed subject stays mixed rather than gain objects of classes like Stringable or GMP, - // which say nothing about the value; enum cases are precise enough to narrow to + // which say nothing about the value; enum cases are precise enough to narrow to. + // It still loses null when null cannot equal the value, so the offset or variable is set if ( $context->true() && $subjectType instanceof MixedType && !$type->isObject()->no() && !TypeCombinator::intersect($type, new ObjectWithoutClassType())->isEnum()->yes() ) { - return new SpecifiedTypes([], []); + $nullType = new NullType(); + if (!$type->isSuperTypeOf($nullType)->no()) { + return new SpecifiedTypes([], []); + } + $type = TypeCombinator::remove($subjectType, $nullType); } return $this->defaultNarrowingHelper->createSubjectTypes($evaluationScope, $subject, $subjectResult, $type, $context); diff --git a/tests/PHPStan/Analyser/data/loose-equality-narrowing-php7.php b/tests/PHPStan/Analyser/data/loose-equality-narrowing-php7.php index b3fed80357d..1063b99ad03 100644 --- a/tests/PHPStan/Analyser/data/loose-equality-narrowing-php7.php +++ b/tests/PHPStan/Analyser/data/loose-equality-narrowing-php7.php @@ -29,7 +29,7 @@ function doFoo(int $int, string $string, float $float, $mixed): void } if ($mixed == 'abc') { - assertType('mixed', $mixed); + assertType('mixed~null', $mixed); } if ($mixed == NAN) { diff --git a/tests/PHPStan/Analyser/nsrt/bug-15725.php b/tests/PHPStan/Analyser/nsrt/bug-15725.php new file mode 100644 index 00000000000..4bb9920efcb --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-15725.php @@ -0,0 +1,32 @@ +|false $input */ +function arrayOrFalse($input): void +{ + if ($input['type'] == 2) { + assertType("non-empty-array&hasOffsetValue('type', mixed~null)", $input); + echo 'match'; + } +} + +/** @param array $input */ +function arrayOffset(array $input): void +{ + if ($input['type'] == 'success') { + assertType("non-empty-array&hasOffsetValue('type', mixed~null)", $input); + echo 'match'; + } +} + +/** @param array $input */ +function nullCanEqual(array $input): void +{ + // null == 0, so the offset might still be missing + if ($input['type'] == 0) { + assertType('array', $input); + } +} diff --git a/tests/PHPStan/Analyser/nsrt/loose-equality-narrowing.php b/tests/PHPStan/Analyser/nsrt/loose-equality-narrowing.php index 1aeefaf412e..d2f8b30d876 100644 --- a/tests/PHPStan/Analyser/nsrt/loose-equality-narrowing.php +++ b/tests/PHPStan/Analyser/nsrt/loose-equality-narrowing.php @@ -28,15 +28,16 @@ class Bar */ public function doFoo(mixed $mixed, int $int, float $float, string $string, bool $bool, $intOrString, $constants, Foo|int $fooOrInt, $scalar, float $floatRange, string $numericString, $intStringOrArray, array $array, Suit $suit): void { - // mixed would only become a union with objects it says nothing about + // mixed would only become a union with objects it says nothing about, + // so it only loses null, which cannot equal the value if ($mixed == 'bar') { - assertType('mixed', $mixed); + assertType('mixed~null', $mixed); } else { assertType("mixed~('bar'|true)", $mixed); } if ($mixed == 5) { - assertType('mixed', $mixed); + assertType('mixed~null', $mixed); } if ($mixed == NAN) { @@ -174,20 +175,20 @@ public function doFoo(mixed $mixed, int $int, float $float, string $string, bool function mixedStaysMixed($t, $nonEmptyMixed, array $array, $stringBoolOrNull, $objectOrString): void { if ($t == 'a') { - assertType('T (function LooseEqualityNarrowing\\mixedStaysMixed(), argument)', $t); + assertType('T of mixed~null (function LooseEqualityNarrowing\\mixedStaysMixed(), argument)', $t); } if ($nonEmptyMixed == 'a') { assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $nonEmptyMixed); } if ($array['x'] == 'a') { - assertType('mixed', $array['x']); + assertType('mixed~null', $array['x']); } if (in_array($array['y'], ['a', 'b'])) { - assertType('mixed', $array['y']); + assertType('mixed~null', $array['y']); } switch ($array['z']) { case 'a': - assertType('mixed', $array['z']); + assertType('mixed~null', $array['z']); break; } diff --git a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php index 99f580819af..01cf1fdc5a2 100644 --- a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php +++ b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php @@ -539,4 +539,24 @@ public function testDeadCatch(): void ]); } + public function testBug15725(): void + { + $this->cliArgumentsVariablesRegistered = true; + $this->checkMaybeUndefinedVariables = true; + $this->analyse([__DIR__ . '/data/bug-15725.php'], [ + [ + 'Variable $level might not be defined.', + 5, + ], + [ + 'Variable $other might not be defined.', + 10, + ], + [ + 'Variable $other might not be defined.', + 11, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Variables/data/bug-15725.php b/tests/PHPStan/Rules/Variables/data/bug-15725.php new file mode 100644 index 00000000000..ec8e2c72328 --- /dev/null +++ b/tests/PHPStan/Rules/Variables/data/bug-15725.php @@ -0,0 +1,12 @@ +