Skip to content

Commit 196ae53

Browse files
Merge pull request #897 from WordPress/871-register-setting-check
Remove warning for dynamic callback
2 parents 85bb53a + a7fbef8 commit 196ae53

File tree

5 files changed

+2
-43
lines changed

5 files changed

+2
-43
lines changed

phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/SettingSanitizationSniff.php

-17
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,6 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
8080
$error_code . 'Invalid',
8181
array( $matched_content )
8282
);
83-
84-
return;
85-
}
86-
87-
$next_token = $this->phpcsFile->findNext( Tokens::$emptyTokens, $third_param['start'], ( $third_param['end'] + 1 ), true );
88-
89-
if ( false !== $next_token ) {
90-
$next_type = $this->tokens[ $next_token ]['type'];
91-
92-
if ( in_array( $next_type, array( 'T_ARRAY', 'T_OPEN_SHORT_ARRAY', 'T_VARIABLE', 'T_CALLABLE' ), true ) ) {
93-
$this->phpcsFile->addWarning(
94-
'Dynamic argument passed in third parameter of %s(). Please ensure proper sanitization.',
95-
$stackPtr,
96-
$error_code . 'Dynamic',
97-
array( $matched_content )
98-
);
99-
}
10083
}
10184
}
10285
}

phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/SettingSanitizationUnitTest.inc

-13
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ register_setting( 'my_options_group', 'my_option_name', 10 ); // Error.
44
register_setting( 'my_options_group', 'my_option_name', false ); // Error.
55
register_setting( 'my_options_group', 'my_option_name', 'absint' ); // Good.
66
register_setting('my_options_group','my_option_name', 'sanitize_text_field' ); // Good.
7-
register_setting('my_options_group', 'my_option_name', [ 'sanitize_callback' => 'sanitize_text_field']); // Warning.
8-
$args = array( 'sanitize_callback' => 'absint' );
9-
register_setting( 'my_options_group', 'my_option_name', $args ); // Warning.
107

118
class TestClass {
129
public function register_setting() {
@@ -15,13 +12,3 @@ class TestClass {
1512

1613
$obj = new TestClass();
1714
$obj->register_setting(); // Good.
18-
19-
register_setting('my_options_group', 'my_option_name',array(&$this,'validate')); // Warning.
20-
21-
register_setting(
22-
'my_options_group',
23-
$setting_key,
24-
array(
25-
'sanitize_callback' => $setting['sanitize_callback']
26-
)
27-
); // Warning.

phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/SettingSanitizationUnitTest.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ public function getErrorList() {
3535
* @return array <int line number> => <int number of warnings>
3636
*/
3737
public function getWarningList() {
38-
return array(
39-
7 => 1,
40-
9 => 1,
41-
19 => 1,
42-
21 => 1,
43-
);
38+
return array();
4439
}
4540

4641
/**

tests/phpunit/testdata/plugins/test-plugin-setting-sanitization-check-with-errors/load.php

-2
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,3 @@
1919
register_setting( 'my_options_group', 'option_1' ); // Error.
2020
register_setting( 'my_options_group', 'option_2', false ); // Error.
2121
register_setting( 'my_options_group', 'option_3', 'absint' ); // Good.
22-
$args = array( 'sanitize_callback' => 'absint' );
23-
register_setting( 'my_options_group', 'option_4', $args ); // Warning.

tests/phpunit/tests/Checker/Checks/Setting_Sanitization_Check_Test.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@ public function test_run_with_errors() {
1818

1919
$check->run( $check_result );
2020

21-
$errors = $check_result->get_errors();
22-
$warnings = $check_result->get_warnings();
21+
$errors = $check_result->get_errors();
2322

2423
$this->assertNotEmpty( $errors );
25-
$this->assertNotEmpty( $warnings );
2624
$this->assertArrayHasKey( 'load.php', $errors );
27-
$this->assertArrayHasKey( 'load.php', $warnings );
2825

2926
$this->assertSame( 'PluginCheck.CodeAnalysis.SettingSanitization.register_settingMissing', $errors['load.php'][19][1][0]['code'] );
3027
$this->assertSame( 'PluginCheck.CodeAnalysis.SettingSanitization.register_settingInvalid', $errors['load.php'][20][1][0]['code'] );
31-
$this->assertSame( 'PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic', $warnings['load.php'][23][1][0]['code'] );
3228
}
3329
}

0 commit comments

Comments
 (0)