From d4d2668aa8b7c355bd73d9b56e2831d1a5ada88a Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 02:51:06 +0000 Subject: [PATCH 1/5] Composer: Add PHPCSUtils as required dependency VIPCS makes use of PHPCSUtils directly (such as for MessageHelper), so it should be marked as such in the Composer config. --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index eabe943a..58af65a1 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "require": { "php": ">=5.4", "dealerdirect/phpcodesniffer-composer-installer": "^0.4.1 || ^0.5 || ^0.6.2 || ^0.7 || ^1.0", + "phpcsstandards/phpcsutils": "^1.0", "sirbrillig/phpcs-variable-analysis": "^2.11.1", "squizlabs/php_codesniffer": "^3.7.1", "wp-coding-standards/wpcs": "^2.3" From 3796c6071179af36f7c080c3e9ae15d9d614947a Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:13:33 +0000 Subject: [PATCH 2/5] Sniff::strip_quotes(): use PHPCSUtils version --- WordPressVIPMinimum/Sniffs/Functions/DynamicCallsSniff.php | 3 ++- .../Sniffs/Performance/LowExpiryCacheTimeSniff.php | 3 ++- .../Sniffs/Security/ProperEscapingFunctionSniff.php | 5 +++-- WordPressVIPMinimum/Sniffs/Security/UnderscorejsSniff.php | 5 +++-- .../Sniffs/UserExperience/AdminBarRemovalSniff.php | 5 +++-- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Functions/DynamicCallsSniff.php b/WordPressVIPMinimum/Sniffs/Functions/DynamicCallsSniff.php index c069696f..ca2f2acc 100644 --- a/WordPressVIPMinimum/Sniffs/Functions/DynamicCallsSniff.php +++ b/WordPressVIPMinimum/Sniffs/Functions/DynamicCallsSniff.php @@ -8,6 +8,7 @@ namespace WordPressVIPMinimum\Sniffs\Functions; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\Utils\TextStrings; use WordPressVIPMinimum\Sniffs\Sniff; /** @@ -139,7 +140,7 @@ private function collect_variables() { * If we reached the end of the loop and the $value_ptr was set, we know for sure * this was a plain text string variable assignment. */ - $current_var_value = $this->strip_quotes( $this->tokens[ $value_ptr ]['content'] ); + $current_var_value = TextStrings::stripQuotes( $this->tokens[ $value_ptr ]['content'] ); if ( isset( $this->disallowed_functions[ $current_var_value ] ) === false ) { // Text string is not one of the ones we're looking for. diff --git a/WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php b/WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php index 23639560..d22716ad 100644 --- a/WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php +++ b/WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php @@ -8,6 +8,7 @@ namespace WordPressVIPMinimum\Sniffs\Performance; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\Utils\TextStrings; use WordPressCS\WordPress\AbstractFunctionParameterSniff; /** @@ -147,7 +148,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p } if ( $this->tokens[ $i ]['code'] === T_CONSTANT_ENCAPSED_STRING ) { - $content = $this->strip_quotes( $this->tokens[ $i ]['content'] ); + $content = TextStrings::stripQuotes( $this->tokens[ $i ]['content'] ); if ( is_numeric( $content ) === true ) { $tokensAsString .= $content; continue; diff --git a/WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php b/WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php index 416ae604..5548c865 100644 --- a/WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php +++ b/WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php @@ -8,8 +8,9 @@ namespace WordPressVIPMinimum\Sniffs\Security; -use WordPressVIPMinimum\Sniffs\Sniff; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\Utils\TextStrings; +use WordPressVIPMinimum\Sniffs\Sniff; /** * Checks whether proper escaping function is used. @@ -195,7 +196,7 @@ public function process_token( $stackPtr ) { $content = $this->tokens[ $html ]['content']; if ( isset( Tokens::$stringTokens[ $this->tokens[ $html ]['code'] ] ) === true ) { - $content = Sniff::strip_quotes( $content ); + $content = TextStrings::stripQuotes( $content ); } $escaping_type = $this->escaping_functions[ $function_name ]; diff --git a/WordPressVIPMinimum/Sniffs/Security/UnderscorejsSniff.php b/WordPressVIPMinimum/Sniffs/Security/UnderscorejsSniff.php index b15114ea..f76e773e 100644 --- a/WordPressVIPMinimum/Sniffs/Security/UnderscorejsSniff.php +++ b/WordPressVIPMinimum/Sniffs/Security/UnderscorejsSniff.php @@ -9,6 +9,7 @@ namespace WordPressVIPMinimum\Sniffs\Security; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\Utils\TextStrings; use WordPressVIPMinimum\Sniffs\Sniff; /** @@ -72,7 +73,7 @@ public function process_token( $stackPtr ) { /* * Ignore Gruntfile.js files as they are configuration, not code. */ - $file_name = $this->strip_quotes( $this->phpcsFile->getFileName() ); + $file_name = TextStrings::stripQuotes( $this->phpcsFile->getFileName() ); $file_name = strtolower( basename( $file_name ) ); if ( $file_name === 'gruntfile.js' ) { @@ -120,7 +121,7 @@ public function process_token( $stackPtr ) { return; } - $content = $this->strip_quotes( $this->tokens[ $stackPtr ]['content'] ); + $content = TextStrings::stripQuotes( $this->tokens[ $stackPtr ]['content'] ); $match_count = preg_match_all( self::UNESCAPED_INTERPOLATE_REGEX, $content, $matches ); if ( $match_count > 0 ) { diff --git a/WordPressVIPMinimum/Sniffs/UserExperience/AdminBarRemovalSniff.php b/WordPressVIPMinimum/Sniffs/UserExperience/AdminBarRemovalSniff.php index a029134b..77ddf63a 100644 --- a/WordPressVIPMinimum/Sniffs/UserExperience/AdminBarRemovalSniff.php +++ b/WordPressVIPMinimum/Sniffs/UserExperience/AdminBarRemovalSniff.php @@ -9,6 +9,7 @@ namespace WordPressVIPMinimum\Sniffs\UserExperience; +use PHPCSUtils\Utils\TextStrings; use WordPressCS\WordPress\AbstractFunctionParameterSniff; use PHP_CodeSniffer\Util\Tokens; @@ -207,13 +208,13 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p break; case 'add_filter': - $filter_name = $this->strip_quotes( $parameters[1]['raw'] ); + $filter_name = TextStrings::stripQuotes( $parameters[1]['raw'] ); if ( $filter_name !== 'show_admin_bar' ) { break; } $error = true; - if ( $this->remove_only === true && isset( $parameters[2]['raw'] ) && $this->strip_quotes( $parameters[2]['raw'] ) === '__return_true' ) { + if ( $this->remove_only === true && isset( $parameters[2]['raw'] ) && TextStrings::stripQuotes( $parameters[2]['raw'] ) === '__return_true' ) { $error = false; } break; From 0be82b9c5d0708259fd54ae41cc8e64ee2c60e5f Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:16:36 +0000 Subject: [PATCH 3/5] Sniff::find_array_open_close(): PHPCSUtils version --- .../Sniffs/Hooks/AlwaysReturnInFilterSniff.php | 5 +++-- WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php b/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php index 907ab5df..4c02d878 100644 --- a/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php +++ b/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php @@ -7,8 +7,9 @@ namespace WordPressVIPMinimum\Sniffs\Hooks; -use WordPressVIPMinimum\Sniffs\Sniff; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\Utils\Arrays; +use WordPressVIPMinimum\Sniffs\Sniff; /** * This sniff validates that filters always return a value @@ -94,7 +95,7 @@ public function process_token( $stackPtr ) { */ private function processArray( $stackPtr ) { - $open_close = $this->find_array_open_close( $stackPtr ); + $open_close = Arrays::getOpenClose( $this->phpcsFile, $stackPtr ); if ( $open_close === false ) { return; } diff --git a/WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php b/WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php index 75b54729..c34030c0 100644 --- a/WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php +++ b/WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php @@ -7,8 +7,9 @@ namespace WordPressVIPMinimum\Sniffs\Hooks; -use WordPressVIPMinimum\Sniffs\Sniff; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\Utils\Arrays; +use WordPressVIPMinimum\Sniffs\Sniff; /** * This sniff validates a proper usage of pre_get_posts action callback. @@ -95,7 +96,7 @@ public function process_token( $stackPtr ) { */ private function processArray( $stackPtr ) { - $open_close = $this->find_array_open_close( $stackPtr ); + $open_close = Arrays::getOpenClose( $this->phpcsFile, $stackPtr ); if ( $open_close === false ) { return; } From b977416abf7cb3a6296870bab56a35afa8b61d97 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:20:47 +0000 Subject: [PATCH 4/5] Sniff::get_function_call_parameter(): PHPCSUtils --- WordPressVIPMinimum/Sniffs/Constants/ConstantStringSniff.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Constants/ConstantStringSniff.php b/WordPressVIPMinimum/Sniffs/Constants/ConstantStringSniff.php index 1cb40e9b..d0a2fb61 100644 --- a/WordPressVIPMinimum/Sniffs/Constants/ConstantStringSniff.php +++ b/WordPressVIPMinimum/Sniffs/Constants/ConstantStringSniff.php @@ -8,8 +8,9 @@ namespace WordPressVIPMinimum\Sniffs\Constants; -use WordPressVIPMinimum\Sniffs\Sniff; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\Utils\PassedParameters; +use WordPressVIPMinimum\Sniffs\Sniff; /** * Sniff for properly using constant name when checking whether a constant is defined. @@ -55,7 +56,7 @@ public function process_token( $stackPtr ) { return; } - $param = $this->get_function_call_parameter( $stackPtr, 1 ); + $param = PassedParameters::getParameter( $this->phpcsFile, $stackPtr, 1, 'constant_name' ); if ( $param === false ) { // Target parameter not found. return; From 7ce4c18c5d161a80835f21356b9bb0c9f38584c3 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:40:24 +0000 Subject: [PATCH 5/5] Use PHPCSUtils MessageHelper::addMessage This is more tested than the WPCS Sniff::addMessage(). --- .../Sniffs/AbstractVariableRestrictionsSniff.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php b/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php index 89234f2e..1f1b04ee 100644 --- a/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php +++ b/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php @@ -9,7 +9,7 @@ namespace WordPressVIPMinimum\Sniffs; -use WordPressVIPMinimum\Sniffs\Sniff; +use PHPCSUtils\Utils\MessageHelper; /** * Restricts usage of some variables. @@ -200,11 +200,13 @@ public function process_token( $stackPtr ) { continue; } - $this->addMessage( + $code = MessageHelper::stringToErrorcode( $groupName . '_' . $match[1] ); + MessageHelper::addMessage( + $this->phpcsFile, $group['message'], $stackPtr, $group['type'] === 'error', - $this->string_to_errorcode( $groupName . '_' . $match[1] ), + $code, [ $var ] );