From 99d8e04cea8579ea7757cb693a8ecc33c71e2086 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 22 Aug 2023 10:19:01 +0200 Subject: [PATCH 01/13] Composer: require PHPCSUtils By starting to use PHPCSUtils, we make VIPCS less dependent on WordPressCS. It has brings added benefits of more stable, thoroughly tested utility methods, which all support modern PHP. This adds the dependency and updates relevant documentation. Note: I've not changed the GH Actions workflows to add PHPCSUtils to the matrix (yet). Changes to the workflows will be done in a separate commit. --- .github/CONTRIBUTING.md | 1 + .github/ISSUE_TEMPLATE/bug_report.md | 1 + README.md | 3 ++- composer.json | 2 +- tests/bootstrap.php | 3 ++- 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 42e7700b..f2651a80 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -64,6 +64,7 @@ When you introduce new `public` sniff properties, or your sniff extends a class ### Pre-requisites * VIP Coding Standards * WordPress-Coding-Standards +* PHPCSUtils 1.x * PHP_CodeSniffer 3.x * PHPUnit 4.x, 5.x, 6.x or 7.x diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index f68fd0df..c089b1d2 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -44,6 +44,7 @@ Use `php -v` and `composer show` to get versions. | ------------------------ | ------- | PHP version | x.y.z | PHP_CodeSniffer version | x.y.z +| PHPCSUtils version | x.y.z | VIPCS version | x.y.z | WordPressCS version | x.y.z | VariableAnalysis version | x.y.z diff --git a/README.md b/README.md index 6b5c15da..1bcb6465 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Go to https://docs.wpvip.com/technical-references/code-review/phpcs-report/ to l * PHP 5.4+ * [PHPCS 3.7.1+](https://github.com/squizlabs/PHP_CodeSniffer/releases) +* [PHPCSUtils 1.0.8+](https://github.com/PHPCSStandards/PHPCSUtils) * [WPCS 2.3.0+](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/releases) * [VariableAnalysis 2.11.17+](https://github.com/sirbrillig/phpcs-variable-analysis/releases) @@ -34,7 +35,7 @@ composer g config allow-plugins.dealerdirect/phpcodesniffer-composer-installer t composer g require automattic/vipwpcs ``` -This will install the latest compatible versions of PHPCS, WPCS and VariableAnalysis and register the external standards with PHP_CodeSniffer. +This will install the latest compatible versions of PHPCS, PHPCSUtils, WPCS and VariableAnalysis and register the external standards with PHP_CodeSniffer. Please refer to the [installation instructions for installing PHP_CodeSniffer for WordPress.com VIP](https://docs.wpvip.com/how-tos/code-review/php_codesniffer/) for more details. diff --git a/composer.json b/composer.json index 1fc19981..005944eb 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,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.8", "sirbrillig/phpcs-variable-analysis": "^2.11.17", "squizlabs/php_codesniffer": "^3.7.1", "wp-coding-standards/wpcs": "^2.3" diff --git a/tests/bootstrap.php b/tests/bootstrap.php index bb063fdc..667f58a7 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -48,7 +48,8 @@ If you use Composer, please run `composer install`. Otherwise, make sure you set a `PHPCS_DIR` environment variable in your phpunit.xml file -pointing to the PHPCS directory. +pointing to the PHPCS directory and that PHPCSUtils is included in the `installed_paths` +for that PHPCS install. Please read the contributors guidelines for more information: https://github.com/Automattic/VIP-Coding-Standards/blob/develop/.github/CONTRIBUTING.md From bf95652182276aab29a060c11f171e0d726ea962 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 27 Jul 2020 20:06:22 +0200 Subject: [PATCH 02/13] VIPMinimum ruleset: add PHPCSUtils requirement PHPCSUtils does not contain any sniffs, so adding this rule isn't strictly necessary, but by having the rule in the ruleset anyway, if PHPCSUtils is missing, the user will get a descriptive error message during the loading of the ruleset instead of a fatal "class not found" error once the sniffs start running. Adding this only in the `WordPressVIPMinimum` ruleset is sufficient as the `WordPress-VIP-Go` ruleset includes `WordPressVIPMinimum`. --- WordPressVIPMinimum/ruleset.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/WordPressVIPMinimum/ruleset.xml b/WordPressVIPMinimum/ruleset.xml index ae8347d4..87394301 100644 --- a/WordPressVIPMinimum/ruleset.xml +++ b/WordPressVIPMinimum/ruleset.xml @@ -1,6 +1,16 @@ WordPress VIP Minimum Coding Standards + + + + From f70d6ac99b5b347fd806f73f4c53fddea842f943 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:40:24 +0000 Subject: [PATCH 03/13] AbstractVariableRestrictionsSniff: 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 ] ); From 6101474e001ead783af5c0876db36606145cba82 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:16:36 +0000 Subject: [PATCH 04/13] 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 03cc8a78..5c77ba61 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 @@ -96,7 +97,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 ddcf4fac..09ee720c 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. @@ -97,7 +98,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 08ab57d26d197990efd9f4e8d602e574d3fbb548 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:20:47 +0000 Subject: [PATCH 05/13] 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 9c8d98b4f7eb5e6f331e7d428310b5eef58f2dbe Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:13:33 +0000 Subject: [PATCH 06/13] 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 7bc8ca32..54f2b19c 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; /** @@ -149,7 +150,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 6d95cb34..7830e2db 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; @@ -208,13 +209,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 c00cb4f983013b4637444d83e43627b768f70538 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 22 Aug 2023 14:52:21 +0200 Subject: [PATCH 07/13] Classes/DeclarationCompatibility: use PHPCSUtils to retrieve class/method names --- .../Sniffs/Classes/DeclarationCompatibilitySniff.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php b/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php index 9c76bd25..6f8745f0 100644 --- a/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php +++ b/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php @@ -9,6 +9,8 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\AbstractScopeSniff; +use PHPCSUtils\Utils\FunctionDeclarations; +use PHPCSUtils\Utils\ObjectDeclarations; /** * Class WordPressVIPMinimum_Sniffs_Classes_DeclarationCompatibilitySniff @@ -201,13 +203,13 @@ public function __construct() { */ protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currScope ) { - $className = $phpcsFile->getDeclarationName( $currScope ); + $className = ObjectDeclarations::getName( $phpcsFile, $currScope ); if ( $className !== $this->currentClass ) { $this->currentClass = $className; } - $methodName = $phpcsFile->getDeclarationName( $stackPtr ); + $methodName = FunctionDeclarations::getName( $phpcsFile, $stackPtr ); $parentClassName = $phpcsFile->findExtendedClassName( $currScope ); if ( $parentClassName === false ) { From 51faf2d01b57e02578d9831b2df337dc4fa3de58 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 22 Aug 2023 14:54:06 +0200 Subject: [PATCH 08/13] Classes/DeclarationCompatibility: use PHPCSUtils to retrieve function parameters --- .../Sniffs/Classes/DeclarationCompatibilitySniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php b/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php index 6f8745f0..200facc4 100644 --- a/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php +++ b/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php @@ -244,7 +244,7 @@ protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currSco return; } - $signatureParams = $phpcsFile->getMethodParameters( $stackPtr ); + $signatureParams = FunctionDeclarations::getParameters( $phpcsFile, $stackPtr ); $parentSignature = $this->checkClasses[ $parentClassName ][ $methodName ]; From 8e4c149bdf7b57acdbcd16aa6fdb75024edef4d4 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 22 Aug 2023 14:55:37 +0200 Subject: [PATCH 09/13] Hooks/AlwaysReturnInFilter: use PHPCSUtils to retrieve function declaration info --- WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php b/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php index 5c77ba61..f3544e6b 100644 --- a/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php +++ b/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php @@ -9,6 +9,7 @@ use PHP_CodeSniffer\Util\Tokens; use PHPCSUtils\Utils\Arrays; +use PHPCSUtils\Utils\FunctionDeclarations; use WordPressVIPMinimum\Sniffs\Sniff; /** @@ -186,7 +187,7 @@ private function processFunctionBody( $stackPtr ) { $filterName = $this->tokens[ $this->filterNamePtr ]['content']; - $methodProps = $this->phpcsFile->getMethodProperties( $stackPtr ); + $methodProps = FunctionDeclarations::getProperties( $this->phpcsFile, $stackPtr ); if ( $methodProps['is_abstract'] === true ) { $message = 'The callback for the `%s` filter hook-in points to an abstract method. Please ensure that child class implementations of this method always return a value.'; $data = [ $filterName ]; From 4fdb103b732a3a1024ef11f071f11cbe9877ccc6 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 22 Aug 2023 15:07:38 +0200 Subject: [PATCH 10/13] UserExperience/AdminBarRemoval: use PHPCSUtils for retrieving tokens as string --- .../Sniffs/UserExperience/AdminBarRemovalSniff.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WordPressVIPMinimum/Sniffs/UserExperience/AdminBarRemovalSniff.php b/WordPressVIPMinimum/Sniffs/UserExperience/AdminBarRemovalSniff.php index 7830e2db..20c1d853 100644 --- a/WordPressVIPMinimum/Sniffs/UserExperience/AdminBarRemovalSniff.php +++ b/WordPressVIPMinimum/Sniffs/UserExperience/AdminBarRemovalSniff.php @@ -9,6 +9,7 @@ namespace WordPressVIPMinimum\Sniffs\UserExperience; +use PHPCSUtils\Utils\GetTokensAsString; use PHPCSUtils\Utils\TextStrings; use WordPressCS\WordPress\AbstractFunctionParameterSniff; use PHP_CodeSniffer\Util\Tokens; @@ -344,7 +345,7 @@ protected function process_css_style( $stackPtr ) { } } $start = ( $i + 1 ); - $selector = trim( $this->phpcsFile->getTokensAsString( $start, $opener - $start ) ); + $selector = trim( GetTokensAsString::normal( $this->phpcsFile, $start, ( $opener - 1 ) ) ); unset( $i ); foreach ( $this->target_css_selectors as $target_selector ) { From 4b5b0774d3c6283dc11be3faedabeb7aab319a58 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 22 Aug 2023 15:10:45 +0200 Subject: [PATCH 11/13] AbstractVariableRestrictions: use PHPCSUtils for retrieving tokens as string --- .../Sniffs/AbstractVariableRestrictionsSniff.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php b/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php index 1f1b04ee..61d3ed7d 100644 --- a/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php +++ b/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php @@ -9,6 +9,7 @@ namespace WordPressVIPMinimum\Sniffs; +use PHPCSUtils\Utils\GetTokensAsString; use PHPCSUtils\Utils\MessageHelper; /** @@ -179,7 +180,7 @@ public function process_token( $stackPtr ) { if ( isset( $token['bracket_closer'] ) ) { $owner = $this->phpcsFile->findPrevious( \T_VARIABLE, $stackPtr ); - $inside = $this->phpcsFile->getTokensAsString( $stackPtr, $token['bracket_closer'] - $stackPtr + 1 ); + $inside = GetTokensAsString::normal( $this->phpcsFile, $stackPtr, $token['bracket_closer'] ); $var = implode( '', [ $this->tokens[ $owner ]['content'], $inside ] ); } } From d0e14efef7603ce4fc74a422cc8f1c0f897e8d4e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 22 Aug 2023 15:14:57 +0200 Subject: [PATCH 12/13] Use PHPCSUtils BackCompat for find[Start|End]ofStatement() --- WordPressVIPMinimum/Sniffs/Files/IncludingNonPHPFileSniff.php | 3 ++- .../Sniffs/Security/ProperEscapingFunctionSniff.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Files/IncludingNonPHPFileSniff.php b/WordPressVIPMinimum/Sniffs/Files/IncludingNonPHPFileSniff.php index 5165f36c..09ec5081 100644 --- a/WordPressVIPMinimum/Sniffs/Files/IncludingNonPHPFileSniff.php +++ b/WordPressVIPMinimum/Sniffs/Files/IncludingNonPHPFileSniff.php @@ -9,6 +9,7 @@ use WordPressVIPMinimum\Sniffs\Sniff; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\BackCompat\BCFile; /** * Ensure that non-PHP files are included via `file_get_contents()` instead of using `include/require[_once]`. @@ -59,7 +60,7 @@ public function register() { * @return void */ public function process_token( $stackPtr ) { - $end_of_statement = $this->phpcsFile->findEndOfStatement( $stackPtr ); + $end_of_statement = BCFile::findEndOfStatement( $this->phpcsFile, $stackPtr ); $curStackPtr = ( $end_of_statement + 1 ); do { diff --git a/WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php b/WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php index 5548c865..1341e590 100644 --- a/WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php +++ b/WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php @@ -9,6 +9,7 @@ namespace WordPressVIPMinimum\Sniffs\Security; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\BackCompat\BCFile; use PHPCSUtils\Utils\TextStrings; use WordPressVIPMinimum\Sniffs\Sniff; @@ -179,7 +180,7 @@ public function process_token( $stackPtr ) { if ( $this->in_short_echo !== false ) { $ignore[ T_COMMA ] = T_COMMA; } else { - $start_of_statement = $this->phpcsFile->findStartOfStatement( $stackPtr, T_COMMA ); + $start_of_statement = BCFile::findStartOfStatement( $this->phpcsFile, $stackPtr, T_COMMA ); if ( $this->tokens[ $start_of_statement ]['code'] === T_ECHO ) { $ignore[ T_COMMA ] = T_COMMA; } From 02888e6a8bf66c12f3f594ad97336309d70a7221 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 22 Aug 2023 15:16:25 +0200 Subject: [PATCH 13/13] Classes/DeclarationCompatibility: use PHPCSUtils for retrieving name of extended class --- .../Sniffs/Classes/DeclarationCompatibilitySniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php b/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php index 200facc4..a1e80c97 100644 --- a/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php +++ b/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php @@ -211,7 +211,7 @@ protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currSco $methodName = FunctionDeclarations::getName( $phpcsFile, $stackPtr ); - $parentClassName = $phpcsFile->findExtendedClassName( $currScope ); + $parentClassName = ObjectDeclarations::findExtendedClassName( $phpcsFile, $currScope ); if ( $parentClassName === false ) { // This class does not extend any other class. return;