Skip to content

Commit 32623e7

Browse files
committed
Hooks/RestrictedHooks: bug fix - quotes within a hook name would be stripped
Use PHPCSUtils `TextStrings::stripQuotes()` to only strip surrounding quotes and not remove any quotes potentially present _within_ the text string. Includes tests.
1 parent ef84c20 commit 32623e7

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

WordPressVIPMinimum/Sniffs/Hooks/RestrictedHooksSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHP_CodeSniffer\Util\Tokens;
1313
use PHPCSUtils\Utils\MessageHelper;
1414
use PHPCSUtils\Utils\PassedParameters;
15+
use PHPCSUtils\Utils\TextStrings;
1516
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
1617

1718
/**
@@ -129,7 +130,7 @@ private function normalize_hook_name_from_parameter( $parameter ) {
129130
$hook_name = '';
130131
for ( $i = $parameter['start']; $i <= $parameter['end']; $i++ ) {
131132
if ( $this->tokens[ $i ]['code'] === T_CONSTANT_ENCAPSED_STRING ) {
132-
$hook_name .= str_replace( [ "'", '"' ], '', $this->tokens[ $i ]['content'] );
133+
$hook_name .= TextStrings::stripQuotes( $this->tokens[ $i ]['content'] );
133134
}
134135
}
135136

WordPressVIPMinimum/Tests/Hooks/RestrictedHooksUnitTest.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,7 @@ add_filter('do_' . 'robots' . 'txt', 'bad_example_function'); // Warning.
7878
// Ignore partially dynamic hook names.
7979
add_filter( 'robots_' . $something . 'txt' , $callback); // OK, ignored as undetermined.
8080
add_filter( 'http_request_timeout' . $something, $callback); // OK, ignored as undetermined.
81+
82+
// Ensure quote stripping is done correctly.
83+
add_filter( 'upload"_mimes', 'bad_example_function' ); // OK, not a filter we're looking for.
84+
add_filter( "upload_'mimes", 'bad_example_function' ); // OK, not a filter we're looking for.

0 commit comments

Comments
 (0)