Skip to content
Draft
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
13 changes: 11 additions & 2 deletions WordPress/Tests/CodeAnalysis/EscapedNotTranslatedUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ esc_html( 'text' );
esc_html( $var );

esc_html( 'text', 'domain' ); // Warning.
esc_html( $foo, $bar ); // Warning.
esc_attr(
\ESC_HTML( $foo, $bar ); // Warning.
esc_ATTR(
'text', // Some comment.
MY_DOMAIN // More comment.
); // Warning.

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\esc_attr( 'text', 'domain' );
MyNamespace\esc_html( 'text', 'domain' );
\MyNamespace\esc_attr( 'text', 'domain' );
namespace\esc_html( 'text', 'domain' ); // The sniff should start flagging this once it can resolve relative namespaces.
namespace\Sub\esc_attr( 'text', 'domain' );
7 changes: 4 additions & 3 deletions WordPress/Tests/CodeAnalysis/EscapedNotTranslatedUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function getErrorList() {
*/
public function getWarningList() {
return array(
6 => 1,
7 => 1,
8 => 1,
6 => 1,
7 => 1,
8 => 1,
16 => 1,
);
}
}
11 changes: 10 additions & 1 deletion WordPress/Tests/DateTime/CurrentTimeTimestampUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ current_time( // Error.
);

current_time( 'timestamp', $gmt ); // Warning.
current_time( 'timestamp', false ); // Warning.
\Current_Time( 'timestamp', false ); // Warning.
current_time( 'U', 0 ); // Warning.
current_time( 'U' ); // Warning.

Expand All @@ -30,3 +30,12 @@ current_time( gmt: true, type: 'mysql', ); // OK.
current_time( type: 'Y-m-d' ); // OK.
current_time( gmt: true, type: 'timestamp' ); // Error.
current_time( gmt: 0, type : 'U' ); // Warning.

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\current_time( 'timestamp', true );
MyNamespace\current_time( 'timestamp', true );
\MyNamespace\current_time( 'timestamp', true );
namespace\current_time( 'timestamp', true ); // The sniff should start flagging this once it can resolve relative namespaces.
namespace\Sub\current_time( 'timestamp', true );
11 changes: 10 additions & 1 deletion WordPress/Tests/DateTime/CurrentTimeTimestampUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ current_time( // Error.
);

current_time( 'timestamp', $gmt ); // Warning.
current_time( 'timestamp', false ); // Warning.
\Current_Time( 'timestamp', false ); // Warning.
current_time( 'U', 0 ); // Warning.
current_time( 'U' ); // Warning.

Expand All @@ -27,3 +27,12 @@ current_time( gmt: true, type: 'mysql', ); // OK.
current_time( type: 'Y-m-d' ); // OK.
time(); // Error.
current_time( gmt: 0, type : 'U' ); // Warning.

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\time();
MyNamespace\current_time( 'timestamp', true );
\MyNamespace\current_time( 'timestamp', true );
namespace\current_time( 'timestamp', true ); // The sniff should start flagging this once it can resolve relative namespaces.
namespace\Sub\current_time( 'timestamp', true );
1 change: 1 addition & 0 deletions WordPress/Tests/DateTime/CurrentTimeTimestampUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function getErrorList() {
11 => 1,
17 => 1,
31 => 1,
37 => 1,
);
}

Expand Down
13 changes: 11 additions & 2 deletions WordPress/Tests/NamingConventions/ValidPostTypeSlugUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ register_post_type( 'my_own_post_type', array() ); // OK.
register_post_type( 'my-own-post-type-too-long', array() ); // Bad.
register_post_type( 'author', array() ); // Bad. Reserved slug name.
register_post_type( 'My-Own-Post-Type', array() ); // Bad. Invalid chars: uppercase.
register_post_type( 'my/own/post/type', array() ); // Bad. Invalid chars: "/".
register_POST_TYPE( 'my/own/post/type', array() ); // Bad. Invalid chars: "/".

register_post_type( <<<EOD
my_own_post_type
Expand Down Expand Up @@ -33,7 +33,7 @@ register_post_type( $this->get_post_type_id() ); // Non string literal. Warning
register_post_type( null, array() ); // Non string literal. Warning with severity: 3
register_post_type( 1000, array() ); // Non string literal. Warning with severity: 3

register_post_type( 'wp_', array() ); // Bad. Reserved prefix.
\REGISTER_post_TYPE( 'wp_', array() ); // Bad. Reserved prefix.
register_post_type( 'wp_post_type', array() ); // Bad. Reserved prefix.

register_post_type( '', array() ); // Bad. Empty post type slug.
Expand Down Expand Up @@ -68,3 +68,12 @@ register_post_type(
// Post type name.
$name,// Non string literal. Warning with severity: 3
);

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\register_post_type( 'my-own-post-type-too-long', array() ); // Bad.
MyNamespace\register_post_type( 'my-own-post-type-too-long', array() ); // Ok.
\MyNamespace\register_post_type( 'my-own-post-type-too-long', array() ); // Ok.
namespace\register_post_type( 'my-own-post-type-too-long', array() ); // The sniff should start flagging this once it can resolve relative namespaces.
namespace\Sub\register_post_type( 'my-own-post-type-too-long', array() ); // Ok.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function getErrorList( $testFile = '' ) {
52 => 1,
62 => 1,
64 => 1,
75 => 1,
);

case 'ValidPostTypeSlugUnitTest.2.inc':
Expand Down
13 changes: 11 additions & 2 deletions WordPress/Tests/PHP/IniSetUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ ini_set('short_open_tag', 'On'); // Ok.
ini_set('short_open_tag', 'on'); // Ok.

ini_set('bcmath.scale', 0); // Error.
ini_set( 'bcmath.scale' ,0 ); // Error.
ini_set('display_errors', 0); // Error.
\ini_SET( 'bcmath.scale' ,0 ); // Error.
INI_set('display_errors', 0); // Error.
ini_set('error_reporting', 0); // Error.
ini_set('filter.default', 'full_special_chars'); // Error.
ini_set('filter.default_flags', 0); // Error.
Expand Down Expand Up @@ -58,3 +58,12 @@ ini_set(
// Set the number of decimals.
0
); // Error.

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\ini_set( 'bcmath.scale', 0 ); // Error.
MyNamespace\ini_alter( 'bcmath.scale', 0 ); // Ok.
\MyNamespace\ini_set( 'bcmath.scale', 0 ); // Ok.
namespace\ini_alter( 'bcmath.scale', 0 ); // The sniff should start flagging this once it can resolve relative namespaces.
namespace\Sub\ini_set( 'bcmath.scale', 0 ); // Ok.
1 change: 1 addition & 0 deletions WordPress/Tests/PHP/IniSetUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function getErrorList() {
42 => 1,
51 => 1,
55 => 1,
65 => 1,
);
}

Expand Down
13 changes: 11 additions & 2 deletions WordPress/Tests/PHP/PregQuoteDelimiterUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ preg_quote($keywords, '/'); // OK.
preg_quote( $keywords, '`' ); // OK.

preg_quote($keywords); // Warning.
$textbody = preg_replace ( "/" . preg_quote($word) . "/", // Warning
$textbody = preg_replace ( "/" . \PREG_quote($word) . "/", // Warning
"<i>" . $word . "</i>",
$textbody );

// Safeguard support for PHP 8.0+ named parameters.
preg_quote(delimiter: '#', str: $keywords); // OK.
preg_quote(str: $keywords); // Warning.
preg_quote(str: $keywords, delimitter: '#'); // Warning (typo in param name).
Preg_QUOTE(str: $keywords, delimitter: '#'); // Warning (typo in param name).
preg_quote(delimiter: '#'); // OK. Invalid function call, but that's not the concern of this sniff.

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\preg_quote($keywords); // Warning.
MyNamespace\preg_quote($keywords); // Ok.
\MyNamespace\preg_quote($keywords); // Ok.
namespace\preg_quote($keywords); // The sniff should start flagging this once it can resolve relative namespaces.
namespace\Sub\preg_quote($keywords); // Ok.
1 change: 1 addition & 0 deletions WordPress/Tests/PHP/PregQuoteDelimiterUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function getWarningList() {
7 => 1,
13 => 1,
14 => 1,
20 => 1,
);
}
}
11 changes: 10 additions & 1 deletion WordPress/Tests/PHP/StrictInArrayUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
in_array( 1, array( '1', 1, true ), true ); // Ok.

in_array( 1, array( '1', 1, true ) ); // Warning.
in_array( 1, array( '1', 1, true ), false ); // Warning.
\In_Array( 1, array( '1', 1, true ), false ); // Warning.
IN_ARRAY( 1, array( '1', 1, true ), false ); // Warning.

Foo::in_array( 1, array( '1', 1, true ) ); // Ok.
Expand Down Expand Up @@ -55,3 +55,12 @@ array_search(
$haystack,
true // Use strict typing.
); // Ok.

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\in_array( 1, array( '1', 2 ) ); // Bad.
MyNamespace\array_search( 1, array( '1', 2 ) ); // Ok.
\MyNamespace\array_keys( array( '1', 2 ) ); // Ok.
namespace\in_array( 1, array( '1', 2 ) ); // The sniff should start flagging this once it can resolve relative namespaces.
namespace\Sub\array_search( 1, array( '1', 2 ) ); // Ok.
1 change: 1 addition & 0 deletions WordPress/Tests/PHP/StrictInArrayUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function getWarningList() {
44 => 1,
48 => 1,
49 => 1,
62 => 1,
);
}
}
13 changes: 11 additions & 2 deletions WordPress/Tests/Security/PluginMenuSlugUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

add_menu_page( $page_title, $menu_title, $capability, __FILE__, $function, $icon_url, $position ); // Bad.

add_dashboard_page( $page_title, $menu_title, $capability, __file__, $function); // Bad.
\ADD_DASHBOARD_PAGE( $page_title, $menu_title, $capability, __file__, $function); // Bad.

add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, 'awesome-submenu-page', $function ); // Ok.

add_submenu_page( __FILE__ . 'parent', $page_title, $menu_title, $capability, __FILE__, $function ); // Bad x 2.
Add_Submenu_Page( __FILE__ . 'parent', $page_title, $menu_title, $capability, __FILE__, $function ); // Bad x 2.

// These are all ok: not calling the WP core function.
$my_class->add_dashboard_page( $page_title, $menu_title, $capability, __FILE__, $function); // Ok.
Expand All @@ -20,3 +20,12 @@ add_submenu_page(
parent_slug: __FILE__, // Bad.
capability: $capability,
);

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\add_menu_page( $page_title, $menu_title, $capability, __FILE__, $function, $icon_url, $position ); // Bad.
MyNamespace\add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, __FILE__, $function ); // Ok.
\MyNamespace\add_dashboard_page( $page_title, $menu_title, $capability, __FILE__, $function ); // Ok.
namespace\add_menu_page( $page_title, $menu_title, $capability, __FILE__, $function, $icon_url, $position ); // The sniff should start flagging this once it can resolve relative namespaces.
namespace\Sub\add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, __FILE__, $function ); // Ok.
1 change: 1 addition & 0 deletions WordPress/Tests/Security/PluginMenuSlugUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function getWarningList() {
5 => 1,
9 => 2,
20 => 1,
27 => 1,
);
}
}
16 changes: 10 additions & 6 deletions WordPress/Tests/WP/CapabilitiesUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ add_menu_page( $pagetitle, 'menu_title', 'foo_bar', 'handle', 'function', 'icon_
* Roles found instead of capabilities.
*/
add_posts_page( 'page_title', 'menu_title', 'administrator', 'menu_slug', 'function' ); // Error.
add_media_page( 'page_title', 'menu_title', 'editor', 'menu_slug', 'function' ); // Error.
add_pages_page( 'page_title', 'menu_title', 'author', 'menu_slug', 'function' ); // Error.
\aDd_MeDiA_pAgE( 'page_title', 'menu_title', 'editor', 'menu_slug', 'function' ); // Error.
ADD_PAGES_PAGE( 'page_title', 'menu_title', 'author', 'menu_slug', 'function' ); // Error.
add_comments_page( 'page_title', 'menu_title', 'contributor', 'menu_slug', 'function' ); // Error.
add_theme_page( 'page_title', $menu_title, 'subscriber', 'menu_slug', 'function' ); // Error.
add_plugins_page( 'page_title', 'menu_title', 'super_admin', 'menu_slug', 'function' ); // Error.
Expand Down Expand Up @@ -111,8 +111,12 @@ add_menu_page( $p, $t, /* deliberately empty */, $slug, );
add_menu_page( [] ); // Should bow out because the parameter is not found.

$obj->current_user_can( 'foo_bar' ); // Ok. We're not checking for method calls.
My\NamespaceS\add_posts_page( 'page_title', 'menu_title', 'administrator', 'menu_slug', 'function' ); // Ok. We're not checking namespaced functions.

// Parse error, should be handled correctly by bowing out.
// This must be the last test in the file!
add_posts_page( 'page_title',
/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\add_posts_page( 'page_title', 'menu_title', 'administrator', 'menu_slug', 'function' ); // Bad.
My\NamespaceS\current_user_can( 'administrator' ); // Ok.
\MyNamespace\add_comments_page( 'page_title', 'menu_title', 'administrator', 'menu_slug', 'function' ); // Ok.
namespace\author_can( $post, 'administrator' ); // The sniff should start flagging this once it can resolve relative namespaces.
namespace\Sub\add_posts_page( 'page_title', 'menu_title', 'administrator', 'menu_slug', 'function' ); // Ok.
8 changes: 8 additions & 0 deletions WordPress/Tests/WP/CapabilitiesUnitTest.5.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/*
* Intentional parse error (nothing after comma separating parameters).
* This should be the only test in this file.
*/

add_posts_page( 'page_title',
1 change: 1 addition & 0 deletions WordPress/Tests/WP/CapabilitiesUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function getErrorList( $testFile = '' ) {
78 => 1,
85 => 1,
106 => 1,
118 => 1,
);

case 'CapabilitiesUnitTest.3.inc':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// All will give an ERROR.

get_bloginfo( 'home' );
get_bloginfo( 'siteurl' );
get_bloginfo( "text_direction" );
\GeT_bLoGiNfO( 'siteurl' );
Get_Bloginfo( "text_direction" );
echo bloginfo( 'home' );
echo bloginfo( "siteurl" );
echo bloginfo( 'text_direction' );
Expand Down Expand Up @@ -55,5 +55,11 @@ wp_get_typography_font_size_value( $preset, array() ); // OK.
wp_get_typography_font_size_value( $preset, true ); // Error.
wp_get_typography_font_size_value( $preset, false ); // Error.

// Live coding/parse error.
get_bloginfo( show: /*to do*/, );
/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\get_bloginfo( 'home' ); // Bad.
MyNamespace\register_setting( 'privacy' ); // Ok.
\MyNamespace\unregister_setting( 'misc' ); // Ok.
namespace\get_option('blacklist_keys'); // The sniff should start flagging this once it can resolve relative namespaces.
namespace\Sub\add_option('comment_whitelist', $value); // Ok.
8 changes: 8 additions & 0 deletions WordPress/Tests/WP/DeprecatedParameterValuesUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/*
* Intentional parse error (empty named parameter).
* This should be the only test in this file.
*/

get_bloginfo( show: /*to do*/, );
Loading
Loading