Skip to content

Variables/ServerVariables: various sniff improvements #850

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

Merged
merged 6 commits into from
Jul 21, 2025

Conversation

jrfnl
Copy link
Collaborator

@jrfnl jrfnl commented Jul 21, 2025

Variables/ServerVariables: improve the tests

  • Expand the tests safeguarding against false positives.
  • Ensure all five indices are covered by at least one test.
  • Add some more variations to the pre-existing tests:
    • Unconventional spacing.
    • Comments in unexpected places.

Variables/ServerVariables: use the correct terminology

The sniff looks for specific indexes/keys for array access.

This commit fixes some documentation and variable names used to use this terminology correctly.

Variables/ServerVariables: bug fix - incorrect quote stripping

As things were, quotes within a text string would also be stripped.
Not that it's very likely for any of the $_SERVER keys to ever have these, but that's beside the point.

Variables/ServerVariables: bug fix - false positives on OO property access

While it should probably be considered bad practice, it is allowed to declare an OO property called $_SERVER and this sniff should not get confused by that.

Includes tests.

Variables/ServerVariables: bug fix - faulty array key determination

The code to find the array index was flawed and could walk beyond the brackets of this array access.

Additionally, array access keys comprised of multiple tokens were not handled correctly.

Includes tests.

Note: WordPressCS has helper functions to retrieve the array access name, but those are marked as internal, which is the reason to introduce a custom function.

Variables/ServerVariables: bug fix - false negatives for $GLOBALS['_SERVER']

The $GLOBALS['_SERVER'] superglobals access is equivalent to using $_SERVER, so should be examined too.

Includes tests.

Closes #550

jrfnl added 6 commits July 21, 2025 19:09
* Expand the tests safeguarding against false positives.
* Ensure all five indices are covered by at least one test.
* Add some more variations to the pre-existing tests:
    - Unconventional spacing.
    - Comments in unexpected places.
The sniff looks for specific indexes/keys for array access.

This commit fixes some documentation and variable names used to use this terminology correctly.
As things were, quotes _within_ a text string would also be stripped.
Not that it's very likely for any of the `$_SERVER` keys to ever have these, but that's beside the point.
…ccess

While it should probably be considered bad practice, it is allowed to declare an OO property called `$_SERVER` and this sniff should not get confused by that.

Includes tests.
The code to find the array index was flawed and could walk beyond the brackets of this array access.

Additionally, array access keys comprised of multiple tokens were not handled correctly.

Includes tests.

Note: WordPressCS has helper functions to retrieve the array access name, but those are marked as internal, which is the reason to introduce a custom function.
…SERVER']`

The `$GLOBALS['_SERVER']` superglobals access is equivalent to using `$_SERVER`, so should be examined too.

Includes tests.
@jrfnl jrfnl added this to the 3.1.0 milestone Jul 21, 2025
@jrfnl jrfnl requested a review from a team as a code owner July 21, 2025 17:24
@jrfnl jrfnl added Type: Enhancement Type: Maintenance Type: False positive PHPCSUtils The addition and utilisation of PHPCSUtils package labels Jul 21, 2025
@jrfnl
Copy link
Collaborator Author

jrfnl commented Jul 21, 2025

Note: It could be considered to also sniff for array indexes provided as nowdoc or heredoc without interpolation, but I expect that will rarely, if ever, be used, so I deemed updating the sniff to go that far over the top, especially considering this is not a security sniff.

@GaryJones GaryJones merged commit ff75da7 into develop Jul 21, 2025
42 checks passed
@GaryJones GaryJones deleted the feature/variables-servervariables-sniff-review branch July 21, 2025 21:20
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.

Review the WordPressVIPMinimum.Variables.ServerVariables sniff
2 participants