Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic/DisallowYodaConditions: improve code coverage #465

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
jrfnl marked this conversation as resolved.
Show resolved Hide resolved
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