Skip to content

Commit

Permalink
Separate the Yoda sniffs and also added tests about them
Browse files Browse the repository at this point in the history
  • Loading branch information
gmponos committed Oct 15, 2018
1 parent e95daf1 commit bb4a456
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
/**
* Verifies that inline control statements are not present.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Standards\Generic\Sniffs\ControlStructures;

Expand All @@ -11,8 +18,10 @@
*
* @see https://github.com/php-fig-rectified/psr2r-sniffer/blob/master/PSR2R/Sniffs/ControlStructures/ConditionalExpressionOrderSniff.php
*/
class YodaOrNoYodaSniff implements Sniff
class DisallowYodaConditionsSniff implements Sniff
{


/**
* Returns an array of tokens this test wants to listen for.
*
Expand All @@ -21,8 +30,10 @@ class YodaOrNoYodaSniff implements Sniff
public function register()
{
return Tokens::$comparisonTokens;

}//end register()


/**
* Processes this test, when one of its tokens is encountered.
*
Expand All @@ -32,42 +43,50 @@ public function register()
*
* @return void
*/
public function process(File $phpCsFile, $stackPointer)
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpCsFile->getTokens();
$prevIndex = $phpCsFile->findPrevious(Tokens::$emptyTokens, $stackPointer - 1, null, true);
if (in_array($tokens[$prevIndex]['code'], [
$tokens = $phpcsFile->getTokens();
$prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if (in_array(
$tokens[$prevIndex]['code'],
[
T_CLOSE_SHORT_ARRAY,
T_TRUE,
T_FALSE,
T_NULL,
T_LNUMBER,
T_CONSTANT_ENCAPSED_STRING,
], true) === false) {
],
true
) === false
) {
return;
}

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

$prevIndex = $phpCsFile->findPrevious(Tokens::$emptyTokens, $prevIndex - 1, null, true);
$prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevIndex - 1), null, true);
if ($prevIndex === false) {
return;
}

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

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

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

}//end process()


}//end class
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Verifies that inline control statements are not present.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Standards\Generic\Sniffs\ControlStructures;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

class EnforceYodaConditionsSniff implements Sniff
{


/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return Tokens::$comparisonTokens;

}//end register()


/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$nextIndex = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if (in_array(
$tokens[$nextIndex]['code'],
[
T_CLOSE_SHORT_ARRAY,
T_TRUE,
T_FALSE,
T_NULL,
T_LNUMBER,
T_CONSTANT_ENCAPSED_STRING,
],
true
) === false
) {
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,
'YodaCondition'
);

}//end process()


}//end class
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

$value = '';
// Check booleans
if ($value === true) {}
if ($value == true) {}
if (true === $value) {}
if(true == $value){}

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

// check integer comparison
if($value === 5){}
if($value == 5){}
if(5 === $value){}
if(5 == $value){}

// check float comparison
if($value === 5.2){}
if($value == 5.2){}
if(5.2 === $value){}
if(5.2 == $value){}

// check null comparison
if($value === null){}
if($value == null){}
if(null === $value){}
if(null == $value){}

// check string comparison
if($value === 'string'){}
if($value == 'string'){}
if('string' === $value){}
if('string' == $value){}

// check string comparison
$assigned = $value === 'string';
$assigned = 'string' == $value;
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Unit test class for the DisallowYodaConditions sniff.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Standards\Generic\Tests\ControlStructures;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class DisallowYodaConditionsUnitTest extends AbstractSniffUnitTest
{


/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @return array<int, int>
*/
public function getErrorList()
{
return [
7 => 1,
8 => 1,
12 => 1,
13 => 1,
18 => 1,
19 => 1,
30 => 1,
31 => 1,
36 => 1,
37 => 1,
41 => 1,
];

}//end getErrorList()


/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @return array<int, int>
*/
public function getWarningList()
{
return [];

}//end getWarningList()


}//end class
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

$value = '';
// Check booleans
if ($value === true) {}
if ($value == true) {}
if (true === $value) {}
if(true == $value){}

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

// check integer comparison
if($value === 5){}
if($value == 5){}
if(5 === $value){}
if(5 == $value){}

// check float comparison
if($value === 5.2){}
if($value == 5.2){}
if(5.2 === $value){}
if(5.2 == $value){}

// check null comparison
if($value === null){}
if($value == null){}
if(null === $value){}
if(null == $value){}

// check string comparison
if($value === 'string'){}
if($value == 'string'){}
if('string' === $value){}
if('string' == $value){}

// check string comparison
$assigned = $value === 'string';
$assigned = 'string' == $value;
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Unit test class for the EnforceYodaConditions sniff.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Standards\Generic\Tests\ControlStructures;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class EnforceYodaConditionsUnitTest extends AbstractSniffUnitTest
{


/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @return array<int, int>
*/
public function getErrorList()
{
return [
5 => 1,
6 => 1,
10 => 1,
11 => 1,
16 => 1,
17 => 1,
28 => 1,
29 => 1,
34 => 1,
35 => 1,
40 => 1,
];

}//end getErrorList()


/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @return array<int, int>
*/
public function getWarningList()
{
return [];

}//end getWarningList()


}//end class

0 comments on commit bb4a456

Please sign in to comment.