Conversation
| } | ||
|
|
||
| $sameAsToken = $tokens->get($sameAsIndex); | ||
| $targetIndex = $sameAsIndex; |
There was a problem hiding this comment.
I don't understand the need of this new variable.
|
|
||
| $isNegated = $token->isMatching(Token::OPERATOR_TYPE, 'is not'); | ||
|
|
||
| $sameAsIndex = $tokens->findNext(Token::WHITESPACE_TOKENS, $tokenIndex + 1, exclude: true); |
There was a problem hiding this comment.
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)
| return; | ||
| } | ||
|
|
||
| $asIndex = $tokens->findNext(Token::WHITESPACE_TOKENS, $targetIndex + 1, exclude: true); |
There was a problem hiding this comment.
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)
| } | ||
|
|
||
| $openParenthesisIndex = $tokens->findNext(Token::WHITESPACE_TOKENS, $asIndex + 1, exclude: true); | ||
| if (false === $openParenthesisIndex || !$tokens->get($openParenthesisIndex)->isMatching(Token::PUNCTUATION_TYPE, '(')) { |
There was a problem hiding this comment.
You cannot return if there is no punctuation since
{% if foo is same as bar %}
is a valid syntax similar to
{% if foo is same as(bar) %}
| } | ||
|
|
||
| $fixer = $this->addFixableError( | ||
| 'Use strict comparison operators === / !== instead of same as / not same as.', |
There was a problem hiding this comment.
I prefer a specific message
'Use strict comparison operators === instead of same as.',
or
'Use strict comparison operators !== instead of not same as.',
based on the situation
| return; | ||
| } | ||
|
|
||
| $closeParenthesisIndex = $this->findMatchingParenthesis($tokens, $openParenthesisIndex); |
There was a problem hiding this comment.
You shouldn't have to do this, there is $token->getRelatedToken() to get the related parenthesis
|
|
||
| $fixer->beginChangeSet(); | ||
|
|
||
| $replacement = $isNegated ? '!==' : '==='; |
There was a problem hiding this comment.
This one time var is useless
| // 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 ? '' : ' '); |
There was a problem hiding this comment.
I wouldn't do any of this here and rely on other fixers.
Once same as(foo) is fixed into ===foo the whitespace fixer will change it into === foo.
Add StrictComparisonOperatorRule
Summary
This PR introduces a new optional fixable rule:
StrictComparisonOperatorRule.It replaces Twig strict comparison tests:
is same as(...)→===is not same as(...)→!==Motivation
Twig 3.23 introduced
===and!==as strict comparison operators.Using these operators improves readability and aligns Twig syntax with PHP and JavaScript.
Details
same as/not same ascasesExample
Before
{% if foo is same as(bar) %} {% endif %}After
{% if foo === bar %} {% endif %}