Conversation
Looks like some of the test cases were not actually being tested. These cases were originally added in 6c75f62 and luckily still pass, even though they weren't being tested between then and now.
…ait` This moves the logic which is needed by both the `NewPCREModifiers` as well as the `RemovedPCREModifiers` to a dedicated trait. This allows us to decouple the `NewPCREModifiers` sniff from the `RemovedPCREModifiersSniff` and let it extend the `AbstractFunctionCallParameterSniff` class instead. Sniffs which extend other sniffs can run into problems with the autoloading used in PHPCS as per the reports in issue 638 and 793. This is part of an effort to fix those problems. Having this trait in place will also make it easier to potentially, eventually address issue 965. This commit does not change the actual functionality of the sniffs. It only moves the relevant code to the trait and makes the trait methods stand-alone methods which return a value instead of passing off directly to the next function. As part of making the code stand-alone, the `Sniff::stripVariables()` method has been made static. The only difference in functionality is that now the last token in the regex pattern will be used to report the error on in all cases. Previously, this was only the case for array parameters, not for string parameters. Includes adjusted line numbers in the test files for this.
... for consistency with the parent class and the sister-class.
The original code copied over from the `RemovedPCREModifiers` was nested quite deeply. This changes that code to use an "early return" pattern, lowering the nesting levels and improving code readability. There are no functional changes in this commit.
This will be more reliable than just searching for a double arrow as, while unlikely in this particular case, just searching for a double arrow could incorrectly identify an arrow of a nested array, the arrow in a `yield` or for an arrow function as the array double arrow. The `Arrays::getDoubleArrowPtr()` method contains protection against all these type of issues.
* Switch out the call to the PHPCS native `getTokensAsString()` method for calls to the methods in the PHPCSUtils `GetTokensAsString` class for improved results. * Also, the return value of the PHPCSUtils `PassedParameters::getParameters()` et al methods also have a `clean` key, so for feature completeness, the content of that key should also be adjusted to be in line with the actual regex.
As part of the `getRegexModifiers()` method, we're walking the tokens within the regex parameter to retrieve all the text string parts of the parameter. The code already took multi-line text strings into account, but didn't handle heredoc or nowdoc constructs being used to declare the regex yet. While this may be rare, it is allowed and the `TextStrings::getCompleteTextString()` method handles both multi-line text strings as well as supporting all types of text string tokens. Includes making sure that interpolated variables are also being stripped from heredoc texts. Includes removing handling of quotes for multi-line text string. The `TextStrings::getCompleteTextString()` method already handles this correctly. Includes unit tests via the sniff integration tests.
To check whether the paremeter is an array, we can use the `Collections::$arrayTokensBC` array, which also includes the `T_OPEN_SQUARE_BRACKET` which in some cases is in actual fact a short array when not correctly tokenized. No need to follow this by a call to `Arrays::isShortArray()` as the regex parameter can't really be a short list as that doesn't make any sense at all.
b86a4df to
7443ff8
Compare
wimg
approved these changes
Feb 3, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
RemovedPCREModifiers: enable some tests
Looks like some of the test cases were not actually being tested.
These cases were originally added in 6c75f62 and luckily still pass, even though they weren't being tested between then and now.
New/RemovedPCREModifiers: decouple the sniffs, introduce
PCRERegexTraitThis moves the logic which is needed by both the
NewPCREModifiersas well as theRemovedPCREModifiersto a dedicated trait.This allows us to decouple the
NewPCREModifierssniff from theRemovedPCREModifiersSniffand let it extend theAbstractFunctionCallParameterSniffclass instead.Sniffs which extend other sniffs can run into problems with the autoloading used in PHPCS as per the reports in issue #638 and #793.
This is part of an effort to fix those problems.
Having this trait in place will also make it easier to potentially, eventually address issue #965.
This commit does not change the actual functionality of the sniffs.
It only moves the relevant code to the trait and makes the trait methods stand-alone methods which return a value instead of passing off directly to the next function.
As part of making the code stand-alone, the
Sniff::stripVariables()method has been made static.The only difference in functionality is that now the last token in the regex pattern will be used to report the error on in all cases.
Previously, this was only the case for array parameters, not for string parameters.
Includes adjusted line numbers in the test files for this.
RemovedPCREModifiers: move a method up
... for consistency with the parent class and the sister-class.
RemovedPCREModifiers: minor (internal) documentation improvement
PCRERegexTrait: lower nesting level of getRegexPatternsFromParameter()
The original code copied over from the
RemovedPCREModifierswas nested quite deeply.This changes that code to use an "early return" pattern, lowering the nesting levels and improving code readability.
There are no functional changes in this commit.
PCRERegexTrait: use PHPCSUtils
getDoubleArrowPtr()methodThis will be more reliable than just searching for a double arrow as, while unlikely in this particular case, just searching for a double arrow could incorrectly identify an arrow of a nested array, the arrow in a
yieldor for an arrow function as the array double arrow.The
Arrays::getDoubleArrowPtr()method contains protection against all these type of issues.PCRERegexTrait: use PHPCSUtils
GetTokensAsStringgetTokensAsString()method for calls to the methods in the PHPCSUtilsGetTokensAsStringclass for improved results.PassedParameters::getParameters()et al methods also have acleankey, so for feature completeness, the content of that key should also be adjusted to be in line with the actual regex.PCRERegexTrait: use PHPCSUtils
TextStrings::getCompleteTextString()As part of the
getRegexModifiers()method, we're walking the tokens within the regex parameter to retrieve all the text string parts of the parameter.The code already took multi-line text strings into account, but didn't handle heredoc or nowdoc constructs being used to declare the regex yet. While this may be rare, it is allowed and the
TextStrings::getCompleteTextString()method handles both multi-line text strings as well as supporting all types of text string tokens.Includes making sure that interpolated variables are also being stripped from heredoc texts.
Includes removing handling of quotes for multi-line text string. The
TextStrings::getCompleteTextString()method already handles this correctly.Includes unit tests via the sniff integration tests.
PCRERegexTrait: use PHPCSUtils
Collections::$arrayTokensBCTo check whether the parameter is an array, we can use the
Collections::$arrayTokensBCarray, which also includes theT_OPEN_SQUARE_BRACKETwhich in some cases is in actual fact a short array when not correctly tokenized.No need to follow this by a call to
Arrays::isShortArray()as the regex parameter can't really be a short list as that doesn't make any sense at all.