3.0: Move "variable" related utilities to dedicated VariableHelper + use PHPCSUtils + handle match #2062
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Move "array access keys" related utilities to dedicated VariableHelper
The "array access keys" related utilities are only used by a small set of sniffs, so are better placed in a dedicated class.
This commit moves the
get_array_access_keys()
method and theget_array_access_key()
method to a newWordPressCS\WordPress\Helpers\VariableHelper
and starts using that class in the relevant sniffs.In contrast to some of the other "move methods out of the Sniff class" PRs, these methods have been moved to a class and made
static
- instead of moved to atrait
.The reason for this difference is that the methods in other "moves" are setting properties which the sniff classes would need access to, while these methods are 100% stand-alone.
Note:
It is expected for PHPCSUtils to have dedicated methods for the same at some point in the future. If/when those methods become available, it is recommended for the sniffs to start using the PHPCSUtils methods. With this in mind, this class has been marked as "internal" without BC promise.
Related to #1465
VariableHelper::get_array_access_keys() use PHPCSUtils
Use the PHPCSUtils
GetTokensAsString::compact()
method to retrieve array access keys without extraneous whitespace or comments.Move "is comparison" related utility to dedicated VariableHelper
The "is comparison" related utility method is only used by a small set of sniffs, so is better placed in a dedicated class.
As this method also expects a
T_VARIABLE
token as input, theVariableHelper
class seems appropriate.This commit moves the
is_comparison()
method to the newWordPressCS\WordPress\Helpers\VariableHelper
and starts using that class in the relevant sniffs.Note:
It is expected for PHPCSUtils to have dedicated methods for the same at some point in the future. If/when those methods become available, it is recommended for the sniffs to start using the PHPCSUtils methods.
Related to #1465
VariableHelper::is_comparison(): use PHPCSUtils
... to get the potential parentheses opener.
PHP 8.0 | VariableHelper::is_comparison(): add support for variables being compared in a
match
expressionPHP 8.0 introduced
match
expressions, which do a (strict) comparison on the value in the condition against the options in thematch
body.Ref: https://www.php.net/manual/en/control-structures.match.php
This commit updates the
is_comparison()
method to allow for variables in the "condition" part of amatch
expression to be considered part of a comparison.Tested via the
ValidatedSanitizedInput
sniff.N.B. While reviewing the "array access keys" methods, I noticed that the methods do not account for array assignments without a key, i.e.
$var[]
. This may need improved handling at some point, but is outside the scope of this PR.