Skip to content

Commit

Permalink
WIP work about yoda conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
gmponos committed Oct 16, 2018
1 parent 058ffc7 commit db6b2bb
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 24 deletions.
Expand Up @@ -46,16 +46,18 @@ public function register()
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$prevIndex = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if (in_array(
$tokens[$prevIndex]['code'],
$nextIndex = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);

if ($nextIndex === false || in_array(
$tokens[$nextIndex]['code'],
[
T_OPEN_SHORT_ARRAY,
T_ARRAY,
T_TRUE,
T_FALSE,
T_NULL,
T_LNUMBER,
T_DNUMBER,
T_CONSTANT_ENCAPSED_STRING,
],
true
Expand All @@ -64,29 +66,35 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

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

$prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevIndex - 1), null, true);
if ($prevIndex === false) {
return;
}
$skipTokens = array_merge(
[
T_CLOSE_PARENTHESIS => T_CLOSE_PARENTHESIS,
T_OPEN_PARENTHESIS => T_OPEN_PARENTHESIS,
],
Tokens::$emptyTokens
);

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

if ($tokens[$prevIndex]['code'] === T_STRING_CONCAT) {
return;
if (in_array(
$tokens[$previousIndex]['code'],
[
T_VARIABLE,
T_STRING,
],
true
) === false
) {
$phpcsFile->addError(
'Usage of Yoda conditions is not allowed. Switch the expression order.',
$stackPtr,
'DisallowYodaCondition'
);
}

$phpcsFile->addError(
'Usage of Yoda conditions is not allowed. Switch the expression order.',
$stackPtr,
'DisallowYodaCondition'
);

}//end process()


Expand Down
Expand Up @@ -51,6 +51,7 @@ public function process(File $phpcsFile, $stackPtr)
T_FALSE,
T_NULL,
T_LNUMBER,
T_DNUMBER,
T_CONSTANT_ENCAPSED_STRING,
],
true
Expand Down
Expand Up @@ -5,7 +5,7 @@ $value = '';
if ($value === true) {}
if ($value == true) {}
if (true === $value) {}
if(true == $value){}
if (true == $value) {}

if($value === true){}
if($value == true){}
Expand Down Expand Up @@ -61,7 +61,57 @@ if(array() == $value){}
$assigned = $value === 'string';
$assigned = 'string' == $value;

if(($otherValue) === $value){}
if($otherValue === ($value)){}
if(($value) === $otherValue){}
if($value === ($otherValue)){}

if(($value) === true){}
if((true) === $value){}
if((true) === $value){}

if(($value + 1 + 1) === $value){}
if(($value + $value) === $value){}

if($value == self::CONSTANT_1){}

const CONSTANT1 = 1;
if($value === CONSTANT1){}
if(CONSTANT1 === $value){}

if($value === ($value1 | $value2)){}
if(($value1 | $value2) === $value){}

// Check with objects
if($object->myVar === $value){}
if($value === $object->myVar){}

if($object->function() === $value){}
if($value === $object->function()){}

// Check with functions
if(myFunction() === $value){}
if($value === myFunction()){}

// check with multiple operations
if($value === true && $value === 1 && $value === null){}
if(($value === true && $value === 1) == ($value === null && $value === new stdClass())){}

if(true === $value && 1 === $value && null === $value){}
if((true === $value && 1 === $value) == (null === $value && new stdClass() === $value)){}

// Add comments in the middle
if(
//comment
true
// comment
===
// comment
$value
){}

if(
//comment
$value
// comment
===
// comment
true
){}
Expand Up @@ -44,6 +44,7 @@ public function getErrorList()
57 => 1,
58 => 1,
62 => 1,
68 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit db6b2bb

Please sign in to comment.