Skip to content

Commit

Permalink
Disallow yoda conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
gmponos committed Nov 13, 2018
1 parent a829bf0 commit 55785b0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function process(File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();
$nextIndex = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);

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

$previousIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if ($previousIndex === false || in_array(
$tokens[$previousIndex]['code'],
[
T_VARIABLE,
T_STRING,
],
true
) === true
) {

if ($previousIndex === false || in_array($tokens[$previousIndex]['code'], [
T_CLOSE_SHORT_ARRAY,
T_CLOSE_PARENTHESIS,
T_TRUE,
T_FALSE,
T_NULL,
T_LNUMBER,
T_DNUMBER,
T_CONSTANT_ENCAPSED_STRING,
], true) === false) {
return;
}

if ($tokens[$previousIndex]['code'] === T_CLOSE_SHORT_ARRAY) {
$previousIndex = $tokens[$previousIndex]['bracket_opener'];
}
$prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, $previousIndex - 1, null, true);
if ($prevIndex === false) {
return;
}
if (in_array($tokens[$prevIndex]['code'], Tokens::$arithmeticTokens, true)) {
return;
}
if ($tokens[$prevIndex]['code'] === T_STRING_CONCAT) {
return;
}

// Is it a parenthesis.
if ($tokens[$previousIndex]['code'] === T_CLOSE_PARENTHESIS) {
$found = $phpcsFile->findPrevious([T_VARIABLE], ($previousIndex - 1), $tokens[$previousIndex]['parenthesis_opener']);
if ($found !== false) {
return;
// Check what exists inside the parenthesis.
$closeParenthesisIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($tokens[$previousIndex]['parenthesis_opener'] - 1), null, true);

if ($closeParenthesisIndex === false || $tokens[$closeParenthesisIndex]['code'] !== T_ARRAY) {
if ($tokens[$closeParenthesisIndex]['code'] === T_STRING) {
return;
}

// If it is not an array check what is inside.
$found = $phpcsFile->findPrevious([T_VARIABLE], ($previousIndex - 1), $tokens[$previousIndex]['parenthesis_opener']);

// If a variable exists it is not Yoda.
if ($found !== false) {
return;
}
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,12 @@ if(
===
// comment
true
){}
){}

if(array($key => $val) === $value){}
if(array($key => $val) == $value){}

if([$key => $val] === $value){}
if([$key => $val] == $value){}

$config['checkAuthIn'] !== $event->getName();
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,32 @@ class DisallowYodaConditionsUnitTest extends AbstractSniffUnitTest
public function getErrorList()
{
return [
7 => 1,
8 => 1,
12 => 1,
13 => 1,
18 => 1,
19 => 1,
24 => 1,
25 => 1,
30 => 1,
31 => 1,
40 => 1,
47 => 1,
48 => 1,
50 => 1,
52 => 1,
57 => 1,
58 => 1,
62 => 1,
68 => 1,
7 => 1,
8 => 1,
12 => 1,
13 => 1,
18 => 1,
19 => 1,
24 => 1,
25 => 1,
30 => 1,
31 => 1,
40 => 1,
47 => 1,
48 => 1,
50 => 1,
52 => 1,
57 => 1,
58 => 1,
62 => 1,
68 => 1,
97 => 3,
98 => 3,
105 => 1,
119 => 1,
120 => 1,
122 => 1,
123 => 1,
];

}//end getErrorList()
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 55785b0

Please sign in to comment.