Skip to content

Commit 457c32f

Browse files
phpstan-botgithub-actions[bot]VincentLanglet
committed
Fix phpstan/phpstan#13029: False match.unhandled on array with multiple booleans (phpstan#5107)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Vincent Langlet <vincentlanglet@hotmail.fr>
1 parent 402aaf3 commit 457c32f

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/Type/TypeCombinator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ public static function remove(Type $fromType, Type $typeToRemove): Type
9090
$fromFiniteTypes = $fromType->getFiniteTypes();
9191
if (count($fromFiniteTypes) > 0) {
9292
$finiteTypesToRemove = $typeToRemove->getFiniteTypes();
93-
if (count($finiteTypesToRemove) === 1) {
93+
if (count($finiteTypesToRemove) > 0) {
9494
$result = [];
9595
foreach ($fromFiniteTypes as $finiteType) {
96-
if ($finiteType->equals($finiteTypesToRemove[0])) {
97-
continue;
96+
foreach ($finiteTypesToRemove as $finiteTypeToRemove) {
97+
if ($finiteType->equals($finiteTypeToRemove)) {
98+
continue 2;
99+
}
98100
}
99101

100102
$result[] = $finiteType;

tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,12 @@ public function testBug9534(): void
446446
]);
447447
}
448448

449+
#[RequiresPhp('>= 8.0')]
450+
public function testBug13029(): void
451+
{
452+
$this->analyse([__DIR__ . '/data/bug-13029.php'], []);
453+
}
454+
449455
#[RequiresPhp('>= 8.0')]
450456
public function testBug11310(): void
451457
{
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug13029;
6+
7+
function foo(): void
8+
{
9+
/** @var bool **/
10+
$bool1 = true;
11+
/** @var bool **/
12+
$bool2 = false;
13+
14+
$x = match([$bool1, $bool2]) {
15+
[true, false], [true, true] => 1,
16+
[false, false] => 0,
17+
[false, true] => -1,
18+
};
19+
20+
/** @var int<0, 2> */
21+
$int1 = 1;
22+
/** @var int<0, 2> */
23+
$int2 = 2;
24+
25+
$y = match([$int1, $int2]) {
26+
[0, 0], [0, 1], [0, 2] => 1,
27+
[1, 1], [1, 2], [1, 0] => 0,
28+
[2, 1], [2, 0], [2, 2] => -1,
29+
};
30+
31+
/** @var 0|1 **/
32+
$int1 = 1;
33+
/** @var 0|1 **/
34+
$int2 = 0;
35+
36+
$z = match([$int1, $int2]) {
37+
[1, 0], [1, 1] => 1,
38+
[0, 0] => 0,
39+
[0, 1] => -1,
40+
};
41+
}

0 commit comments

Comments
 (0)