Skip to content

Commit 5b13215

Browse files
committed
LastConditionVisitor - consider last condition in last arm
1 parent 8fd9fca commit 5b13215

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/Parser/LastConditionVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function enterNode(Node $node): ?Node
3131
}
3232

3333
$isLast = $i === $lastArm;
34-
$arm->conds[0]->setAttribute(self::ATTRIBUTE_NAME, $isLast);
34+
$arm->conds[count($arm->conds) - 1]->setAttribute(self::ATTRIBUTE_NAME, $isLast);
3535
}
3636
}
3737

tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,11 @@ public function dataLastMatchArm(): iterable
813813
62,
814814
'Remove remaining cases below this one and this error will disappear too.',
815815
],
816+
[
817+
"Strict comparison using === between 'bbb' and 'bbb' will always evaluate to true.",
818+
79,
819+
'Remove remaining cases below this one and this error will disappear too.',
820+
],
816821
]];
817822
yield [true, [
818823
[
@@ -839,6 +844,14 @@ public function dataLastMatchArm(): iterable
839844
"Strict comparison using === between 'bbb' and 'bbb' will always evaluate to true.",
840845
62,
841846
],
847+
[
848+
"Strict comparison using === between 'bbb' and 'bbb' will always evaluate to true.",
849+
75,
850+
],
851+
[
852+
"Strict comparison using === between 'bbb' and 'bbb' will always evaluate to true.",
853+
79,
854+
],
842855
]];
843856
}
844857

tests/PHPStan/Rules/Comparison/data/strict-comparison-last-match-arm.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,21 @@ public function doIpsum(): void
6464
};
6565
}
6666

67+
public function doMoreConditionsInLastArm(): void
68+
{
69+
$a = 'aaa';
70+
if (rand(0, 1)) {
71+
$a = 'bbb';
72+
}
73+
74+
match (true) {
75+
$a === 'aaa', $a === 'bbb' => 1,
76+
};
77+
78+
match (true) {
79+
$a === 'aaa', $a === 'bbb' => 1,
80+
default => new \Exception(),
81+
};
82+
}
83+
6784
}

0 commit comments

Comments
 (0)