Skip to content

Commit

Permalink
WIP work about yoda
Browse files Browse the repository at this point in the history
  • Loading branch information
gmponos committed Oct 15, 2018
1 parent 093ec1b commit 058ffc7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,20 @@ public function register()
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
$prevIndex = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if (in_array(
$tokens[$prevIndex]['code'],
[
T_CLOSE_SHORT_ARRAY,
T_OPEN_SHORT_ARRAY,
T_ARRAY,
T_TRUE,
T_FALSE,
T_NULL,
T_LNUMBER,
T_CONSTANT_ENCAPSED_STRING,
],
true
) === false
) === true
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function process(File $phpcsFile, $stackPtr)
$tokens[$nextIndex]['code'],
[
T_OPEN_SHORT_ARRAY,
T_ARRAY,
T_TRUE,
T_FALSE,
T_NULL,
Expand All @@ -58,23 +59,6 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

if ($tokens[$nextIndex]['code'] === T_CLOSE_SHORT_ARRAY) {
$nextIndex = $tokens[$nextIndex]['bracket_opener'];
}

$nextIndex = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextIndex + 1), null, true);
if ($nextIndex === false) {
return;
}

if (in_array($tokens[$nextIndex]['code'], Tokens::$arithmeticTokens, true) === true) {
return;
}

if ($tokens[$nextIndex]['code'] === T_STRING_CONCAT) {
return;
}

$phpcsFile->addError(
'Use Yoda conditions. Switch the expression order.',
$stackPtr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ if(array() == $value){}

// check string comparison
$assigned = $value === 'string';
$assigned = 'string' == $value;
$assigned = 'string' == $value;

if(($otherValue) === $value){}
if($otherValue === ($value)){}
if(($value) === true){}
if((true) === $value){}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function getErrorList()
13 => 1,
18 => 1,
19 => 1,
24 => 1,
25 => 1,
30 => 1,
31 => 1,
40 => 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ if(array() == $value){}

// check string comparison
$assigned = $value === 'string';
$assigned = 'string' == $value;
$assigned = 'string' == $value;

if(($otherValue) === $value){}
if($otherValue === ($value)){}
if(($value) === true){}
if((true) === $value){}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function getErrorList()
11 => 1,
16 => 1,
17 => 1,
22 => 1,
23 => 1,
28 => 1,
29 => 1,
35 => 1,
Expand Down

0 comments on commit 058ffc7

Please sign in to comment.