Skip to content

New/RemovedPCREModifiers: decouple the sniffs, introduce PCRERegexTrait and implement PHPCSUtils#1261

Merged
wimg merged 9 commits intodevelopfrom
feature/new-pcre-regex-analyzer-trait
Feb 3, 2021
Merged

New/RemovedPCREModifiers: decouple the sniffs, introduce PCRERegexTrait and implement PHPCSUtils#1261
wimg merged 9 commits intodevelopfrom
feature/new-pcre-regex-analyzer-trait

Conversation

@jrfnl
Copy link
Copy Markdown
Member

@jrfnl jrfnl commented Dec 24, 2020

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 PCRERegexTrait

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.

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 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.

PCRERegexTrait: use PHPCSUtils getDoubleArrowPtr() method

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.

PCRERegexTrait: use PHPCSUtils GetTokensAsString

  • 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.

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::$arrayTokensBC

To check whether the parameter 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.

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.
@jrfnl jrfnl force-pushed the feature/new-pcre-regex-analyzer-trait branch from b86a4df to 7443ff8 Compare December 24, 2020 01:58
@wimg wimg merged commit caad3ad into develop Feb 3, 2021
@wimg wimg deleted the feature/new-pcre-regex-analyzer-trait branch February 3, 2021 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants