Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -99,6 +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.
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();
8 changes: 5 additions & 3 deletions WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ 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' );
MyNamespace\date_default_timezone_set( 'Foo/Bar' );
\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' );
1 change: 1 addition & 0 deletions WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function getErrorList() {
3 => 1,
8 => 2,
14 => 1,
15 => 1,
);
}

Expand Down
9 changes: 5 additions & 4 deletions WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -40,6 +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.
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();
20 changes: 14 additions & 6 deletions WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

// Safeguard against false positives on namespaced function calls.
MyNamespace\serialize( $value );
\MyNamespace\serialize( $value );
namespace\serialize( $value ); // The sniff should start flagging this once it can resolve relative namespaces.





serialize(); // Warning.
\serialize( $value ); // Warning.
unserialize(); // Warning.
UNserialize(); // Warning.

urlencode(); // Warning.
rawurlencode(); // Ok.
Expand All @@ -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.

Expand All @@ -41,3 +41,11 @@ $handle = popen( '/bin/ls', 'r' );
// Issue #898.
class System {}
class Serialize {}

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
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 );
3 changes: 3 additions & 0 deletions WordPress/Tests/PHP/DontExtractUnitTest.inc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

extract( array( 'a' => 1 ) ); // Bad.
exTRAct( array( 'a' => 1 ) ); // Bad.

// Similarly named functions or methods however are fine.
my_extract(); // Ok.
Expand All @@ -12,6 +13,8 @@ $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.
namespace\Sub\extract( array( 'a' => 1 ) );
4 changes: 3 additions & 1 deletion WordPress/Tests/PHP/DontExtractUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ final class DontExtractUnitTest extends AbstractSniffUnitTest {
public function getErrorList() {
return array(
3 => 1,
14 => 1,
4 => 1,
15 => 1,
16 => 1,
);
}

Expand Down
11 changes: 6 additions & 5 deletions WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -29,6 +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.
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' );
3 changes: 3 additions & 0 deletions WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
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.
namespace\Sub\create_function('', 'return;');
4 changes: 3 additions & 1 deletion WordPress/Tests/PHP/RestrictedPHPFunctionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ final class RestrictedPHPFunctionsUnitTest extends AbstractSniffUnitTest {
public function getErrorList() {
return array(
3 => 1,
10 => 1,
6 => 1,
11 => 1,
12 => 1,
);
}

Expand Down
3 changes: 3 additions & 0 deletions WordPress/Tests/Security/SafeRedirectUnitTest.inc
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

wp_redirect( $location ); // Warning.
WP_redirect( $location ); // Warning.
wp_safe_redirect( $location ); // OK.

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\wp_redirect( $location );
\wp_REDIRECT( $location );
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 );
6 changes: 4 additions & 2 deletions WordPress/Tests/Security/SafeRedirectUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ public function getErrorList() {
*/
public function getWarningList() {
return array(
3 => 1,
9 => 1,
3 => 1,
4 => 1,
10 => 1,
11 => 1,
);
}
}
1 change: 1 addition & 0 deletions WordPress/Tests/WP/DeprecatedFunctionsUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
8 changes: 5 additions & 3 deletions WordPress/Tests/WP/DeprecatedFunctionsUnitTest.2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Safeguard correct handling of all types of namespaced function calls.
*/
\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.
\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();
3 changes: 2 additions & 1 deletion WordPress/Tests/WP/DeprecatedFunctionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function getErrorList( $testFile = '' ) {
case 'DeprecatedFunctionsUnitTest.2.inc':
return array(
6 => 1,
7 => 1,
);

default:
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

query_posts(); // Warning, use WP_Query instead.
wp_reset_query(); // Warning, use wp_reset_postdata instead.
WP_reset_QUERY(); // Warning, use wp_reset_postdata instead.

/*
* Tests which are more specifically for the AbstractFunctionRestrictionsSniff class and Helper methods.
Expand All @@ -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.
Expand Down Expand Up @@ -64,6 +64,11 @@ 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
/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\query_POSTS(); // Warning.
MyNamespace\wp_reset_query(); // OK, not the global function.
\MyNamespace\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.
8 changes: 8 additions & 0 deletions WordPress/Tests/WP/DiscouragedFunctionsUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/*
* Intentional parse error (missing opening parenthesis).
* This should be the only test in this file.
*/

\query_posts
30 changes: 19 additions & 11 deletions WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, int> 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,
33 => 1,
34 => 1,
53 => 1,
62 => 1,
65 => 1,
70 => 1,
);

default:
return array();
}
}
}
Loading