Skip to content

Commit 55785b0

Browse files
committed
Disallow yoda conditions
1 parent a829bf0 commit 55785b0

File tree

6 files changed

+77
-238
lines changed

6 files changed

+77
-238
lines changed

src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function process(File $phpcsFile, $stackPtr)
4848
$tokens = $phpcsFile->getTokens();
4949
$nextIndex = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
5050

51+
// Check the token after the comparison. If it is one of these then it is not a Yoda condition.
5152
if ($nextIndex === false || in_array(
5253
$tokens[$nextIndex]['code'],
5354
[
@@ -67,22 +68,51 @@ public function process(File $phpcsFile, $stackPtr)
6768
}
6869

6970
$previousIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
70-
if ($previousIndex === false || in_array(
71-
$tokens[$previousIndex]['code'],
72-
[
73-
T_VARIABLE,
74-
T_STRING,
75-
],
76-
true
77-
) === true
78-
) {
71+
72+
if ($previousIndex === false || in_array($tokens[$previousIndex]['code'], [
73+
T_CLOSE_SHORT_ARRAY,
74+
T_CLOSE_PARENTHESIS,
75+
T_TRUE,
76+
T_FALSE,
77+
T_NULL,
78+
T_LNUMBER,
79+
T_DNUMBER,
80+
T_CONSTANT_ENCAPSED_STRING,
81+
], true) === false) {
7982
return;
8083
}
8184

85+
if ($tokens[$previousIndex]['code'] === T_CLOSE_SHORT_ARRAY) {
86+
$previousIndex = $tokens[$previousIndex]['bracket_opener'];
87+
}
88+
$prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, $previousIndex - 1, null, true);
89+
if ($prevIndex === false) {
90+
return;
91+
}
92+
if (in_array($tokens[$prevIndex]['code'], Tokens::$arithmeticTokens, true)) {
93+
return;
94+
}
95+
if ($tokens[$prevIndex]['code'] === T_STRING_CONCAT) {
96+
return;
97+
}
98+
99+
// Is it a parenthesis.
82100
if ($tokens[$previousIndex]['code'] === T_CLOSE_PARENTHESIS) {
83-
$found = $phpcsFile->findPrevious([T_VARIABLE], ($previousIndex - 1), $tokens[$previousIndex]['parenthesis_opener']);
84-
if ($found !== false) {
85-
return;
101+
// Check what exists inside the parenthesis.
102+
$closeParenthesisIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($tokens[$previousIndex]['parenthesis_opener'] - 1), null, true);
103+
104+
if ($closeParenthesisIndex === false || $tokens[$closeParenthesisIndex]['code'] !== T_ARRAY) {
105+
if ($tokens[$closeParenthesisIndex]['code'] === T_STRING) {
106+
return;
107+
}
108+
109+
// If it is not an array check what is inside.
110+
$found = $phpcsFile->findPrevious([T_VARIABLE], ($previousIndex - 1), $tokens[$previousIndex]['parenthesis_opener']);
111+
112+
// If a variable exists it is not Yoda.
113+
if ($found !== false) {
114+
return;
115+
}
86116
}
87117
}
88118

src/Standards/Generic/Sniffs/ControlStructures/EnforceYodaConditionsSniff.php

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,12 @@ if(
114114
===
115115
// comment
116116
true
117-
){}
117+
){}
118+
119+
if(array($key => $val) === $value){}
120+
if(array($key => $val) == $value){}
121+
122+
if([$key => $val] === $value){}
123+
if([$key => $val] == $value){}
124+
125+
$config['checkAuthIn'] !== $event->getName();

src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,32 @@ class DisallowYodaConditionsUnitTest extends AbstractSniffUnitTest
2626
public function getErrorList()
2727
{
2828
return [
29-
7 => 1,
30-
8 => 1,
31-
12 => 1,
32-
13 => 1,
33-
18 => 1,
34-
19 => 1,
35-
24 => 1,
36-
25 => 1,
37-
30 => 1,
38-
31 => 1,
39-
40 => 1,
40-
47 => 1,
41-
48 => 1,
42-
50 => 1,
43-
52 => 1,
44-
57 => 1,
45-
58 => 1,
46-
62 => 1,
47-
68 => 1,
29+
7 => 1,
30+
8 => 1,
31+
12 => 1,
32+
13 => 1,
33+
18 => 1,
34+
19 => 1,
35+
24 => 1,
36+
25 => 1,
37+
30 => 1,
38+
31 => 1,
39+
40 => 1,
40+
47 => 1,
41+
48 => 1,
42+
50 => 1,
43+
52 => 1,
44+
57 => 1,
45+
58 => 1,
46+
62 => 1,
47+
68 => 1,
48+
97 => 3,
49+
98 => 3,
50+
105 => 1,
51+
119 => 1,
52+
120 => 1,
53+
122 => 1,
54+
123 => 1,
4855
];
4956

5057
}//end getErrorList()

src/Standards/Generic/Tests/ControlStructures/EnforceYodaConditionsUnitTest.inc

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/Standards/Generic/Tests/ControlStructures/EnforceYodaConditionsUnitTest.php

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)