PrefixAllGlobals: prevent false negatives for autoloaded user-defined global functions#1633
Merged
GaryJones merged 1 commit intoJan 23, 2019
Conversation
GaryJones
previously requested changes
Jan 22, 2019
GaryJones
left a comment
Member
There was a problem hiding this comment.
Minor docs tweak. Code otherwise good to go.
… global functions The `PrefixAllGlobals` sniff verifies that functions declared in the global namespace are prefixed with one of the whitelisted prefixes as passed to the sniff in the `prefixes` property in a custom ruleset. The sniff prevents false positives for polyfills for PHP native functions - which should be named exactly as named in PHP without prefix for them to be usable as a polyfill - by checking that the function didn't exist. Generally speaking this works fine in 95% of all cases as PHPCS is normally run stand-alone and doesn't contain any functions defined in the global namespace. Similarly, when PHPCS is installed via Composer, this would normally work fine as the commonly used `autoload` options are `PSR-0`, `PSR-4` and `classmap` which are all based on code being in classes. However, if the Composer `autoload` `files` option is used to load, for instance, a functions file declaring functions in the global namespace, this "polyfill false positive prevention" would incorrectly cause errors _not_ to be thrown for functions declared in the global namespace which were now autoloaded via Composer. I have now fixed this by, instead of using `function_exists()`, using a check against a functions list retrieved via `get_defined_functions()`. This change does not have unit tests as: * Preventing false positives for polyfills is already unit tested. * Checking that autoloaded user defined functions are not recognized as PHP native functions is not something which can be unit tested as such. This would need an integration test including a composer setup to be tested. Based on the test case provided in the issue which originally reported this, I have confirmed that this PR fixes the issue though. Fixes 1632
d60cbc6 to
97167b2
Compare
GaryJones
approved these changes
Jan 23, 2019
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.
The
PrefixAllGlobalssniff verifies that functions declared in the global namespace are prefixed with one of the whitelisted prefixes as passed to the sniff in theprefixesproperty in a custom ruleset.The sniff prevents false positives for polyfills for PHP native functions - which should be named exactly as named in PHP without prefix for them to be usable as a polyfill - by checking that the function doesn't exist.
Generally speaking this works fine in 98% of all cases as PHPCS is normally run stand-alone and doesn't contain any functions defined in the global namespace.
Similarly, when PHPCS is installed via Composer, this would normally work fine as the commonly used
autoloadoptions arePSR-0,PSR-4andclassmapwhich are all based on code being in classes.However, if the Composer
autoloadfilesoption is used to load, for instance, a functions file declaring functions in the global namespace, this "polyfill false positive prevention" would incorrectly cause errors not to be thrown for functions declared in the global namespace which were now autoloaded via Composer.I have now fixed this by, instead of using
function_exists(), using a check against a functions list retrieved viaget_defined_functions().This change does not have unit tests as:
Based on the test case provided in the issue which originally reported this, I have confirmed that this PR fixes the issue though.
Fixes #1632