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
8 changes: 8 additions & 0 deletions WordPress/Tests/CodeAnalysis/EscapedNotTranslatedUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ 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_attr( 'text', 'domain' );
\MyNamespace\esc_attr( 'text', 'domain' );
namespace\esc_attr( 'text', 'domain' ); // The sniff should start flagging this once it can resolve relative namespaces.
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,
);
}
}
8 changes: 8 additions & 0 deletions WordPress/Tests/DateTime/CurrentTimeTimestampUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ 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.
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ 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.
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ 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.
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
8 changes: 8 additions & 0 deletions WordPress/Tests/PHP/IniSetUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,11 @@ 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_set( 'bcmath.scale', 0 ); // Ok.
\MyNamespace\ini_set( 'bcmath.scale', 0 ); // Ok.
namespace\ini_set( 'bcmath.scale', 0 ); // The sniff should start flagging this once it can resolve relative namespaces.
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
8 changes: 8 additions & 0 deletions WordPress/Tests/PHP/PregQuoteDelimiterUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ preg_quote(delimiter: '#', str: $keywords); // OK.
preg_quote(str: $keywords); // Warning.
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.
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,
);
}
}
8 changes: 8 additions & 0 deletions WordPress/Tests/PHP/StrictInArrayUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ 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\in_array( 1, array( '1', 2 ) ); // Ok.
\MyNamespace\in_array( 1, array( '1', 2 ) ); // Ok.
namespace\in_array( 1, array( '1', 2 ) ); // The sniff should start flagging this once it can resolve relative namespaces.
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,
);
}
}
8 changes: 8 additions & 0 deletions WordPress/Tests/Security/PluginMenuSlugUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ 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_menu_page( $page_title, $menu_title, $capability, __FILE__, $function, $icon_url, $position ); // Ok.
\MyNamespace\add_menu_page( $page_title, $menu_title, $capability, __FILE__, $function, $icon_url, $position ); // 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.
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,
);
}
}
11 changes: 7 additions & 4 deletions WordPress/Tests/WP/CapabilitiesUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@ 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\add_posts_page( 'page_title', 'menu_title', 'administrator', 'menu_slug', 'function' ); // Ok. We're not checking namespaced functions.
\MyNamespace\add_posts_page( 'page_title', 'menu_title', 'administrator', 'menu_slug', 'function' ); // Ok.
namespace\add_posts_page( 'page_title', 'menu_title', 'administrator', 'menu_slug', 'function' ); // The sniff should start flagging this once it can resolve relative namespaces.
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 @@ -55,5 +55,10 @@ 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\get_bloginfo( 'home' ); // Ok.
\MyNamespace\get_bloginfo( 'home' ); // Ok.
namespace\get_bloginfo( 'home' ); // The sniff should start flagging this once it can resolve relative namespaces.
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*/, );
83 changes: 50 additions & 33 deletions WordPress/Tests/WP/DeprecatedParameterValuesUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,64 @@ final class DeprecatedParameterValuesUnitTest extends AbstractSniffUnitTest {
/**
* Returns the lines where errors should occur.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int> Key is the line number, value is the number of expected errors.
*/
public function getErrorList() {
return array(
5 => 1,
6 => 1,
7 => 1,
8 => 1,
9 => 1,
10 => 1,
11 => 1,
12 => 1,
13 => 1,
14 => 1,
15 => 1,
16 => 1,
17 => 1,
18 => 1,
35 => 1,
40 => 1,
43 => 1,
44 => 1,
45 => 1,
46 => 1,
47 => 1,
48 => 1,
49 => 1,
50 => 1,
51 => 1,
);
public function getErrorList( $testFile = '' ) {
switch ( $testFile ) {
case 'DeprecatedParameterValuesUnitTest.1.inc':
return array(
5 => 1,
6 => 1,
7 => 1,
8 => 1,
9 => 1,
10 => 1,
11 => 1,
12 => 1,
13 => 1,
14 => 1,
15 => 1,
16 => 1,
17 => 1,
18 => 1,
35 => 1,
40 => 1,
43 => 1,
44 => 1,
45 => 1,
46 => 1,
47 => 1,
48 => 1,
49 => 1,
50 => 1,
51 => 1,
61 => 1,
);

default:
return array();
}
}

/**
* Returns the lines where warnings should occur.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int> Key is the line number, value is the number of expected warnings.
*/
public function getWarningList() {
return array(
55 => 1,
56 => 1,
);
public function getWarningList( $testFile = '' ) {
switch ( $testFile ) {
case 'DeprecatedParameterValuesUnitTest.1.inc':
return array(
55 => 1,
56 => 1,
);

default:
return array();
}
}
}
8 changes: 8 additions & 0 deletions WordPress/Tests/WP/DeprecatedParametersUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,11 @@ global_terms( $foo, 'deprecated' );
// All will give an WARNING as they have been deprecated after WP 6.5.
inject_ignored_hooked_blocks_metadata_attributes('', 'deprecated');
wp_render_elements_support_styles('deprecated');

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\add_option( '', '', [] ); // Bad.
MyNamespace\add_option( '', '', [] ); // Ok.
\MyNamespace\add_option( '', '', [] ); // Ok.
namespace\add_option( '', '', [] ); // The sniff should start flagging this once it can resolve relative namespaces.
3 changes: 3 additions & 0 deletions WordPress/Tests/WP/DeprecatedParametersUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function getErrorList() {
$errors[50] = 2;
$errors[76] = 2;

// Fully qualified function call.
$errors[106] = 1;

return $errors;
}

Expand Down
7 changes: 7 additions & 0 deletions WordPress/Tests/WP/GetMetaSingleUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ $warning = get_metadata(
);
$warning = get_metadata_raw( 'post', $post_id, $meta_key );
$warning = get_metadata_default( 'post', $post_id, $meta_key );

/*
* Safeguard correct handling of fully qualified and relative namespaced function calls (fully qualified global function
* call and partially qualified namespaced function call are already handled above).
*/
\MyNamespace\get_post_meta( $post_id, $meta_key ); // Ok.
namespace\get_post_meta( $post_id, $meta_key ); // The sniff should start flagging this once it can resolve relative namespaces.
10 changes: 10 additions & 0 deletions WordPress/Tests/WP/I18nUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,14 @@ esc_html_e( 'foo', '' ); // Bad: text-domain can not be empty.
// PHP 8.0+: safeguard handling of newly introduced placeholders.
__( 'There are %1$h monkeys in the %H', 'my-slug' ); // Bad: multiple arguments should be numbered.

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\_( 'foo', 'my-slug' ); // Bad.
\translate( 'foo', 'my-slug' ); // Bad.
\translate_with_gettext_context( 'foo', 'bar', 'my-slug' ); // Bad.
MyNamespace\translate_with_gettext_context( 'foo', 'bar', 'my-slug' ); // Ok.
\MyNamespace\translate_with_gettext_context( 'foo', 'bar', 'my-slug' ); // Ok.
namespace\translate_with_gettext_context( 'foo', 'bar', 'my-slug' ); // The sniff should start flagging this once it can resolve relative namespaces.

// phpcs:enable WordPress.WP.I18n.MissingTranslatorsComment
10 changes: 10 additions & 0 deletions WordPress/Tests/WP/I18nUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,14 @@ esc_html_e( 'foo', '' ); // Bad: text-domain can not be empty.
// PHP 8.0+: safeguard handling of newly introduced placeholders.
__( 'There are %1$h monkeys in the %H', 'my-slug' ); // Bad: multiple arguments should be numbered.

/*
* Safeguard correct handling of all types of namespaced function calls.
*/
\_( 'foo', 'my-slug' ); // Bad.
\translate( 'foo', 'my-slug' ); // Bad.
\translate_with_gettext_context( 'foo', 'bar', 'my-slug' ); // Bad.
MyNamespace\translate_with_gettext_context( 'foo', 'bar', 'my-slug' ); // Ok.
\MyNamespace\translate_with_gettext_context( 'foo', 'bar', 'my-slug' ); // Ok.
namespace\translate_with_gettext_context( 'foo', 'bar', 'my-slug' ); // The sniff should start flagging this once it can resolve relative namespaces.

// phpcs:enable WordPress.WP.I18n.MissingTranslatorsComment
3 changes: 3 additions & 0 deletions WordPress/Tests/WP/I18nUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public function getErrorList( $testFile = '' ) {
311 => 1,
315 => 1,
318 => 1,
323 => 1,
);

case 'I18nUnitTest.2.inc':
Expand Down Expand Up @@ -217,6 +218,8 @@ public function getWarningList( $testFile = '' ) {
300 => 1,
301 => 1,
302 => 1,
324 => 1,
325 => 1,
);

case 'I18nUnitTest.2.inc':
Expand Down
Loading