Skip to content

Commit 1a53b21

Browse files
committed
Fixed YodaComparisonSniff for negative or plus-prepended expressions
1 parent b1a4724 commit 1a53b21

File tree

4 files changed

+6
-2
lines changed

4 files changed

+6
-2
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/YodaComparisonSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private function getDynamismForTokens(array $sideTokens)
222222
{
223223
$dynamism = $this->getTokenDynamism();
224224
$sideTokens = array_values(array_filter($sideTokens, function (array $token) {
225-
return !in_array($token['code'], [T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_NS_SEPARATOR], true);
225+
return !in_array($token['code'], [T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_NS_SEPARATOR, T_PLUS, T_MINUS], true);
226226
}));
227227

228228
if (count($sideTokens) > 0) {

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 dataIncorrectFile()
1515
{
1616
$lineNumbers = [];
17-
foreach (range(3, 20) as $lineNumber) {
17+
foreach (range(3, 22) as $lineNumber) {
1818
$lineNumbers[$lineNumber] = [$lineNumber];
1919
}
2020

tests/Sniffs/ControlStructures/data/allYodaComparisons.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
Foo::BAR === $foo;
1515
Foo::BAR === $foo + 2;
1616
Foo::BAR === $this->foo();
17+
-1 === $foo;
18+
+1 === $foo;
1719
(BAR === foo() || (
1820
['test'] === Foo::BAR
1921
)) ? 123.0 === Foo::BAR

tests/Sniffs/ControlStructures/data/noYodaComparisons.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
$foo === Foo::BAR;
1717
$foo + 2 === Foo::BAR;
1818
$this->foo() === Foo::BAR;
19+
$foo === -1;
20+
$foo === +1;
1921
count($cartItem->getReservations()) !== $neededReservationsAmount;
2022
$optionalPartOpeningBracePosition !== strlen($part) - 1;
2123
$optionalPartOpeningBracePosition !== \Nette\Utils\Strings::length($part) - 1;

0 commit comments

Comments
 (0)