Skip to content

Commit c336428

Browse files
authored
Merge pull request #465 from rodrigoprimo/test-coverage-disallow-yoda-condition
Generic/DisallowYodaConditions: improve code coverage
2 parents 4fd52f7 + 48a1a43 commit c336428

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ public function process(File $phpcsFile, $stackPtr)
5757
T_CONSTANT_ENCAPSED_STRING,
5858
];
5959

60-
if ($previousIndex === false
61-
|| in_array($tokens[$previousIndex]['code'], $relevantTokens, true) === false
62-
) {
60+
if (in_array($tokens[$previousIndex]['code'], $relevantTokens, true) === false) {
6361
return;
6462
}
6563

@@ -71,9 +69,6 @@ public function process(File $phpcsFile, $stackPtr)
7169
}
7270

7371
$prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($previousIndex - 1), null, true);
74-
if ($prevIndex === false) {
75-
return;
76-
}
7772

7873
if (in_array($tokens[$prevIndex]['code'], Tokens::$arithmeticTokens, true) === true) {
7974
return;
@@ -85,16 +80,15 @@ public function process(File $phpcsFile, $stackPtr)
8580

8681
// Is it a parenthesis.
8782
if ($tokens[$previousIndex]['code'] === T_CLOSE_PARENTHESIS) {
88-
// Check what exists inside the parenthesis.
89-
$closeParenthesisIndex = $phpcsFile->findPrevious(
83+
$beforeOpeningParenthesisIndex = $phpcsFile->findPrevious(
9084
Tokens::$emptyTokens,
9185
($tokens[$previousIndex]['parenthesis_opener'] - 1),
9286
null,
9387
true
9488
);
9589

96-
if ($closeParenthesisIndex === false || $tokens[$closeParenthesisIndex]['code'] !== T_ARRAY) {
97-
if ($tokens[$closeParenthesisIndex]['code'] === T_STRING) {
90+
if ($beforeOpeningParenthesisIndex === false || $tokens[$beforeOpeningParenthesisIndex]['code'] !== T_ARRAY) {
91+
if ($tokens[$beforeOpeningParenthesisIndex]['code'] === T_STRING) {
9892
return;
9993
}
10094

@@ -110,14 +104,14 @@ public function process(File $phpcsFile, $stackPtr)
110104
return;
111105
}
112106

113-
// If there is nothing inside the parenthesis, it it not a Yoda.
107+
// If there is nothing inside the parenthesis, it is not a Yoda condition.
114108
$opener = $tokens[$previousIndex]['parenthesis_opener'];
115109
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($previousIndex - 1), ($opener + 1), true);
116110
if ($prev === false) {
117111
return;
118112
}
119-
} else if ($tokens[$closeParenthesisIndex]['code'] === T_ARRAY
120-
&& $this->isArrayStatic($phpcsFile, $closeParenthesisIndex) === false
113+
} else if ($tokens[$beforeOpeningParenthesisIndex]['code'] === T_ARRAY
114+
&& $this->isArrayStatic($phpcsFile, $beforeOpeningParenthesisIndex) === false
121115
) {
122116
return;
123117
}//end if
@@ -144,14 +138,14 @@ public function isArrayStatic(File $phpcsFile, $arrayToken)
144138
{
145139
$tokens = $phpcsFile->getTokens();
146140

147-
$arrayEnd = null;
148141
if ($tokens[$arrayToken]['code'] === T_OPEN_SHORT_ARRAY) {
149142
$start = $arrayToken;
150143
$end = $tokens[$arrayToken]['bracket_closer'];
151144
} else if ($tokens[$arrayToken]['code'] === T_ARRAY) {
152145
$start = $tokens[$arrayToken]['parenthesis_opener'];
153146
$end = $tokens[$arrayToken]['parenthesis_closer'];
154147
} else {
148+
// Shouldn't be possible but may happen if external sniffs are using this method.
155149
return true;
156150
}
157151

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,13 @@ echo match ($text) {
175175
};
176176

177177
1 ?? $nullCoalescingShouldNotTriggerSniff;
178+
179+
1 + 2 === $sniffBailsArithmeticToken;
180+
181+
'string' . 'concat' === $sniffBailsStringConcatToken;
182+
183+
1 != $value;
184+
1 <> $value;
185+
1 >= $value;
186+
1 <= $value;
187+
1 <=> $value;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public function getErrorList()
6767
167 => 1,
6868
173 => 1,
6969
174 => 1,
70+
183 => 1,
71+
184 => 1,
72+
185 => 1,
73+
186 => 1,
74+
187 => 1,
7075
];
7176

7277
}//end getErrorList()

0 commit comments

Comments
 (0)