Skip to content

New NewConstants and RemovedConstants sniffs to detect usage of new/removed PHP constants#526

Merged
wimg merged 5 commits intomasterfrom
feature/new-constants-sniff
Nov 12, 2017
Merged

New NewConstants and RemovedConstants sniffs to detect usage of new/removed PHP constants#526
wimg merged 5 commits intomasterfrom
feature/new-constants-sniff

Conversation

@jrfnl
Copy link
Copy Markdown
Member

@jrfnl jrfnl commented Oct 26, 2017

TL;DR> This PR adds two new sniffs, NewConstants and RemovedConstants, a new Sniff::isUseOfGlobalConstant() utility function and improves the accuracy of the MethodTestFrame::getTargetToken() method.

The PR will be easiest to review by looking at the individual commits.

The lists of constants used has been collected over time and is based on the various migration guides, the changelog and the reserved constants list in the PHP Manual. See below for more detail.

As the T_STRING token is used as a kind of "catch-all", there may initially be false positives for code structures I did not consider. All the same, chances are small as that would also require the T_STRING to have the same content and case as the PHP native constant name.

Any false positives found, should be addressed in the new Sniff::isUseOfGlobalConstant() method and additional unit tests should be added to the dedicated unit test file for that function.
Both the introduction of this new function and the initial unit tests for it are part of this PR.

Note: this sniff currently only handles global constants. It does not deal with class constants as may be provided by PHP Extensions.

This sniff does not automatically detect PHP constants which have been back-filled, but the error codes are modular, so if a PHP constant back-fill is in place in a certain project, the message for that particular constant can easily be excluded from the custom ruleset.

Includes unit tests.

Note: Most of the code for the new Sniff::isUseOfGlobalConstant() method is code I've previously written for a slightly similar sniff in the WordPress Coding Standards.
As I wrote that code and nobody else has touched it since, AFAIK we're ok regarding the licensing of the code as I'm pulling this PR. Nevertheless, I figured I ought to mention it.

The sniff also does not detect clashes between aliased constants and the PHP constants, i.e. use const ABC as NEW_CONSTANT;.
It would be an interesting potential future enhancement to detect these kind of aliases and see if we can on-the-fly exclude the current file being examined for that particular error code, but that's for a future PR.

Fixes #263

N.B.: Merging PR #525 is a prerequisite for the unit tests for this PR to be able to pass as it addresses a bug I discovered while working on this PR.
I've tested this and a passing build which includes the fix from PR #525 can be seen here: https://travis-ci.org/jrfnl/PHPCompatibility/builds/293000017


For the NewConstants sniff the following sources have been used:

For the RemovedConstants sniff:

These sniffs could be further improved by going through the "Predefined constants" pages of all PHP extensions. Done

@jrfnl jrfnl requested a review from wimg October 26, 2017 06:59
@jrfnl
Copy link
Copy Markdown
Member Author

jrfnl commented Oct 29, 2017

I'm going to presume that nobody has looked at this PR yet nor started reviewing and will amend a couple of the commits with yet more constants to sniff for... ;-)

@jrfnl jrfnl force-pushed the feature/new-constants-sniff branch from 03e567c to b2d7174 Compare October 29, 2017 09:31
jrfnl added 5 commits November 2, 2017 08:06
… precise

The `getTargetToken()` method is used to find a token in a test case file based on a preceding comment.

In case the comment was not found, this method could inadvertently return a stackPtr to a completely different token, which could - by accident, not design - pass the test.

In case the target token was not found before the next `/* Case ... */` comment, the method could inadvertently return a stackPtr to the token for a next case, which could - by accident, not design - pass the test.

Both these cases have now been addressed.

Additionally, the method now allows for optionally passing a `$tokenContent` parameter to target one specific token in a code snippet which contains several tokens of the same type.
Includes unit tests for this new method.
The list of constants is based on the various migration guides, the changelog and the reserved constants list in the PHP Manual.

As the `T_STRING` token is used as a kind of "catch-all", there may initially be false positives for code structures I did not consider. All the same, chances are small as that would also require the `T_STRING` to have the same content and case as the PHP native constant name.

This sniff does not automatically detect back-fills, but the error codes are modular, so if a PHP constant back-fill is in place in a certain project, that message for that particular constant can be excluded from the custom ruleset.

Includes unit tests.

The sniff also does not detect clashes between aliased constants and the PHP constants, i.e. `use const ABC as NEW_CONSTANT;`.
It would be an interesting potential future enhancement to detect these kind of aliases and see if we can on-the-fly exclude the current file being examined for that particular error code.
…ved PHP constants

Sister-sniff to the new `NewConstants` sniff.
…be available

While PHPCS tries to back-fill for PHP native tokenizer constants, it does not do so consistently in all PHPCS versions and for all tokens.

As PHPCompatibility still supports PHPCS 1.x and 2.x, using a little more defensive coding is warranted.
@jrfnl jrfnl force-pushed the feature/new-constants-sniff branch from b2d7174 to bb34630 Compare November 2, 2017 07:07
@jrfnl
Copy link
Copy Markdown
Member Author

jrfnl commented Nov 2, 2017

Rebased after the merge of #525 so the build can pass ;-)

@wimg wimg merged commit 4ce4d8d into master Nov 12, 2017
@wimg wimg deleted the feature/new-constants-sniff branch November 12, 2017 12:18
@jrfnl jrfnl added this to the 8.1.0 milestone Nov 13, 2017
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.

Add sniffs for new and removed PHP constants

2 participants