Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support narrowing down via in_array with enum cases
- Loading branch information
Showing
6 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php declare(strict_types = 1); // lint >= 8.1 | ||
|
||
namespace Bug7176; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
enum Suit | ||
{ | ||
case Hearts; | ||
case Diamonds; | ||
case Clubs; | ||
case Spades; | ||
} | ||
|
||
function test(Suit $x): string { | ||
if ($x === Suit::Clubs) { | ||
assertType('Bug7176\Suit::Clubs', $x); | ||
} | ||
|
||
if (in_array($x, [Suit::Spades], true)) { | ||
assertType('Bug7176\Suit::Spades', $x); | ||
return 'DOES NOT WORK'; | ||
} | ||
assertType('Bug7176\Suit::Clubs|Bug7176\Suit::Diamonds|Bug7176\Suit::Hearts', $x); | ||
|
||
return match ($x) { | ||
Suit::Hearts => 'a', | ||
Suit::Diamonds => 'b', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php declare(strict_types = 1); // lint >= 8.1 | ||
|
||
namespace Bug7176; | ||
|
||
enum Suit | ||
{ | ||
case Hearts; | ||
case Diamonds; | ||
case Clubs; | ||
case Spades; | ||
} | ||
|
||
function test(Suit $x): string { | ||
if ($x === Suit::Clubs) { | ||
return 'WORKS'; | ||
} | ||
// Suit::Clubs is correctly eliminated from possible values | ||
|
||
if (in_array($x, [Suit::Spades], true)) { | ||
return 'DOES NOT WORK'; | ||
} | ||
// Suit::Spades is not eliminated from possible values | ||
|
||
return match ($x) { // no error is expected here | ||
Suit::Hearts => 'a', | ||
Suit::Diamonds => 'b', | ||
}; | ||
} |