Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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: 6 additions & 3 deletions src/Analyser/ExprHandler/AssignHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1279,15 +1279,18 @@
// The variable can prove an arm was taken even when the arm types overlap:
// the part of an arm's type not producible by the other arm implies that
// arm's condition outcome. With fully disjoint arms both remainders are
// the full arm types.
// the full arm types. Disjointness is checked in both directions because
// isSuperTypeOf() is not symmetric for constant arrays that are not
// sealed - array{'a'} is a supertype of array{'a', 'b'} - and a remainder
// matching values of the other arm would prove the wrong outcome.
$truthyRemainder = TypeCombinator::remove($truthyType, $falseyType);
if ($falseyType->isSuperTypeOf($truthyRemainder)->no()) {
if ($falseyType->isSuperTypeOf($truthyRemainder)->no() && $truthyRemainder->isSuperTypeOf($falseyType)->no()) {

Check warning on line 1287 in src/Analyser/ExprHandler/AssignHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ // sealed - array{'a'} is a supertype of array{'a', 'b'} - and a remainder // matching values of the other arm would prove the wrong outcome. $truthyRemainder = TypeCombinator::remove($truthyType, $falseyType); - if ($falseyType->isSuperTypeOf($truthyRemainder)->no() && $truthyRemainder->isSuperTypeOf($falseyType)->no()) { + if ($falseyType->isSuperTypeOf($truthyRemainder)->no() && !$truthyRemainder->isSuperTypeOf($falseyType)->yes()) { $conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult); $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult); $conditionalExpressions = $this->processTernaryArmValueImpliedTypesAfterAssign($nodeScopeResolver, $truthyScope, $storage, $var->name, $conditionalExpressions, $if, $truthyRemainder, $falseyType, $impurePoints, $assignedExpr, $storedAssignedExprResult);

Check warning on line 1287 in src/Analyser/ExprHandler/AssignHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ // sealed - array{'a'} is a supertype of array{'a', 'b'} - and a remainder // matching values of the other arm would prove the wrong outcome. $truthyRemainder = TypeCombinator::remove($truthyType, $falseyType); - if ($falseyType->isSuperTypeOf($truthyRemainder)->no() && $truthyRemainder->isSuperTypeOf($falseyType)->no()) { + if (!$falseyType->isSuperTypeOf($truthyRemainder)->yes() && $truthyRemainder->isSuperTypeOf($falseyType)->no()) { $conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult); $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult); $conditionalExpressions = $this->processTernaryArmValueImpliedTypesAfterAssign($nodeScopeResolver, $truthyScope, $storage, $var->name, $conditionalExpressions, $if, $truthyRemainder, $falseyType, $impurePoints, $assignedExpr, $storedAssignedExprResult);
$conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult);
$conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult);
$conditionalExpressions = $this->processTernaryArmValueImpliedTypesAfterAssign($nodeScopeResolver, $truthyScope, $storage, $var->name, $conditionalExpressions, $if, $truthyRemainder, $falseyType, $impurePoints, $assignedExpr, $storedAssignedExprResult);
}
$falseyRemainder = TypeCombinator::remove($falseyType, $truthyType);
if ($truthyType->isSuperTypeOf($falseyRemainder)->no()) {
if ($truthyType->isSuperTypeOf($falseyRemainder)->no() && $falseyRemainder->isSuperTypeOf($truthyType)->no()) {

Check warning on line 1293 in src/Analyser/ExprHandler/AssignHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $conditionalExpressions = $this->processTernaryArmValueImpliedTypesAfterAssign($nodeScopeResolver, $truthyScope, $storage, $var->name, $conditionalExpressions, $if, $truthyRemainder, $falseyType, $impurePoints, $assignedExpr, $storedAssignedExprResult); } $falseyRemainder = TypeCombinator::remove($falseyType, $truthyType); - if ($truthyType->isSuperTypeOf($falseyRemainder)->no() && $falseyRemainder->isSuperTypeOf($truthyType)->no()) { + if ($truthyType->isSuperTypeOf($falseyRemainder)->no() && !$falseyRemainder->isSuperTypeOf($truthyType)->yes()) { $conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult); $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult); $conditionalExpressions = $this->processTernaryArmValueImpliedTypesAfterAssign($nodeScopeResolver, $falsyScope, $storage, $var->name, $conditionalExpressions, $assignedExpr->else, $falseyRemainder, $truthyType, $impurePoints, $assignedExpr, $storedAssignedExprResult);

Check warning on line 1293 in src/Analyser/ExprHandler/AssignHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $conditionalExpressions = $this->processTernaryArmValueImpliedTypesAfterAssign($nodeScopeResolver, $truthyScope, $storage, $var->name, $conditionalExpressions, $if, $truthyRemainder, $falseyType, $impurePoints, $assignedExpr, $storedAssignedExprResult); } $falseyRemainder = TypeCombinator::remove($falseyType, $truthyType); - if ($truthyType->isSuperTypeOf($falseyRemainder)->no() && $falseyRemainder->isSuperTypeOf($truthyType)->no()) { + if (!$truthyType->isSuperTypeOf($falseyRemainder)->yes() && $falseyRemainder->isSuperTypeOf($truthyType)->no()) { $conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult); $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult); $conditionalExpressions = $this->processTernaryArmValueImpliedTypesAfterAssign($nodeScopeResolver, $falsyScope, $storage, $var->name, $conditionalExpressions, $assignedExpr->else, $falseyRemainder, $truthyType, $impurePoints, $assignedExpr, $storedAssignedExprResult);
$conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult);
$conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult);
$conditionalExpressions = $this->processTernaryArmValueImpliedTypesAfterAssign($nodeScopeResolver, $falsyScope, $storage, $var->name, $conditionalExpressions, $assignedExpr->else, $falseyRemainder, $truthyType, $impurePoints, $assignedExpr, $storedAssignedExprResult);
Expand Down
33 changes: 0 additions & 33 deletions tests/PHPStan/Analyser/Bug15736Test.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
use PHPUnit\Framework\Attributes\DataProvider;

/**
* Runs without bleeding edge, where constant array shapes have unknown sealedness.
* Runs without bleeding edge
*/
class Bug15749Test extends TypeInferenceTestCase
class NoBleedingEdgeTypeInferenceTestCase extends TypeInferenceTestCase
{

public static function dataAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-15736.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-15749.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-15760.php');
}

/**
Expand Down
87 changes: 87 additions & 0 deletions tests/PHPStan/Analyser/data/bug-15760.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php declare(strict_types = 1);

namespace Bug15760;

use function PHPStan\Testing\assertType;

function f(string $unit, bool $blur): string
{
$units = $blur ? ['px', 'rem'] : ['px', 'rem', '%'];

if (!in_array($unit, $units, true)) {
return 'none';
}

assertType('bool', $blur);

return $blur ? 'blur' : 'distance';
}

function reversedArms(string $unit, bool $blur): void
{
$units = $blur ? ['px', 'rem', '%'] : ['px', 'rem'];
if (!in_array($unit, $units, true)) {
return;
}
assertType('bool', $blur);
}

function stringKeys(string $unit, bool $blur): void
{
$units = $blur ? ['a' => 'px'] : ['a' => 'px', 'b' => 'rem'];
if (!in_array($unit, $units, true)) {
return;
}
assertType('bool', $blur);
}

function ifElse(string $unit, bool $blur): void
{
if ($blur) {
$units = ['px', 'rem'];
} else {
$units = ['px', 'rem', '%'];
}
if (!in_array($unit, $units, true)) {
return;
}
assertType('bool', $blur);
}

function matchArms(string $unit, int $i): void
{
$units = match ($i) {
1 => ['px', 'rem'],
default => ['px', 'rem', '%'],
};
if (!in_array($unit, $units, true)) {
return;
}
assertType('int', $i);
}

function disjointArmsStillNarrow(bool $blur): void
{
$units = $blur ? ['px', 'rem'] : ['em'];
if ($units === ['em']) {
assertType('false', $blur);
} else {
assertType('true', $blur);
}
}

function remainderMaybeSupertypeOfOtherArm(bool $blur, string $s): void
{
$units = $blur ? ['px', 'rem'] : ['px', $s, '%'];
if ($units === ['px', 'rem', '%']) {
assertType('bool', $blur);
}
}

function remainderMaybeSupertypeOfOtherArmReversed(bool $blur, string $s): void
{
$units = $blur ? ['px', $s, '%'] : ['px', 'rem'];
if ($units === ['px', 'rem', '%']) {
assertType('bool', $blur);
}
}
13 changes: 12 additions & 1 deletion turbo-ext/src/AssignHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2265,10 +2265,17 @@ class AssignHandler
// The variable can prove an arm was taken even when the arm types overlap:
// the part of an arm's type not producible by the other arm implies that
// arm's condition outcome. With fully disjoint arms both remainders are
// the full arm types.
// the full arm types. Disjointness is checked in both directions because
// isSuperTypeOf() is not symmetric for constant arrays that are not
// sealed - array{'a'} is a supertype of array{'a', 'b'} - and a remainder
// matching values of the other arm would prove the wrong outcome.
AH_VALB(truthyRemainder, pt_type_combinator_remove(truthyType.raw(), falseyType.raw()));
zend_long truthyDisjoint = ahTypeOpTri(falseyType.raw(), PT_OP_IS_SUPER_TYPE_OF, 1, truthyRemainder.raw());
AH_OKB(truthyDisjoint >= 0);
if (truthyDisjoint == PT_TRI_NO) {
truthyDisjoint = ahTypeOpTri(truthyRemainder.raw(), PT_OP_IS_SUPER_TYPE_OF, 1, falseyType.raw());
AH_OKB(truthyDisjoint >= 0);
}
if (truthyDisjoint == PT_TRI_NO) {
AH_OKB(processSureTypesForConditionalExpressionsAfterAssign(nsr, condScope.raw(), storage, varName, conditionalExpressions, truthySpecifiedTypes.raw(), truthyRemainder.raw(), impurePoints, assignedExpr, stored));
AH_OKB(processSureNotTypesForConditionalExpressionsAfterAssign(nsr, condScope.raw(), storage, varName, conditionalExpressions, truthySpecifiedTypes.raw(), truthyRemainder.raw(), impurePoints, assignedExpr, stored));
Expand All @@ -2277,6 +2284,10 @@ class AssignHandler
AH_VALB(falseyRemainder, pt_type_combinator_remove(falseyType.raw(), truthyType.raw()));
zend_long falseyDisjoint = ahTypeOpTri(truthyType.raw(), PT_OP_IS_SUPER_TYPE_OF, 1, falseyRemainder.raw());
AH_OKB(falseyDisjoint >= 0);
if (falseyDisjoint == PT_TRI_NO) {
falseyDisjoint = ahTypeOpTri(falseyRemainder.raw(), PT_OP_IS_SUPER_TYPE_OF, 1, truthyType.raw());
AH_OKB(falseyDisjoint >= 0);
}
if (falseyDisjoint == PT_TRI_NO) {
AH_OKB(processSureTypesForConditionalExpressionsAfterAssign(nsr, condScope.raw(), storage, varName, conditionalExpressions, falseySpecifiedTypes.raw(), falseyRemainder.raw(), impurePoints, assignedExpr, stored));
AH_OKB(processSureNotTypesForConditionalExpressionsAfterAssign(nsr, condScope.raw(), storage, varName, conditionalExpressions, falseySpecifiedTypes.raw(), falseyRemainder.raw(), impurePoints, assignedExpr, stored));
Expand Down
Loading