Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15725.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace Bug15725;

use function PHPStan\Testing\assertType;

/** @param array<string, mixed>|false $input */
function arrayOrFalse($input): void
{
if ($input['type'] == 2) {
assertType("non-empty-array<string, mixed>&hasOffsetValue('type', mixed~null)", $input);
echo 'match';
}
}

/** @param array<string, mixed> $input */
function arrayOffset(array $input): void
{
if ($input['type'] == 'success') {
assertType("non-empty-array<string, mixed>&hasOffsetValue('type', mixed~null)", $input);
echo 'match';
}
}

/** @param array<string, mixed> $input */
function nullCanEqual(array $input): void
{
// null == 0, so the offset might still be missing
if ($input['type'] == 0) {
assertType('array<string, mixed>', $input);
}
}
15 changes: 8 additions & 7 deletions tests/PHPStan/Analyser/nsrt/loose-equality-narrowing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}

}
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-15725.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

namespace Bug15725;

if ($level == 'success') {
echo $level;
}

// null == '', so the variable might still be undefined
if ($other == '') {
echo $other;
}
15 changes: 13 additions & 2 deletions turbo-ext/src/IdenticalNarrowingHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,8 @@ class IdenticalNarrowingHelper
if (UNEXPECTED(!ctxFalse(context, isFalse))) return zv::Val();
if (isFalse && instanceof_function(Z_OBJCE_P(type.raw()), pt_ce_never_type)) return pt_specified_types_new();
// 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 (isTrue && Z_TYPE_P(subjectType) == IS_OBJECT && instanceof_function(Z_OBJCE_P(subjectType), pt_ce_mixed_type)) {
zend_long isObject = trinaryByName(type.raw(), "isObject", PT_LC("isobject"));
if (UNEXPECTED(isObject < 0)) return zv::Val();
Expand All @@ -690,7 +691,17 @@ class IdenticalNarrowingHelper
if (UNEXPECTED(objects.isUndef())) return zv::Val();
zend_long isEnum = trinaryByName(objects.raw(), "isEnum", PT_LC("isenum"));
if (UNEXPECTED(isEnum < 0)) return zv::Val();
if (isEnum != PT_TRI_YES) return pt_specified_types_new();
if (isEnum != PT_TRI_YES) {
zv::Val nullType = newNullType();
if (UNEXPECTED(nullType.isUndef())) return zv::Val();
zv::Val coversNull = pt_type_op(Z_OBJ_P(type.raw()), PT_OP_IS_SUPER_TYPE_OF, 1, nullType.raw());
if (UNEXPECTED(coversNull.isUndef())) return zv::Val();
zend_long coversNullValue = pt_type_result_trinary(coversNull.raw());
if (UNEXPECTED(coversNullValue < 0)) return zv::Val();
if (coversNullValue != PT_TRI_NO) return pt_specified_types_new();
type = pt_type_combinator_remove(subjectType, nullType.raw());
if (UNEXPECTED(type.isUndef())) return zv::Val();
}
}
}

Expand Down
Loading