Commit 97167b2
committed
PrefixAllGlobals: prevent false negatives for autoloaded user-defined 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 16321 parent 75fc071 commit 97167b2
File tree
1 file changed
+18
-1
lines changed- WordPress/Sniffs/NamingConventions
1 file changed
+18
-1
lines changedLines changed: 18 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
176 | 188 | | |
177 | 189 | | |
178 | 190 | | |
| |||
181 | 193 | | |
182 | 194 | | |
183 | 195 | | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
184 | 201 | | |
185 | 202 | | |
186 | 203 | | |
| |||
345 | 362 | | |
346 | 363 | | |
347 | 364 | | |
348 | | - | |
| 365 | + | |
349 | 366 | | |
350 | 367 | | |
351 | 368 | | |
| |||
0 commit comments