-
-
Notifications
You must be signed in to change notification settings - Fork 36
Add StrictComparisonOperatorRule #443
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace TwigCsFixer\Rules\Operator; | ||
|
|
||
| use TwigCsFixer\Rules\AbstractFixableRule; | ||
| use TwigCsFixer\Token\Token; | ||
| use TwigCsFixer\Token\Tokens; | ||
|
|
||
| /** | ||
| * Ensures that strict comparison operators are used instead of "same as" and "not same as". | ||
| */ | ||
| final class StrictComparisonOperatorRule extends AbstractFixableRule | ||
| { | ||
| protected function process(int $tokenIndex, Tokens $tokens): void | ||
| { | ||
| $token = $tokens->get($tokenIndex); | ||
|
|
||
| if (!$token->isMatching(Token::OPERATOR_TYPE, ['is', 'is not'])) { | ||
| return; | ||
| } | ||
|
|
||
| $isNegated = $token->isMatching(Token::OPERATOR_TYPE, 'is not'); | ||
|
|
||
| $sameAsIndex = $tokens->findNext(Token::WHITESPACE_TOKENS, $tokenIndex + 1, exclude: true); | ||
| if (false === $sameAsIndex) { | ||
| return; | ||
| } | ||
|
|
||
| $sameAsToken = $tokens->get($sameAsIndex); | ||
| $targetIndex = $sameAsIndex; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand the need of this new variable. |
||
|
|
||
| if (!$sameAsToken->isMatching(Token::TEST_NAME_TYPE, 'same')) { | ||
| return; | ||
| } | ||
|
|
||
| $asIndex = $tokens->findNext(Token::WHITESPACE_TOKENS, $targetIndex + 1, exclude: true); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exclude EMPTY_TOKENS rather than WHITESPACE. Cause here you don't support someone with an indent in the middle of the code or a new line. (And add test with this case) |
||
| if (false === $asIndex || !$tokens->get($asIndex)->isMatching(Token::TEST_NAME_TYPE, 'as')) { | ||
| return; | ||
| } | ||
|
|
||
| $openParenthesisIndex = $tokens->findNext(Token::WHITESPACE_TOKENS, $asIndex + 1, exclude: true); | ||
| if (false === $openParenthesisIndex || !$tokens->get($openParenthesisIndex)->isMatching(Token::PUNCTUATION_TYPE, '(')) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You cannot return if there is no punctuation since is a valid syntax similar to |
||
| return; | ||
| } | ||
|
|
||
| $closeParenthesisIndex = $this->findMatchingParenthesis($tokens, $openParenthesisIndex); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't have to do this, there is |
||
| if (false === $closeParenthesisIndex) { | ||
| return; | ||
| } | ||
|
|
||
| $fixer = $this->addFixableError( | ||
| 'Use strict comparison operators === / !== instead of same as / not same as.', | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer a specific message or based on the situation |
||
| $token | ||
| ); | ||
|
|
||
| if (null === $fixer) { | ||
| return; | ||
| } | ||
|
|
||
| $fixer->beginChangeSet(); | ||
|
|
||
| $replacement = $isNegated ? '!==' : '==='; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one time var is useless |
||
|
|
||
| $fixer->replaceToken($tokenIndex, $replacement); | ||
|
|
||
| for ($i = $tokenIndex + 1; $i <= $asIndex; ++$i) { | ||
| $fixer->replaceToken($i, ''); | ||
| } | ||
|
|
||
| // We want only one whitespace between the operator and the parenthesis content. | ||
| // If there is already a whitespace between "as" and "(", we can remove the "(". | ||
| // Otherwise, we replace "(" by a whitespace. | ||
| $hasWhitespaceBeforeParen = $tokens->get($openParenthesisIndex - 1)->isMatching(Token::WHITESPACE_TOKENS); | ||
| $fixer->replaceToken($openParenthesisIndex, $hasWhitespaceBeforeParen ? '' : ' '); | ||
|
Comment on lines
+72
to
+76
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't do any of this here and rely on other fixers. Once |
||
| $fixer->replaceToken($closeParenthesisIndex, ''); | ||
|
|
||
| $fixer->endChangeSet(); | ||
| } | ||
|
|
||
| private function findMatchingParenthesis(Tokens $tokens, int $openParenthesisIndex): int|false | ||
| { | ||
| $level = 0; | ||
| $tokensArray = $tokens->toArray(); | ||
| $count = \count($tokensArray); | ||
|
|
||
| for ($i = $openParenthesisIndex; $i < $count; ++$i) { | ||
| $token = $tokensArray[$i]; | ||
|
|
||
| if ($token->isMatching(Token::PUNCTUATION_TYPE, '(')) { | ||
| ++$level; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if ($token->isMatching(Token::PUNCTUATION_TYPE, ')')) { | ||
| --$level; | ||
|
|
||
| if (0 === $level) { | ||
| return $i; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| {{ a === b }} | ||
| {{ a !== b }} | ||
| {{ a === b }} | ||
| {{ a !== b }} | ||
| {{ a === b }} | ||
| {{ a !== b }} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace TwigCsFixer\Tests\Rules\Operator\StrictComparisonOperator; | ||
|
|
||
| use TwigCsFixer\Rules\Operator\StrictComparisonOperatorRule; | ||
| use TwigCsFixer\Test\AbstractRuleTestCase; | ||
|
|
||
| final class StrictComparisonOperatorRuleTest extends AbstractRuleTestCase | ||
| { | ||
| public function testRule(): void | ||
| { | ||
| $this->checkRule(new StrictComparisonOperatorRule(), [ | ||
| 'StrictComparisonOperator.Error:1:6' => 'Use strict comparison operators === / !== instead of same as / not same as.', | ||
| 'StrictComparisonOperator.Error:2:6' => 'Use strict comparison operators === / !== instead of same as / not same as.', | ||
| 'StrictComparisonOperator.Error:3:6' => 'Use strict comparison operators === / !== instead of same as / not same as.', | ||
| 'StrictComparisonOperator.Error:4:6' => 'Use strict comparison operators === / !== instead of same as / not same as.', | ||
| ]); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| {{ a is same as(b) }} | ||
| {{ a is not same as(b) }} | ||
| {{ a is same as (b) }} | ||
| {{ a is not same as (b) }} | ||
| {{ a === b }} | ||
| {{ a !== b }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exclude EMPTY_TOKENS rather than WHITESPACE.
Cause here you don't support someone with an indent in the middle of the code or a new line. (And add test with this case)