Skip to content

Commit 2a2b12e

Browse files
committed
SlevomatCodingStandard.ControlStructures.RequireYodaComparisonSniff/DisallowYodaComparison: Fixed false positives with match
1 parent f319c1d commit 2a2b12e

11 files changed

+73
-8
lines changed

SlevomatCodingStandard/Helpers/YodaHelper.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use const T_LOGICAL_AND;
4444
use const T_LOGICAL_OR;
4545
use const T_LOGICAL_XOR;
46+
use const T_MATCH_ARROW;
4647
use const T_MINUS;
4748
use const T_NS_SEPARATOR;
4849
use const T_NULL;
@@ -349,9 +350,11 @@ private static function getStopTokenCodes(): array
349350
T_RETURN => true,
350351
T_COMMA => true,
351352
T_CLOSE_CURLY_BRACKET => true,
353+
T_MATCH_ARROW => true,
352354
];
353355

354356
$stopTokenCodes += array_fill_keys(array_keys(Tokens::$assignmentTokens), true);
357+
$stopTokenCodes += array_fill_keys(array_keys(Tokens::$commentTokens), true);
355358
}
356359

357360
return $stopTokenCodes;

tests/Sniffs/ControlStructures/DisallowYodaComparisonSniffTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ public function testNoErrors(): void
1717
public function testErrors(): void
1818
{
1919
$report = self::checkFile(__DIR__ . '/data/disallowYodaComparisonErrors.php');
20+
2021
foreach (range(3, 37) as $lineNumber) {
2122
self::assertSniffError($report, $lineNumber, DisallowYodaComparisonSniff::CODE_DISALLOWED_YODA_COMPARISON);
2223
}
24+
25+
self::assertSniffError($report, 41, DisallowYodaComparisonSniff::CODE_DISALLOWED_YODA_COMPARISON);
2326
}
2427

2528
public function testFixable(): void

tests/Sniffs/ControlStructures/RequireYodaComparisonSniffTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public function testErrors(): void
2020
foreach (range(3, 37) as $lineNumber) {
2121
self::assertSniffError($report, $lineNumber, RequireYodaComparisonSniff::CODE_REQUIRED_YODA_COMPARISON);
2222
}
23+
24+
self::assertSniffError($report, 41, RequireYodaComparisonSniff::CODE_REQUIRED_YODA_COMPARISON);
2325
}
2426

2527
public function testFixable(): void

tests/Sniffs/ControlStructures/data/disallowYodaComparisonErrors.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php // lint >= 8.0
22

33
123 === $foo;
44
true === $foo;
@@ -35,3 +35,10 @@
3535
$param === A::TYPE_A or A::TYPE_B === $param;
3636
A::TYPE_A === $param xor $param === A::TYPE_B;
3737
$x = [$a, $b, $c] === $username;
38+
39+
function ($condition, $actual) {
40+
return match ($condition) {
41+
'anything' => 1 === $actual,
42+
default => false,
43+
};
44+
};

tests/Sniffs/ControlStructures/data/disallowYodaComparisonNoErrors.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php // lint >= 8.0
22

33
$foo === $bar;
44
$foo === 123;
@@ -72,3 +72,10 @@
7272
$param === A::TYPE_A and $param === A::TYPE_B;
7373
$param === A::TYPE_A or $param === A::TYPE_B;
7474
$param === A::TYPE_A xor $param === A::TYPE_B;
75+
76+
function ($condition, $actual) {
77+
return match ($condition) {
78+
'anything' => $actual === 1,
79+
default => false,
80+
};
81+
};

tests/Sniffs/ControlStructures/data/fixableDisallowYodaComparisons.fixed.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php // lint >= 8.0
22

33
$foo === 123;
44
$foo === true;
@@ -69,3 +69,10 @@
6969
if (null === $env = $parameters['env']) {
7070
// ...
7171
}
72+
73+
function ($condition, $actual) {
74+
return match ($condition) {
75+
'anything' => $actual === 1,
76+
default => false,
77+
};
78+
};

tests/Sniffs/ControlStructures/data/fixableDisallowYodaComparisons.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php // lint >= 8.0
22

33
123 === $foo;
44
true === $foo;
@@ -69,3 +69,10 @@
6969
if (null === $env = $parameters['env']) {
7070
// ...
7171
}
72+
73+
function ($condition, $actual) {
74+
return match ($condition) {
75+
'anything' => 1 === $actual,
76+
default => false,
77+
};
78+
};

tests/Sniffs/ControlStructures/data/fixableRequireYodaComparisons.fixed.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php // lint >= 8.0
22

33
123 === $foo;
44
true === $foo;
@@ -66,3 +66,10 @@
6666
A::TYPE_A === $param and A::TYPE_B === $param;
6767
A::TYPE_A === $param or A::TYPE_B === $param;
6868
A::TYPE_A === $param xor A::TYPE_B === $param;
69+
70+
function ($condition, $actual) {
71+
return match ($condition) {
72+
'anything' => 1 === $actual,
73+
default => false,
74+
};
75+
};

tests/Sniffs/ControlStructures/data/fixableRequireYodaComparisons.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php // lint >= 8.0
22

33
$foo === 123;
44
$foo === true;
@@ -66,3 +66,10 @@
6666
$param === A::TYPE_A and $param === A::TYPE_B;
6767
A::TYPE_A === $param or $param === A::TYPE_B;
6868
$param === A::TYPE_A xor A::TYPE_B === $param;
69+
70+
function ($condition, $actual) {
71+
return match ($condition) {
72+
'anything' => $actual === 1,
73+
default => false,
74+
};
75+
};

tests/Sniffs/ControlStructures/data/requireYodaComparisonErrors.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php // lint >= 8.0
22

33
$foo === 123;
44
$foo === true;
@@ -35,3 +35,10 @@
3535
A::TYPE_A === $param or $param === A::TYPE_B;
3636
$param === A::TYPE_A xor A::TYPE_B === $param;
3737
$x = $username === [$a, $b, $c];
38+
39+
function ($condition, $actual) {
40+
return match ($condition) {
41+
'anything' => $actual === 1,
42+
default => false,
43+
};
44+
};

0 commit comments

Comments
 (0)