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
2 changes: 2 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3861,9 +3861,11 @@ private function createConditionalExpressions(
&& $theirExpressionTypes[$guardExprString]->getCertainty()->yes()
) {
$guardIsSuperTypeOfTheirExpr = $guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType());
$theirExprIsSuperTypeOfGuard = $theirExpressionTypes[$guardExprString]->getType()->isSuperTypeOf($guardHolder->getType());

if (
$guardIsSuperTypeOfTheirExpr->yes()
|| $theirExprIsSuperTypeOfGuard->yes()
|| (
array_key_exists($exprString, $theirExpressionTypes)
&& $theirExpressionTypes[$exprString]->getCertainty()->yes()
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14807.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug14807;

use function PHPStan\Testing\assertType;

enum Color
{
case Red;
case Blue;
}

class Item
{
public bool $ready = false;
}

function process(Color $color, Item $item): void
{
if ($color !== Color::Red && $item->ready === true) {
assertType('true', $item->ready);
}

// The narrowing from the first `if` must not leak here: reusing the same
// enum left-hand condition does not imply $item->ready is still true.
if ($color !== Color::Red) {
assertType('bool', $item->ready);
}

if ($color !== Color::Red && $item->ready === false) {
assertType('false', $item->ready);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ public function testBug8555(): void
$this->analyse([__DIR__ . '/data/bug-8555.php'], []);
}

public function testBug14807(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-14807.php'], []);
}

public function testInTrait(): void
{
$this->treatPhpDocTypesAsCertain = true;
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-14807.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace BugRule14807;

enum Color
{
case Red;
case Blue;
}

class Item
{
public bool $ready = false;
}

function process(Color $color, Item $item): void
{
if ($color !== Color::Red && $item->ready === true) {
echo 'go';
}

if ($color !== Color::Red && $item->ready === false) {
throw new \RuntimeException('stop');
}
}
Loading