Skip to content

Commit bd9e672

Browse files
committed
YodaComparisonSniff fix should not reformat code
1 parent d33612c commit bd9e672

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/YodaComparisonSniff.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $comparisonTokenPointe
114114
if (count($leftSideTokens) > 0 & count($rightSideTokens) > 0) {
115115
$phpcsFile->fixer->beginChangeset();
116116
$this->write($phpcsFile, $leftSideTokens, $rightSideTokens);
117-
$phpcsFile->fixer->addContent(key($leftSideTokens), ' ');
118117
$this->write($phpcsFile, $rightSideTokens, $leftSideTokens);
119118
$phpcsFile->fixer->endChangeset();
120119
}
@@ -138,9 +137,9 @@ private function write(\PHP_CodeSniffer_File $phpcsFile, array $leftSideTokens,
138137
$phpcsFile->fixer->replaceToken($i, '');
139138
}
140139

141-
$phpcsFile->fixer->addContent($firstLeftPointer, trim(implode('', array_map(function (array $token): string {
140+
$phpcsFile->fixer->addContent($firstLeftPointer, implode('', array_map(function (array $token): string {
142141
return $token['content'];
143-
}, $rightSideTokens))));
142+
}, $rightSideTokens)));
144143
}
145144

146145
/**
@@ -297,7 +296,7 @@ private function trimWhitespaceTokens(array $tokens): array
297296
}
298297
}
299298

300-
foreach (array_reverse($tokens) as $pointer => $token) {
299+
foreach (array_reverse($tokens, true) as $pointer => $token) {
301300
if ($token['code'] === T_WHITESPACE) {
302301
unset($tokens[$pointer]);
303302
} else {

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
$this->foo() === Foo::BAR;
1818
$foo === -1;
1919
$foo === +1;
20-
(foo() === BAR|| (
21-
Foo::BAR === ['test'])) ? Foo::BAR === 123.0: $foo === null;
22-
(foo() === BAR|| (
23-
Foo::BAR === array('test'))) ? Foo::BAR === 123.0: $foo === null;
20+
(foo() === BAR || (
21+
Foo::BAR === ['test']
22+
)) ? Foo::BAR === 123.0
23+
: $foo === null;
24+
(foo() === BAR || (
25+
Foo::BAR === array('test')
26+
)) ? Foo::BAR === 123.0
27+
: $foo === null;
2428

2529
if (
2630
$foo($bar) === [Foo::BAR, Foo::BAZ] && (
@@ -48,11 +52,11 @@
4852
(int) $bar === FOO;
4953

5054
$x = [$username === self::ADMIN_EMAIL ? self::ROLE_ADMIN : self::ROLE_CUSTOMER];
51-
$x = [$username === self::ADMIN_EMAIL? self::ROLE_ADMIN : self::ROLE_CUSTOMER];
55+
$x = [$username === self::ADMIN_EMAIL ? self::ROLE_ADMIN : self::ROLE_CUSTOMER];
56+
$x = array($username === self::ADMIN_EMAIL ? self::ROLE_ADMIN : self::ROLE_CUSTOMER);
5257
$x = array($username === self::ADMIN_EMAIL ? self::ROLE_ADMIN : self::ROLE_CUSTOMER);
53-
$x = array($username === self::ADMIN_EMAIL? self::ROLE_ADMIN : self::ROLE_CUSTOMER);
5458

55-
$x = array($username === array()? true : false);
56-
$x = array($username === []? true : false);
57-
$x = [$username === array()? true : false];
58-
$x = [$username === []? true : false];
59+
$x = array($username === array() ? true : false);
60+
$x = array($username === [] ? true : false);
61+
$x = [$username === array() ? true : false];
62+
$x = [$username === [] ? true : false];

0 commit comments

Comments
 (0)