Skip to content

Commit 3666bbb

Browse files
authored
Merge pull request #1633 from WordPress-Coding-Standards/feature/1632-prefixallglobals-fix-false-negative-composer-file-autoload
PrefixAllGlobals: prevent false negatives for autoloaded user-defined global functions
2 parents 75fc071 + 97167b2 commit 3666bbb

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ class PrefixAllGlobalsSniff extends AbstractFunctionParameterSniff {
173173
'WP_DEFAULT_THEME' => true,
174174
);
175175

176+
/**
177+
* List of all PHP native functions.
178+
*
179+
* Using this list rather than a call to `function_exists()` prevents
180+
* false negatives from user-defined functions when those would be
181+
* autoloaded via a Composer autoload files directives.
182+
*
183+
* @var array
184+
*/
185+
private $built_in_functions;
186+
187+
176188
/**
177189
* Returns an array of tokens this test wants to listen for.
178190
*
@@ -181,6 +193,11 @@ class PrefixAllGlobalsSniff extends AbstractFunctionParameterSniff {
181193
* @return array
182194
*/
183195
public function register() {
196+
// Get a list of all PHP native functions.
197+
$all_functions = get_defined_functions();
198+
$this->built_in_functions = array_flip( $all_functions['internal'] );
199+
200+
// Set the sniff targets.
184201
$targets = array(
185202
\T_NAMESPACE => \T_NAMESPACE,
186203
\T_FUNCTION => \T_FUNCTION,
@@ -345,7 +362,7 @@ public function process_token( $stackPtr ) {
345362
}
346363

347364
$item_name = $this->phpcsFile->getDeclarationName( $stackPtr );
348-
if ( function_exists( '\\' . $item_name ) ) {
365+
if ( isset( $this->built_in_functions[ $item_name ] ) ) {
349366
// Backfill for PHP native function.
350367
return;
351368
}

0 commit comments

Comments
 (0)