Skip to content

Commit

Permalink
Merge pull request #465 from rodrigoprimo/test-coverage-disallow-yoda…
Browse files Browse the repository at this point in the history
…-condition

Generic/DisallowYodaConditions: improve code coverage
  • Loading branch information
jrfnl committed May 13, 2024
2 parents 4fd52f7 + 48a1a43 commit c336428
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public function process(File $phpcsFile, $stackPtr)
T_CONSTANT_ENCAPSED_STRING,
];

if ($previousIndex === false
|| in_array($tokens[$previousIndex]['code'], $relevantTokens, true) === false
) {
if (in_array($tokens[$previousIndex]['code'], $relevantTokens, true) === false) {
return;
}

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

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

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

// Is it a parenthesis.
if ($tokens[$previousIndex]['code'] === T_CLOSE_PARENTHESIS) {
// Check what exists inside the parenthesis.
$closeParenthesisIndex = $phpcsFile->findPrevious(
$beforeOpeningParenthesisIndex = $phpcsFile->findPrevious(
Tokens::$emptyTokens,
($tokens[$previousIndex]['parenthesis_opener'] - 1),
null,
true
);

if ($closeParenthesisIndex === false || $tokens[$closeParenthesisIndex]['code'] !== T_ARRAY) {
if ($tokens[$closeParenthesisIndex]['code'] === T_STRING) {
if ($beforeOpeningParenthesisIndex === false || $tokens[$beforeOpeningParenthesisIndex]['code'] !== T_ARRAY) {
if ($tokens[$beforeOpeningParenthesisIndex]['code'] === T_STRING) {
return;
}

Expand All @@ -110,14 +104,14 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

// If there is nothing inside the parenthesis, it it not a Yoda.
// If there is nothing inside the parenthesis, it is not a Yoda condition.
$opener = $tokens[$previousIndex]['parenthesis_opener'];
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($previousIndex - 1), ($opener + 1), true);
if ($prev === false) {
return;
}
} else if ($tokens[$closeParenthesisIndex]['code'] === T_ARRAY
&& $this->isArrayStatic($phpcsFile, $closeParenthesisIndex) === false
} else if ($tokens[$beforeOpeningParenthesisIndex]['code'] === T_ARRAY
&& $this->isArrayStatic($phpcsFile, $beforeOpeningParenthesisIndex) === false
) {
return;
}//end if
Expand All @@ -144,14 +138,14 @@ public function isArrayStatic(File $phpcsFile, $arrayToken)
{
$tokens = $phpcsFile->getTokens();

$arrayEnd = null;
if ($tokens[$arrayToken]['code'] === T_OPEN_SHORT_ARRAY) {
$start = $arrayToken;
$end = $tokens[$arrayToken]['bracket_closer'];
} else if ($tokens[$arrayToken]['code'] === T_ARRAY) {
$start = $tokens[$arrayToken]['parenthesis_opener'];
$end = $tokens[$arrayToken]['parenthesis_closer'];
} else {
// Shouldn't be possible but may happen if external sniffs are using this method.
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,13 @@ echo match ($text) {
};

1 ?? $nullCoalescingShouldNotTriggerSniff;

1 + 2 === $sniffBailsArithmeticToken;

'string' . 'concat' === $sniffBailsStringConcatToken;

1 != $value;
1 <> $value;
1 >= $value;
1 <= $value;
1 <=> $value;
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public function getErrorList()
167 => 1,
173 => 1,
174 => 1,
183 => 1,
184 => 1,
185 => 1,
186 => 1,
187 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit c336428

Please sign in to comment.