Skip to content

Commit bb4a456

Browse files
committed
Separate the Yoda sniffs and also added tests about them
1 parent e95daf1 commit bb4a456

File tree

6 files changed

+321
-13
lines changed

6 files changed

+321
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
<?php
2+
/**
3+
* Verifies that inline control statements are not present.
4+
*
5+
* @author Greg Sherwood <gsherwood@squiz.net>
6+
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
29

310
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\ControlStructures;
411

@@ -11,8 +18,10 @@
1118
*
1219
* @see https://github.com/php-fig-rectified/psr2r-sniffer/blob/master/PSR2R/Sniffs/ControlStructures/ConditionalExpressionOrderSniff.php
1320
*/
14-
class YodaOrNoYodaSniff implements Sniff
21+
class DisallowYodaConditionsSniff implements Sniff
1522
{
23+
24+
1625
/**
1726
* Returns an array of tokens this test wants to listen for.
1827
*
@@ -21,8 +30,10 @@ class YodaOrNoYodaSniff implements Sniff
2130
public function register()
2231
{
2332
return Tokens::$comparisonTokens;
33+
2434
}//end register()
2535

36+
2637
/**
2738
* Processes this test, when one of its tokens is encountered.
2839
*
@@ -32,42 +43,50 @@ public function register()
3243
*
3344
* @return void
3445
*/
35-
public function process(File $phpCsFile, $stackPointer)
46+
public function process(File $phpcsFile, $stackPtr)
3647
{
37-
$tokens = $phpCsFile->getTokens();
38-
$prevIndex = $phpCsFile->findPrevious(Tokens::$emptyTokens, $stackPointer - 1, null, true);
39-
if (in_array($tokens[$prevIndex]['code'], [
48+
$tokens = $phpcsFile->getTokens();
49+
$prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
50+
if (in_array(
51+
$tokens[$prevIndex]['code'],
52+
[
4053
T_CLOSE_SHORT_ARRAY,
4154
T_TRUE,
4255
T_FALSE,
4356
T_NULL,
4457
T_LNUMBER,
4558
T_CONSTANT_ENCAPSED_STRING,
46-
], true) === false) {
59+
],
60+
true
61+
) === false
62+
) {
4763
return;
4864
}
4965

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

54-
$prevIndex = $phpCsFile->findPrevious(Tokens::$emptyTokens, $prevIndex - 1, null, true);
70+
$prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevIndex - 1), null, true);
5571
if ($prevIndex === false) {
5672
return;
5773
}
5874

59-
if (in_array($tokens[$prevIndex]['code'], Tokens::$arithmeticTokens, true)) {
75+
if (in_array($tokens[$prevIndex]['code'], Tokens::$arithmeticTokens, true) === true) {
6076
return;
6177
}
6278

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

67-
$phpCsFile->addError(
83+
$phpcsFile->addError(
6884
'Usage of Yoda conditions is not allowed. Switch the expression order.',
69-
$stackPointer,
70-
'YodaCondition'
85+
$stackPtr,
86+
'DisallowYodaCondition'
7187
);
72-
}
73-
}
88+
89+
}//end process()
90+
91+
92+
}//end class
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Verifies that inline control statements are not present.
4+
*
5+
* @author Greg Sherwood <gsherwood@squiz.net>
6+
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\ControlStructures;
11+
12+
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHP_CodeSniffer\Util\Tokens;
15+
16+
class EnforceYodaConditionsSniff implements Sniff
17+
{
18+
19+
20+
/**
21+
* Returns an array of tokens this test wants to listen for.
22+
*
23+
* @return array
24+
*/
25+
public function register()
26+
{
27+
return Tokens::$comparisonTokens;
28+
29+
}//end register()
30+
31+
32+
/**
33+
* Processes this test, when one of its tokens is encountered.
34+
*
35+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
36+
* @param int $stackPtr The position of the current token in the
37+
* stack passed in $tokens.
38+
*
39+
* @return void
40+
*/
41+
public function process(File $phpcsFile, $stackPtr)
42+
{
43+
$tokens = $phpcsFile->getTokens();
44+
$nextIndex = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
45+
if (in_array(
46+
$tokens[$nextIndex]['code'],
47+
[
48+
T_CLOSE_SHORT_ARRAY,
49+
T_TRUE,
50+
T_FALSE,
51+
T_NULL,
52+
T_LNUMBER,
53+
T_CONSTANT_ENCAPSED_STRING,
54+
],
55+
true
56+
) === false
57+
) {
58+
return;
59+
}
60+
61+
if ($tokens[$nextIndex]['code'] === T_CLOSE_SHORT_ARRAY) {
62+
$nextIndex = $tokens[$nextIndex]['bracket_opener'];
63+
}
64+
65+
$nextIndex = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextIndex + 1), null, true);
66+
if ($nextIndex === false) {
67+
return;
68+
}
69+
70+
if (in_array($tokens[$nextIndex]['code'], Tokens::$arithmeticTokens, true) === true) {
71+
return;
72+
}
73+
74+
if ($tokens[$nextIndex]['code'] === T_STRING_CONCAT) {
75+
return;
76+
}
77+
78+
$phpcsFile->addError(
79+
'Use Yoda conditions. Switch the expression order.',
80+
$stackPtr,
81+
'YodaCondition'
82+
);
83+
84+
}//end process()
85+
86+
87+
}//end class
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
$value = '';
4+
// Check booleans
5+
if ($value === true) {}
6+
if ($value == true) {}
7+
if (true === $value) {}
8+
if(true == $value){}
9+
10+
if($value === true){}
11+
if($value == true){}
12+
if(false === $value){}
13+
if(false == $value){}
14+
15+
// check integer comparison
16+
if($value === 5){}
17+
if($value == 5){}
18+
if(5 === $value){}
19+
if(5 == $value){}
20+
21+
// check float comparison
22+
if($value === 5.2){}
23+
if($value == 5.2){}
24+
if(5.2 === $value){}
25+
if(5.2 == $value){}
26+
27+
// check null comparison
28+
if($value === null){}
29+
if($value == null){}
30+
if(null === $value){}
31+
if(null == $value){}
32+
33+
// check string comparison
34+
if($value === 'string'){}
35+
if($value == 'string'){}
36+
if('string' === $value){}
37+
if('string' == $value){}
38+
39+
// check string comparison
40+
$assigned = $value === 'string';
41+
$assigned = 'string' == $value;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Unit test class for the DisallowYodaConditions sniff.
4+
*
5+
* @author Greg Sherwood <gsherwood@squiz.net>
6+
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\Generic\Tests\ControlStructures;
11+
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
14+
class DisallowYodaConditionsUnitTest extends AbstractSniffUnitTest
15+
{
16+
17+
18+
/**
19+
* Returns the lines where errors should occur.
20+
*
21+
* The key of the array should represent the line number and the value
22+
* should represent the number of errors that should occur on that line.
23+
*
24+
* @return array<int, int>
25+
*/
26+
public function getErrorList()
27+
{
28+
return [
29+
7 => 1,
30+
8 => 1,
31+
12 => 1,
32+
13 => 1,
33+
18 => 1,
34+
19 => 1,
35+
30 => 1,
36+
31 => 1,
37+
36 => 1,
38+
37 => 1,
39+
41 => 1,
40+
];
41+
42+
}//end getErrorList()
43+
44+
45+
/**
46+
* Returns the lines where warnings should occur.
47+
*
48+
* The key of the array should represent the line number and the value
49+
* should represent the number of warnings that should occur on that line.
50+
*
51+
* @return array<int, int>
52+
*/
53+
public function getWarningList()
54+
{
55+
return [];
56+
57+
}//end getWarningList()
58+
59+
60+
}//end class
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
$value = '';
4+
// Check booleans
5+
if ($value === true) {}
6+
if ($value == true) {}
7+
if (true === $value) {}
8+
if(true == $value){}
9+
10+
if($value === true){}
11+
if($value == true){}
12+
if(false === $value){}
13+
if(false == $value){}
14+
15+
// check integer comparison
16+
if($value === 5){}
17+
if($value == 5){}
18+
if(5 === $value){}
19+
if(5 == $value){}
20+
21+
// check float comparison
22+
if($value === 5.2){}
23+
if($value == 5.2){}
24+
if(5.2 === $value){}
25+
if(5.2 == $value){}
26+
27+
// check null comparison
28+
if($value === null){}
29+
if($value == null){}
30+
if(null === $value){}
31+
if(null == $value){}
32+
33+
// check string comparison
34+
if($value === 'string'){}
35+
if($value == 'string'){}
36+
if('string' === $value){}
37+
if('string' == $value){}
38+
39+
// check string comparison
40+
$assigned = $value === 'string';
41+
$assigned = 'string' == $value;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Unit test class for the EnforceYodaConditions sniff.
4+
*
5+
* @author Greg Sherwood <gsherwood@squiz.net>
6+
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\Generic\Tests\ControlStructures;
11+
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
14+
class EnforceYodaConditionsUnitTest extends AbstractSniffUnitTest
15+
{
16+
17+
18+
/**
19+
* Returns the lines where errors should occur.
20+
*
21+
* The key of the array should represent the line number and the value
22+
* should represent the number of errors that should occur on that line.
23+
*
24+
* @return array<int, int>
25+
*/
26+
public function getErrorList()
27+
{
28+
return [
29+
5 => 1,
30+
6 => 1,
31+
10 => 1,
32+
11 => 1,
33+
16 => 1,
34+
17 => 1,
35+
28 => 1,
36+
29 => 1,
37+
34 => 1,
38+
35 => 1,
39+
40 => 1,
40+
];
41+
42+
}//end getErrorList()
43+
44+
45+
/**
46+
* Returns the lines where warnings should occur.
47+
*
48+
* The key of the array should represent the line number and the value
49+
* should represent the number of warnings that should occur on that line.
50+
*
51+
* @return array<int, int>
52+
*/
53+
public function getWarningList()
54+
{
55+
return [];
56+
57+
}//end getWarningList()
58+
59+
60+
}//end class

0 commit comments

Comments
 (0)