Skip to content

Commit 058ffc7

Browse files
committed
WIP work about yoda
1 parent 093ec1b commit 058ffc7

File tree

6 files changed

+21
-22
lines changed

6 files changed

+21
-22
lines changed

src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,20 @@ public function register()
4646
public function process(File $phpcsFile, $stackPtr)
4747
{
4848
$tokens = $phpcsFile->getTokens();
49-
$prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
49+
$prevIndex = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
5050
if (in_array(
5151
$tokens[$prevIndex]['code'],
5252
[
53-
T_CLOSE_SHORT_ARRAY,
53+
T_OPEN_SHORT_ARRAY,
54+
T_ARRAY,
5455
T_TRUE,
5556
T_FALSE,
5657
T_NULL,
5758
T_LNUMBER,
5859
T_CONSTANT_ENCAPSED_STRING,
5960
],
6061
true
61-
) === false
62+
) === true
6263
) {
6364
return;
6465
}

src/Standards/Generic/Sniffs/ControlStructures/EnforceYodaConditionsSniff.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function process(File $phpcsFile, $stackPtr)
4646
$tokens[$nextIndex]['code'],
4747
[
4848
T_OPEN_SHORT_ARRAY,
49+
T_ARRAY,
4950
T_TRUE,
5051
T_FALSE,
5152
T_NULL,
@@ -58,23 +59,6 @@ public function process(File $phpcsFile, $stackPtr)
5859
return;
5960
}
6061

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-
7862
$phpcsFile->addError(
7963
'Use Yoda conditions. Switch the expression order.',
8064
$stackPtr,

src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,9 @@ if(array() == $value){}
5959

6060
// check string comparison
6161
$assigned = $value === 'string';
62-
$assigned = 'string' == $value;
62+
$assigned = 'string' == $value;
63+
64+
if(($otherValue) === $value){}
65+
if($otherValue === ($value)){}
66+
if(($value) === true){}
67+
if((true) === $value){}

src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public function getErrorList()
3232
13 => 1,
3333
18 => 1,
3434
19 => 1,
35+
24 => 1,
36+
25 => 1,
3537
30 => 1,
3638
31 => 1,
3739
40 => 1,

src/Standards/Generic/Tests/ControlStructures/EnforceYodaConditionsUnitTest.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,9 @@ if(array() == $value){}
5959

6060
// check string comparison
6161
$assigned = $value === 'string';
62-
$assigned = 'string' == $value;
62+
$assigned = 'string' == $value;
63+
64+
if(($otherValue) === $value){}
65+
if($otherValue === ($value)){}
66+
if(($value) === true){}
67+
if((true) === $value){}

src/Standards/Generic/Tests/ControlStructures/EnforceYodaConditionsUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public function getErrorList()
3232
11 => 1,
3333
16 => 1,
3434
17 => 1,
35+
22 => 1,
36+
23 => 1,
3537
28 => 1,
3638
29 => 1,
3739
35 => 1,

0 commit comments

Comments
 (0)