From 7a6c2e5f738cd60a88756fc40d2c4bd6311faa5d Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 17 Nov 2025 10:34:08 -0300 Subject: [PATCH 1/5] WP/DiscouragedFunctions: rename test case file Doing this to be able to move an intentional syntax error test to its own file. --- ...inc => DiscouragedFunctionsUnitTest.1.inc} | 0 .../Tests/WP/DiscouragedFunctionsUnitTest.php | 30 ++++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) rename WordPress/Tests/WP/{DiscouragedFunctionsUnitTest.inc => DiscouragedFunctionsUnitTest.1.inc} (100%) diff --git a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.inc b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc similarity index 100% rename from WordPress/Tests/WP/DiscouragedFunctionsUnitTest.inc rename to WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc diff --git a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php index 825ff39702..e780d96c4b 100644 --- a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php +++ b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php @@ -36,18 +36,26 @@ public function getErrorList() { /** * Returns the lines where warnings should occur. * + * @param string $testFile The name of the test file being run. + * * @return array Key is the line number, value is the number of expected warnings. */ - public function getWarningList() { - return array( - 3 => 1, - 4 => 1, - 20 => 1, - 33 => 1, - 34 => 1, - 53 => 1, - 62 => 1, - 65 => 1, - ); + public function getWarningList( $testFile = '' ) { + switch ( $testFile ) { + case 'DiscouragedFunctionsUnitTest.1.inc': + return array( + 3 => 1, + 4 => 1, + 20 => 1, + 33 => 1, + 34 => 1, + 53 => 1, + 62 => 1, + 65 => 1, + ); + + default: + return array(); + } } } From 214ff6f021057a48fbcd335d8e907d4ab8705a9a Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 17 Nov 2025 10:36:35 -0300 Subject: [PATCH 2/5] WP/DiscouragedFunctions: move syntax error test to its own file --- WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc | 4 ---- WordPress/Tests/WP/DiscouragedFunctionsUnitTest.2.inc | 8 ++++++++ 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 WordPress/Tests/WP/DiscouragedFunctionsUnitTest.2.inc diff --git a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc index 66a33f4984..b434371143 100644 --- a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc +++ b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc @@ -63,7 +63,3 @@ wp_reset_query(); // OK, excluded group. // Safeguard that a function used as a PHP 8.1+ first class callable is also flagged. call_user_func( query_posts(...), $param ); // Warning. - -// Live coding/parse error. -// This has to be the last test in the file!!! -\query_posts diff --git a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.2.inc b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.2.inc new file mode 100644 index 0000000000..0476780120 --- /dev/null +++ b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.2.inc @@ -0,0 +1,8 @@ + Date: Tue, 9 Sep 2025 11:12:03 -0300 Subject: [PATCH 3/5] Tests: non-standard case function call tests for sniffs extending `AbstractFunctionRestrictionsSniff` This commit ensures that the tests for all sniffs extending the `AbstractFunctionRestrictionsSniff` class contain a test with a non-standard case, an unqualified, and a fully qualified function call. In some cases, it was possible to update an existing test, and in other cases, it was necessary to add a new one. For small test files, new tests were inserted alongside related existing tests (rather than appended at the end) to keep similar test cases grouped. The improved test organization should outweigh the added review complexity due to the need to update line numbers in test expectations. Two sniffs that extend `AbstractFunctionRestrictionsSniff` will be addressed separately: `WordPress.Security.EscapeOutput` and `WordPress.WP.AlternativeFunctions`. --- WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc | 4 ++-- WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc | 3 ++- WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.php | 1 + WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc | 2 +- WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc | 4 ++-- WordPress/Tests/PHP/DontExtractUnitTest.inc | 2 ++ WordPress/Tests/PHP/DontExtractUnitTest.php | 4 +++- WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc | 4 ++-- WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.inc | 2 ++ WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.php | 4 +++- WordPress/Tests/Security/SafeRedirectUnitTest.inc | 2 ++ WordPress/Tests/Security/SafeRedirectUnitTest.php | 6 ++++-- WordPress/Tests/WP/DeprecatedFunctionsUnitTest.1.inc | 1 + WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc | 1 + WordPress/Tests/WP/DeprecatedFunctionsUnitTest.php | 3 ++- WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc | 4 ++-- 16 files changed, 32 insertions(+), 15 deletions(-) diff --git a/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc b/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc index 3bebe1fdc2..2a039f9a06 100644 --- a/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc +++ b/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc @@ -23,8 +23,8 @@ prefix_mysql_info(); // Ok. // MYSQL Extension. mysql_affected_rows(); -mysql_connect(); -mysql_close(); +Mysql_CONNECT(); +\MYSQL_close(); mysql_fetch_row(); mysql_info(); mysql_numrows(); diff --git a/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc b/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc index f96842c9e7..c002d706dc 100644 --- a/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc +++ b/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc @@ -5,13 +5,14 @@ date_default_timezone_set( 'Foo/Bar' ); // Bad. $date = new DateTime(); $date->setTimezone( new DateTimeZone( 'America/Toronto' ) ); // Yay! -$post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), date( __( 'F j, Y' ), $now ), date( __( 'g:i a' ), $now ) ); // Error. +$post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), date( __( 'F j, Y' ), $now ), DaTe( __( 'g:i a' ), $now ) ); // Error. $post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), gmdate( __( 'F j, Y' ), $now ), gmdate( __( 'g:i a' ), $now ) ); // OK. /* * Safeguard correct handling of all types of namespaced function calls. */ \date_default_timezone_set( 'Foo/Bar' ); +\DATE_default_timezone_SET( 'Foo/Bar' ); MyNamespace\date_default_timezone_set( 'Foo/Bar' ); \MyNamespace\date_default_timezone_set( 'Foo/Bar' ); namespace\date_default_timezone_set( 'Foo/Bar' ); // The sniff should start flagging this once it can resolve relative namespaces. diff --git a/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.php b/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.php index a2f29c2364..8a37da1c3a 100644 --- a/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.php +++ b/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.php @@ -30,6 +30,7 @@ public function getErrorList() { 3 => 1, 8 => 2, 14 => 1, + 15 => 1, ); } diff --git a/WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc b/WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc index ada6fa9b4a..bdc7c22cc1 100644 --- a/WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc +++ b/WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc @@ -30,7 +30,7 @@ phpinfo(); // Ok - within excluded group. // Reset group exclusions. // phpcs:set WordPress.PHP.DevelopmentFunctions exclude[] -trigger_error(); // Error. +\TRIGGER_ERROR(); // Error. phpinfo(); // Error. Wrapper_Class::var_dump(); // OK, not the native PHP function. diff --git a/WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc b/WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc index 84b7378b65..492c779524 100644 --- a/WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc +++ b/WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc @@ -7,7 +7,7 @@ namespace\serialize( $value ); // The sniff should start flagging this once it c serialize(); // Warning. \serialize( $value ); // Warning. -unserialize(); // Warning. +UNserialize(); // Warning. urlencode(); // Warning. rawurlencode(); // Ok. @@ -19,7 +19,7 @@ ini_restore(); // Warning. magic_quotes_runtime(); // Warning. set_magic_quotes_runtime(); // Warning. apache_setenv(); // Warning. -putenv(); // Warning. +\PUTENV(); // Warning. set_include_path(); // Warning. restore_include_path(); // Warning. diff --git a/WordPress/Tests/PHP/DontExtractUnitTest.inc b/WordPress/Tests/PHP/DontExtractUnitTest.inc index 7cf000403c..79bb1d3bf0 100644 --- a/WordPress/Tests/PHP/DontExtractUnitTest.inc +++ b/WordPress/Tests/PHP/DontExtractUnitTest.inc @@ -1,6 +1,7 @@ 1 ) ); // Bad. +exTRAct( array( 'a' => 1 ) ); // Bad. // Similarly named functions or methods however are fine. my_extract(); // Ok. @@ -12,6 +13,7 @@ $my_object->extract(); // Ok. * Safeguard correct handling of all types of namespaced function calls. */ \extract( array( 'a' => 1 ) ); +\EXTRACT( array( 'a' => 1 ) ); MyNamespace\extract( array( 'a' => 1 ) ); \MyNamespace\extract( array( 'a' => 1 ) ); namespace\extract( array( 'a' => 1 ) ); // The sniff should start flagging this once it can resolve relative namespaces. diff --git a/WordPress/Tests/PHP/DontExtractUnitTest.php b/WordPress/Tests/PHP/DontExtractUnitTest.php index f9a1df7494..52d6fdeb65 100644 --- a/WordPress/Tests/PHP/DontExtractUnitTest.php +++ b/WordPress/Tests/PHP/DontExtractUnitTest.php @@ -30,7 +30,9 @@ final class DontExtractUnitTest extends AbstractSniffUnitTest { public function getErrorList() { return array( 3 => 1, - 14 => 1, + 4 => 1, + 15 => 1, + 16 => 1, ); } diff --git a/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc b/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc index b3bb11a71c..32902c7952 100644 --- a/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc +++ b/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc @@ -13,9 +13,9 @@ $title = preg_split( 'cool', get_the_title() ); // Good. if ( ereg( '[A-Za-z]+', $title, $regs ) ) // Bad, ereg deprecated. Use preg_match instead. die( $regs ); -if ( eregi( '[a-z]+', $title, $regs ) ) {} // Bad, eregi deprecated. Use preg_match instead. +if ( \EREGI( '[a-z]+', $title, $regs ) ) {} // Bad, eregi deprecated. Use preg_match instead. -$title = ereg_replace( 'cool', 'not cool', get_the_title() ); // Bad, ereg_replace has been deprecated. Use preg_replace instead. +$title = ereg_REPLACE( 'cool', 'not cool', get_the_title() ); // Bad, ereg_replace has been deprecated. Use preg_replace instead. $title = eregi_replace( 'cool', 'not cool', get_the_title() ); // Bad, eregi_replace also deprecated. Use preg_replace instead. diff --git a/WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.inc b/WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.inc index 09465fa6c5..88c02ba80d 100644 --- a/WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.inc +++ b/WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.inc @@ -3,11 +3,13 @@ add_action( 'widgets_init', create_function( '', // Error. 'return register_widget( "time_more_on_time_widget" );' ) ); +CREATE_function( '', '' ); // Error. /* * Safeguard correct handling of all types of namespaced function calls. */ \create_function('', 'return;'); +\Create_Function('', 'return;'); MyNamespace\create_function('', 'return;'); \MyNamespace\create_function('', 'return;'); namespace\create_function('', 'return;'); // The sniff should start flagging this once it can resolve relative namespaces. diff --git a/WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.php b/WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.php index 5cfa75ae51..a596706030 100644 --- a/WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.php +++ b/WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.php @@ -28,7 +28,9 @@ final class RestrictedPHPFunctionsUnitTest extends AbstractSniffUnitTest { public function getErrorList() { return array( 3 => 1, - 10 => 1, + 6 => 1, + 11 => 1, + 12 => 1, ); } diff --git a/WordPress/Tests/Security/SafeRedirectUnitTest.inc b/WordPress/Tests/Security/SafeRedirectUnitTest.inc index 3d8d19c02a..3f72f1b33c 100644 --- a/WordPress/Tests/Security/SafeRedirectUnitTest.inc +++ b/WordPress/Tests/Security/SafeRedirectUnitTest.inc @@ -1,12 +1,14 @@ 1, - 9 => 1, + 3 => 1, + 4 => 1, + 10 => 1, + 11 => 1, ); } } diff --git a/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.1.inc b/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.1.inc index 57a44658ef..291eefdc78 100644 --- a/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.1.inc +++ b/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.1.inc @@ -444,3 +444,4 @@ wp_add_editor_classic_theme_styles(); /* ============ WP 6.9 ============ */ seems_utf8(); wp_print_auto_sizes_contain_css_fix(); +wp_PRINT_auto_SIZES_contain_CSS_fix(); diff --git a/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc b/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc index 6c2a7bb8e1..0125f2962c 100644 --- a/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc +++ b/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc @@ -4,6 +4,7 @@ * Safeguard correct handling of all types of namespaced function calls. */ \the_category_ID(); +\THE_category_id(); MyNamespace\the_category_ID(); \MyNamespace\the_category_ID(); namespace\the_category_ID(); // The sniff should start flagging this once it can resolve relative namespaces. diff --git a/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.php b/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.php index 6931a37a9b..2d75be123c 100644 --- a/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.php +++ b/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.php @@ -93,6 +93,7 @@ public function getErrorList( $testFile = '' ) { case 'DeprecatedFunctionsUnitTest.2.inc': return array( 6 => 1, + 7 => 1, ); default: @@ -111,7 +112,7 @@ public function getWarningList( $testFile = '' ) { switch ( $testFile ) { case 'DeprecatedFunctionsUnitTest.1.inc': $start_line = 430; - $end_line = 446; + $end_line = 447; $warnings = array_fill( $start_line, ( ( $end_line - $start_line ) + 1 ), 1 ); // Unset the lines related to version comments. diff --git a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc index b434371143..9d7a422080 100644 --- a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc +++ b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc @@ -1,7 +1,7 @@ Date: Mon, 17 Nov 2025 11:04:08 -0300 Subject: [PATCH 4/5] Tests: add `namespace\Sub\` tests for sniffs extending `AbstractFunctionRestrictionsSniff` This commit adds `namespace\Sub\function_name()` test cases to all test files for sniffs extending `AbstractFunctionRestrictionsSniff`. Two sniffs that extend `AbstractFunctionRestrictionsSniff` will be addressed separately: `WordPress.Security.EscapeOutput` and `WordPress.WP.AlternativeFunctions`. --- .../Tests/DB/RestrictedFunctionsUnitTest.inc | 1 + .../DateTime/RestrictedFunctionsUnitTest.inc | 1 + .../PHP/DevelopmentFunctionsUnitTest.inc | 1 + .../PHP/DiscouragedPHPFunctionsUnitTest.inc | 16 ++++++++++---- WordPress/Tests/PHP/DontExtractUnitTest.inc | 1 + .../Tests/PHP/POSIXFunctionsUnitTest.inc | 1 + .../PHP/RestrictedPHPFunctionsUnitTest.inc | 1 + .../Tests/Security/SafeRedirectUnitTest.inc | 1 + .../WP/DeprecatedFunctionsUnitTest.2.inc | 1 + .../WP/DiscouragedFunctionsUnitTest.1.inc | 21 +++++++++++++------ .../Tests/WP/DiscouragedFunctionsUnitTest.php | 2 +- 11 files changed, 36 insertions(+), 11 deletions(-) diff --git a/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc b/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc index 2a039f9a06..c4e195fd45 100644 --- a/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc +++ b/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc @@ -102,3 +102,4 @@ Myfictional(); // OK. MyNamespace\mysql_connect(); \MyNamespace\mysql_connect(); namespace\mysql_connect(); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\mysql_connect(); diff --git a/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc b/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc index c002d706dc..d2c9e1f160 100644 --- a/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc +++ b/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc @@ -16,3 +16,4 @@ $post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), gmdat MyNamespace\date_default_timezone_set( 'Foo/Bar' ); \MyNamespace\date_default_timezone_set( 'Foo/Bar' ); namespace\date_default_timezone_set( 'Foo/Bar' ); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\date_default_timezone_set( 'Foo/Bar' ); diff --git a/WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc b/WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc index bdc7c22cc1..daa215fe1d 100644 --- a/WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc +++ b/WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc @@ -43,3 +43,4 @@ $wrapper ->var_dump(); // OK, not the native PHP function. MyNamespace\var_dump( $value ); \MyNamespace\var_dump( $value ); namespace\var_dump( $value ); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\var_dump( $value ); diff --git a/WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc b/WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc index 492c779524..636c384e60 100644 --- a/WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc +++ b/WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc @@ -1,9 +1,9 @@ extract(); // Ok. MyNamespace\extract( array( 'a' => 1 ) ); \MyNamespace\extract( array( 'a' => 1 ) ); namespace\extract( array( 'a' => 1 ) ); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\extract( array( 'a' => 1 ) ); diff --git a/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc b/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc index 32902c7952..a50f77a6fa 100644 --- a/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc +++ b/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc @@ -32,3 +32,4 @@ sql_regcase( 'Foo - bar.'); // Bad. Deprecated. MyNamespace\split( ':', $date ); \MyNamespace\split( ':', $date ); namespace\split( ':', $date ); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\split( ':', $date ); diff --git a/WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.inc b/WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.inc index 88c02ba80d..bada4800a3 100644 --- a/WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.inc +++ b/WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.inc @@ -13,3 +13,4 @@ CREATE_function( '', '' ); // Error. MyNamespace\create_function('', 'return;'); \MyNamespace\create_function('', 'return;'); namespace\create_function('', 'return;'); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\create_function('', 'return;'); diff --git a/WordPress/Tests/Security/SafeRedirectUnitTest.inc b/WordPress/Tests/Security/SafeRedirectUnitTest.inc index 3f72f1b33c..28d8a7b0a8 100644 --- a/WordPress/Tests/Security/SafeRedirectUnitTest.inc +++ b/WordPress/Tests/Security/SafeRedirectUnitTest.inc @@ -12,3 +12,4 @@ wp_safe_redirect( $location ); // OK. MyNamespace\wp_redirect( $location ); \MyNamespace\wp_redirect( $location ); namespace\wp_redirect( $location ); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\wp_redirect( $location ); diff --git a/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc b/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc index 0125f2962c..fbdb4650a3 100644 --- a/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc +++ b/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc @@ -8,3 +8,4 @@ MyNamespace\the_category_ID(); \MyNamespace\the_category_ID(); namespace\the_category_ID(); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\the_category_ID(); diff --git a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc index 9d7a422080..6a2194a64e 100644 --- a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc +++ b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc @@ -12,12 +12,12 @@ $obj->query_posts(); // OK, not the global function. MyClass::wp_reset_query(); // OK, not the global function. $obj?->query_posts(); // OK, not the global function. -// Ensure the sniff doesn't act on namespaced calls. -MyNamespace\query_posts(); // OK, not the global function. -\MyNamespace\query_posts(); // OK, not the global function. -namespace\query_posts(); // OK, not the global function. -// ... but does act on fully qualified function calls. -\query_POSTS(); // Warning. + + + + + + // Ensure the sniff doesn't act on functions not listed in the target functions array. query_post(); // OK, not one of the target functions. @@ -63,3 +63,12 @@ wp_reset_query(); // OK, excluded group. // Safeguard that a function used as a PHP 8.1+ first class callable is also flagged. call_user_func( query_posts(...), $param ); // Warning. + +/* + * Safeguard correct handling of all types of namespaced function calls. + */ +\query_POSTS(); // Warning. +MyNamespace\query_posts(); // OK, not the global function. +\MyNamespace\query_posts(); // OK, not the global function. +namespace\query_posts(); // OK, not the global function. +namespace\Sub\query_posts(); // OK, not the global function. diff --git a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php index e780d96c4b..67031257c3 100644 --- a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php +++ b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php @@ -46,12 +46,12 @@ public function getWarningList( $testFile = '' ) { return array( 3 => 1, 4 => 1, - 20 => 1, 33 => 1, 34 => 1, 53 => 1, 62 => 1, 65 => 1, + 70 => 1, ); default: From a6c038010416bb20616eab3a27d05847f72eeb9c Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 17 Nov 2025 11:39:12 -0300 Subject: [PATCH 5/5] Tests: vary function names in namespace tests for sniffs extending `AbstractFunctionRestrictionsSniff` As suggested in 2581, for sniffs that check multiple functions, the namespace name tests now use different function names rather than repeatedly testing the same function. Two sniffs that extend `AbstractFunctionRestrictionsSniff` will be addressed separately: `WordPress.Security.EscapeOutput` and `WordPress.WP.AlternativeFunctions`. --- WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc | 8 ++++---- WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc | 4 ++-- WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc | 8 ++++---- WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc | 8 ++++---- WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc | 8 ++++---- WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc | 8 ++++---- WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc | 4 ++-- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc b/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc index c4e195fd45..d570897176 100644 --- a/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc +++ b/WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc @@ -99,7 +99,7 @@ Myfictional(); // OK. * Safeguard correct handling of all types of namespaced function calls. */ \mysql_connect(); -MyNamespace\mysql_connect(); -\MyNamespace\mysql_connect(); -namespace\mysql_connect(); // The sniff should start flagging this once it can resolve relative namespaces. -namespace\Sub\mysql_connect(); +MyNamespace\mysqli_init(); +\MyNamespace\mysqlnd_qc_clear_cache(); +namespace\maxdb_close(); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\mysqli_fetch(); diff --git a/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc b/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc index d2c9e1f160..e9c010e8a8 100644 --- a/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc +++ b/WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc @@ -13,7 +13,7 @@ $post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), gmdat */ \date_default_timezone_set( 'Foo/Bar' ); \DATE_default_timezone_SET( 'Foo/Bar' ); -MyNamespace\date_default_timezone_set( 'Foo/Bar' ); +MyNamespace\date( 'Y-m-d' ); \MyNamespace\date_default_timezone_set( 'Foo/Bar' ); -namespace\date_default_timezone_set( 'Foo/Bar' ); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\date( 'Y-m-d' ); // The sniff should start flagging this once it can resolve relative namespaces. namespace\Sub\date_default_timezone_set( 'Foo/Bar' ); diff --git a/WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc b/WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc index daa215fe1d..963e06888a 100644 --- a/WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc +++ b/WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc @@ -40,7 +40,7 @@ $wrapper ->var_dump(); // OK, not the native PHP function. * Safeguard correct handling of all types of namespaced function calls. */ \var_dump( $value ); -MyNamespace\var_dump( $value ); -\MyNamespace\var_dump( $value ); -namespace\var_dump( $value ); // The sniff should start flagging this once it can resolve relative namespaces. -namespace\Sub\var_dump( $value ); +MyNamespace\phpinfo(); +\MyNamespace\print_r( $value ); +namespace\error_reporting(); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\debug_backtrace(); diff --git a/WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc b/WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc index 636c384e60..d79d88abd6 100644 --- a/WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc +++ b/WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc @@ -45,7 +45,7 @@ class Serialize {} /* * Safeguard correct handling of all types of namespaced function calls. */ -MyNamespace\serialize( $value ); -\MyNamespace\serialize( $value ); -namespace\serialize( $value ); // The sniff should start flagging this once it can resolve relative namespaces. -namespace\Sub\serialize( $value ); +MyNamespace\urlencode( $value ); +\MyNamespace\putenv( $value ); +namespace\exec( $command ); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\base64_decode( $value ); diff --git a/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc b/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc index a50f77a6fa..7d34f92fe9 100644 --- a/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc +++ b/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc @@ -29,7 +29,7 @@ sql_regcase( 'Foo - bar.'); // Bad. Deprecated. * Safeguard correct handling of all types of namespaced function calls. */ \split( ':', $date ); -MyNamespace\split( ':', $date ); -\MyNamespace\split( ':', $date ); -namespace\split( ':', $date ); // The sniff should start flagging this once it can resolve relative namespaces. -namespace\Sub\split( ':', $date ); +MyNamespace\ereg( 'pattern', $string ); +\MyNamespace\ereg_replace( 'pattern', 'replacement', $string ); +namespace\spliti( ':', $date ); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\sql_regcase( 'string' ); diff --git a/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc b/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc index fbdb4650a3..87418bf766 100644 --- a/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc +++ b/WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc @@ -5,7 +5,7 @@ */ \the_category_ID(); \THE_category_id(); -MyNamespace\the_category_ID(); -\MyNamespace\the_category_ID(); -namespace\the_category_ID(); // The sniff should start flagging this once it can resolve relative namespaces. -namespace\Sub\the_category_ID(); +MyNamespace\permalink_link(); +\MyNamespace\get_postdata(); +namespace\create_user(); // The sniff should start flagging this once it can resolve relative namespaces. +namespace\Sub\user_can_edit_post(); diff --git a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc index 6a2194a64e..067a7907a5 100644 --- a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc +++ b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.1.inc @@ -68,7 +68,7 @@ call_user_func( query_posts(...), $param ); // Warning. * Safeguard correct handling of all types of namespaced function calls. */ \query_POSTS(); // Warning. -MyNamespace\query_posts(); // OK, not the global function. +MyNamespace\wp_reset_query(); // OK, not the global function. \MyNamespace\query_posts(); // OK, not the global function. -namespace\query_posts(); // OK, not the global function. +namespace\wp_reset_query(); // OK, not the global function. namespace\Sub\query_posts(); // OK, not the global function.