From 574b7bb192f0686c9d8d32bc4c68f61517ac6f9a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 2 Jul 2023 01:47:18 +0200 Subject: [PATCH] NamingConventions/PrefixAllGlobals: make the "is PHP native polyfill ?" check case-insensitive The `PrefixAllGlobals` sniff has a tolerance for non-prefixed polyfills for PHP native functions. This check, however, did a case-sensitive comparison, while PHP natively treats function names case-insensitively. Fixed now. Includes tests. --- .../Sniffs/NamingConventions/PrefixAllGlobalsSniff.php | 4 +++- .../Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php index 03e72181af..e02a2ee650 100644 --- a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php +++ b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php @@ -218,6 +218,7 @@ public function register() { // Get a list of all PHP native functions. $all_functions = get_defined_functions(); $this->built_in_functions = array_flip( $all_functions['internal'] ); + $this->built_in_functions = array_change_key_case( $this->built_in_functions, \CASE_LOWER ); // Set the sniff targets. $targets = array( @@ -413,7 +414,8 @@ public function process_token( $stackPtr ) { } $item_name = FunctionDeclarations::getName( $this->phpcsFile, $stackPtr ); - if ( isset( $this->built_in_functions[ $item_name ] ) ) { + $item_lc = strtolower( $item_name ); + if ( isset( $this->built_in_functions[ $item_lc ] ) ) { // Backfill for PHP native function. return; } diff --git a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc index 96677caecc..fedc9cce9c 100644 --- a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc +++ b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc @@ -644,4 +644,11 @@ function acronym_only_check_global_statement_in_current_scope() { $something = 'value'; // OK, global statement is in different scope. } +/* + * Safeguard that function name comparisons for PHP native function polyfills are done case-insensitively. + */ +if ( function_exists( 'stripos' ) ) { + function striPos() {} +} + // phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[]