-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate the Yoda sniffs and also added tests about them
- Loading branch information
Showing
6 changed files
with
321 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/Standards/Generic/Sniffs/ControlStructures/EnforceYodaConditionsSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
41 changes: 41 additions & 0 deletions
41
src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
60 changes: 60 additions & 0 deletions
60
src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
41 changes: 41 additions & 0 deletions
41
src/Standards/Generic/Tests/ControlStructures/EnforceYodaConditionsUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
60 changes: 60 additions & 0 deletions
60
src/Standards/Generic/Tests/ControlStructures/EnforceYodaConditionsUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |