Skip to content

Commit cb6986f

Browse files
committed
Fixed YodaComparisonSniff for logical "and", "or" and "xor"
1 parent 4c41888 commit cb6986f

File tree

6 files changed

+19
-1
lines changed

6 files changed

+19
-1
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/YodaComparisonSniff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ private function getStopTokenCodes(): array
7777
T_OPEN_TAG => true,
7878
T_INLINE_THEN => true,
7979
T_INLINE_ELSE => true,
80+
T_LOGICAL_AND => true,
81+
T_LOGICAL_OR => true,
82+
T_LOGICAL_XOR => true,
8083
T_COALESCE => true,
8184
T_CASE => true,
8285
T_COLON => true,

tests/Sniffs/ControlStructures/YodaComparisonSniffTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testCorrectFile()
1414
public function testIncorrectFile()
1515
{
1616
$resultFile = $this->checkFile(__DIR__ . '/data/allYodaComparisons.php');
17-
foreach (range(3, 33) as $lineNumber) {
17+
foreach (range(3, 36) as $lineNumber) {
1818
$this->assertSniffError($resultFile, $lineNumber, YodaComparisonSniff::CODE_YODA_COMPARISON);
1919
}
2020
}

tests/Sniffs/ControlStructures/data/allYodaComparisons.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@
3131
array(self::ADMIN_EMAIL === $username ? self::ROLE_ADMIN : self::ROLE_CUSTOMER);
3232
[[] === $array];
3333
[array() === $array];
34+
A::TYPE_A === $param and A::TYPE_B === $param;
35+
$param === A::TYPE_A or A::TYPE_B === $param;
36+
A::TYPE_A === $param xor $param === A::TYPE_B;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@
6060
$x = array($username === [] ? true : false);
6161
$x = [$username === array() ? true : false];
6262
$x = [$username === [] ? true : false];
63+
64+
$param === A::TYPE_A and $param === A::TYPE_B;
65+
$param === A::TYPE_A or $param === A::TYPE_B;
66+
$param === A::TYPE_A xor $param === A::TYPE_B;

tests/Sniffs/ControlStructures/data/fixableYodaComparisons.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@
6060
$x = array([] === $username ? true : false);
6161
$x = [array() === $username ? true : false];
6262
$x = [[] === $username ? true : false];
63+
64+
A::TYPE_A === $param and A::TYPE_B === $param;
65+
$param === A::TYPE_A or A::TYPE_B === $param;
66+
A::TYPE_A === $param xor $param === A::TYPE_B;

tests/Sniffs/ControlStructures/data/noYodaComparisons.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,7 @@
6767
array($username === self::ADMIN_EMAIL ? self::ROLE_ADMIN : self::ROLE_CUSTOMER);
6868
[$array === []];
6969
[$array === array()];
70+
71+
$param === A::TYPE_A and $param === A::TYPE_B;
72+
$param === A::TYPE_A or $param === A::TYPE_B;
73+
$param === A::TYPE_A xor $param === A::TYPE_B;

0 commit comments

Comments
 (0)