From cd7e371a857ba0bc3d33165f681e9886e57a8aa7 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Mon, 19 Dec 2022 17:57:21 +0000 Subject: [PATCH 01/23] Composer: up the minimum PHPCS version to 3.7.1 Add support for PHP 8.1 features, including fixing a retokenization of reserved keywords bug in PHPCS 3.7.0. --- .github/workflows/quicktest.yml | 6 +----- .github/workflows/test.yml | 2 +- README.md | 2 +- composer.json | 2 +- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/.github/workflows/quicktest.yml b/.github/workflows/quicktest.yml index 3de35f83..38336154 100644 --- a/.github/workflows/quicktest.yml +++ b/.github/workflows/quicktest.yml @@ -30,16 +30,12 @@ jobs: phpcs_version: 'dev-master' wpcs_version: '2.3.*' - php: '5.4' - phpcs_version: '3.5.5' + phpcs_version: '3.7.1' wpcs_version: '2.3.*' - php: 'latest' phpcs_version: 'dev-master' wpcs_version: '2.3.*' - - php: 'latest' - # PHPCS 3.6.1 is the lowest version of PHPCS which supports PHP 8.1. - phpcs_version: '3.6.1' - wpcs_version: '2.3.*' name: "QTest${{ matrix.phpcs_version == 'dev-master' && ' + Lint' || '' }}: PHP ${{ matrix.php }} - PHPCS ${{ matrix.phpcs_version }}" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0e4cb6cf..4ac024be 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -71,7 +71,7 @@ jobs: # - experimental: Whether the build is "allowed to fail". matrix: php: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4'] - phpcs_version: ['3.5.5', 'dev-master'] + phpcs_version: ['3.7.1', 'dev-master'] wpcs_version: ['2.3.*'] experimental: [false] diff --git a/README.md b/README.md index a52db246..f2049fbd 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Go to https://docs.wpvip.com/technical-references/code-review/phpcs-report/ to l ## Minimal requirements * PHP 5.4+ -* [PHPCS 3.5.5+](https://github.com/squizlabs/PHP_CodeSniffer/releases) +* [PHPCS 3.7.1+](https://github.com/squizlabs/PHP_CodeSniffer/releases) * [WPCS 2.3.0+](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/releases) * [VariableAnalysis 2.11.1+](https://github.com/sirbrillig/phpcs-variable-analysis/releases) diff --git a/composer.json b/composer.json index bab249fe..e3ada30f 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "php": ">=5.4", "dealerdirect/phpcodesniffer-composer-installer": "^0.4.1 || ^0.5 || ^0.6.2 || ^0.7", "sirbrillig/phpcs-variable-analysis": "^2.11.1", - "squizlabs/php_codesniffer": "^3.5.5", + "squizlabs/php_codesniffer": "^3.7.1", "wp-coding-standards/wpcs": "^2.3" }, "require-dev": { From a3acf351287c009ebdeac58b4a731d5a4b6d09aa Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 00:47:53 +0000 Subject: [PATCH 02/23] Composer: rename scripts Brings them more into line with the Composer scripts in WPCS. --- composer.json | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index e3ada30f..c121771a 100644 --- a/composer.json +++ b/composer.json @@ -35,25 +35,27 @@ } }, "scripts": { - "install-codestandards": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run", - "ruleset": "bin/ruleset-tests", - "lint": [ - "bin/php-lint", - "bin/xml-lint" + "check-all": [ + "@lint", + "@check-cs", + "@run-tests", + "@check-complete-strict", + "@ruleset" ], - "phpcs": "bin/phpcs", - "phpunit": "bin/unit-tests", - "coverage": "bin/unit-tests-coverage", "check-complete": [ "@php ./vendor/phpcsstandards/phpcsdevtools/bin/phpcs-check-feature-completeness -q ./WordPressVIPMinimum" ], - "test": [ - "@lint", - "@ruleset", - "@phpunit", - "@phpcs", - "@check-complete" - ] + "check-complete-strict": [ + "@php ./vendor/phpcsstandards/phpcsdevtools/bin/phpcs-check-feature-completeness ./WordPressVIPMinimum" + ], + "check-cs": "bin/phpcs", + "lint": [ + "bin/php-lint", + "bin/xml-lint" + ], + "ruleset": "bin/ruleset-tests", + "run-tests": "bin/unit-tests", + "run-tests-coverage": "bin/unit-tests-coverage" }, "support": { "issues": "https://github.com/Automattic/VIP-Coding-Standards/issues", From b4aa62850f8d2afe9f746b1ba88ec4ca9d7ead32 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 00:49:47 +0000 Subject: [PATCH 03/23] Composer: add script descriptions --- composer.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/composer.json b/composer.json index c121771a..a464c796 100644 --- a/composer.json +++ b/composer.json @@ -57,6 +57,14 @@ "run-tests": "bin/unit-tests", "run-tests-coverage": "bin/unit-tests-coverage" }, + "scripts-descriptions": { + "check-all": "Run all checks (lint, phpcs, feature completeness) and tests.", + "check-complete": "Check if all the sniffs have tests.", + "check-complete-strict": "Check if all the sniffs have unit tests and XML documentation.", + "check-cs": "Run the PHPCS script against the entire codebase.", + "lint": "Lint PHP files against parse errors.", + "run-tests": "Run all the unit tests for the VIP Coding Standards sniffs." + }, "support": { "issues": "https://github.com/Automattic/VIP-Coding-Standards/issues", "wiki": "https://github.com/Automattic/VIP-Coding-Standards/wiki", From 8fb88055a2d844c299a3e8d9a988a5281157e44f Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 00:53:02 +0000 Subject: [PATCH 04/23] Composer: use develop branch of WPCS VIPCS needs to be compatible with WordPress Coding Standards 3.0.0, which is currently in development on its `develop` branch. WPCS now uses some extra dependencies, which are also in development, so we need to allow for a minimum-stability of dev. Run `composer update -W` to pull those extra dependencies in. --- composer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a464c796..ebce3726 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "dealerdirect/phpcodesniffer-composer-installer": "^0.4.1 || ^0.5 || ^0.6.2 || ^0.7", "sirbrillig/phpcs-variable-analysis": "^2.11.1", "squizlabs/php_codesniffer": "^3.7.1", - "wp-coding-standards/wpcs": "^2.3" + "wp-coding-standards/wpcs": "dev-develop" }, "require-dev": { "php-parallel-lint/php-parallel-lint": "^1.3.2", @@ -29,6 +29,8 @@ "phpcsstandards/phpcsdevtools": "^1.0", "phpunit/phpunit": "^4 || ^5 || ^6 || ^7" }, + "minimum-stability": "dev", + "prefer-stable": true, "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true From 5a82c31b6077292b071f5ed8763cc1d1eb3a0186 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 00:54:48 +0000 Subject: [PATCH 05/23] Composer: Normalize the order Uses the composer-normalize plugin to keep a consistent order. --- composer.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index ebce3726..913a301d 100644 --- a/composer.json +++ b/composer.json @@ -1,20 +1,25 @@ { "name": "automattic/vipwpcs", - "type": "phpcodesniffer-standard", "description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress VIP minimum coding conventions", + "license": "MIT", + "type": "phpcodesniffer-standard", "keywords": [ "phpcs", "static analysis", "standards", "WordPress" ], - "license": "MIT", "authors": [ { "name": "Contributors", "homepage": "https://github.com/Automattic/VIP-Coding-Standards/graphs/contributors" } ], + "support": { + "issues": "https://github.com/Automattic/VIP-Coding-Standards/issues", + "wiki": "https://github.com/Automattic/VIP-Coding-Standards/wiki", + "source": "https://github.com/Automattic/VIP-Coding-Standards" + }, "require": { "php": ">=5.4", "dealerdirect/phpcodesniffer-composer-installer": "^0.4.1 || ^0.5 || ^0.6.2 || ^0.7", @@ -23,8 +28,8 @@ "wp-coding-standards/wpcs": "dev-develop" }, "require-dev": { - "php-parallel-lint/php-parallel-lint": "^1.3.2", "php-parallel-lint/php-console-highlighter": "^1.0.0", + "php-parallel-lint/php-parallel-lint": "^1.3.2", "phpcompatibility/php-compatibility": "^9", "phpcsstandards/phpcsdevtools": "^1.0", "phpunit/phpunit": "^4 || ^5 || ^6 || ^7" @@ -66,10 +71,5 @@ "check-cs": "Run the PHPCS script against the entire codebase.", "lint": "Lint PHP files against parse errors.", "run-tests": "Run all the unit tests for the VIP Coding Standards sniffs." - }, - "support": { - "issues": "https://github.com/Automattic/VIP-Coding-Standards/issues", - "wiki": "https://github.com/Automattic/VIP-Coding-Standards/wiki", - "source": "https://github.com/Automattic/VIP-Coding-Standards" } } From 2b893cad8485484818334542a116d8c7390d8c90 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:00:30 +0000 Subject: [PATCH 06/23] CS: Use pre-increment over post-increment --- .../Sniffs/Classes/DeclarationCompatibilitySniff.php | 2 +- .../Sniffs/Hooks/AlwaysReturnInFilterSniff.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php b/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php index 6b294807..dea566e2 100644 --- a/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php +++ b/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php @@ -291,7 +291,7 @@ protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currSco return; } } - $i++; + ++$i; } } diff --git a/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php b/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php index 8029e732..907ab5df 100644 --- a/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php +++ b/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php @@ -218,9 +218,9 @@ private function processFunctionBody( $stackPtr ) { while ( $returnTokenPtr ) { if ( $this->isInsideIfConditonal( $returnTokenPtr ) ) { - $insideIfConditionalReturn++; + ++$insideIfConditionalReturn; } else { - $outsideConditionalReturn++; + ++$outsideConditionalReturn; } if ( $this->isReturningVoid( $returnTokenPtr ) ) { $message = 'Please, make sure that a callback to `%s` filter is returning void intentionally.'; From 0b84ea86729f34b2b56103c6a7fe4acbce4f60db Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:01:47 +0000 Subject: [PATCH 07/23] CS: Avoid reserved keyword for function param name --- WordPressVIPMinimum/Sniffs/Compatibility/ZoninatorSniff.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Compatibility/ZoninatorSniff.php b/WordPressVIPMinimum/Sniffs/Compatibility/ZoninatorSniff.php index 1dae2b96..b773c79b 100644 --- a/WordPressVIPMinimum/Sniffs/Compatibility/ZoninatorSniff.php +++ b/WordPressVIPMinimum/Sniffs/Compatibility/ZoninatorSniff.php @@ -80,11 +80,11 @@ public function process_token( $stackPtr ) { /** * Removes the quotation marks around T_CONSTANT_ENCAPSED_STRING. * - * @param string $string T_CONSTANT_ENCAPSED_STRING containing wrapping quotation marks. + * @param string $text_string T_CONSTANT_ENCAPSED_STRING containing wrapping quotation marks. * * @return string String w/o wrapping quotation marks. */ - public function remove_wrapping_quotation_marks( $string ) { - return trim( str_replace( '"', "'", $string ), "'" ); + public function remove_wrapping_quotation_marks( $text_string ) { + return trim( str_replace( '"', "'", $text_string ), "'" ); } } From b8382196b1ab684d78ed768a346cdd585545e987 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:06:01 +0000 Subject: [PATCH 08/23] CS: Update WordPress-Extra exclusions WordPress-Extra will no longer use the `Generic.Arrays.DisallowShortArraySyntax` rule, but does use the improved `Universal.Arrays.DisallowShortArraySyntax` from PHPCSExtra instead, so whilst we have short array syntax being used in the VIPCS sniffs, let's continue to exclude the rule. --- .phpcs.xml.dist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index 424f4095..a0049b7b 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -19,7 +19,7 @@ - + From 1f6db81d56e64968bc69defb2109a23fa23ae2d2 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:13:33 +0000 Subject: [PATCH 09/23] 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 9b9513f0..1e61b25b 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. @@ -182,7 +183,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 6ea36135..0118184c 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 6b985f07..094cd702 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 9e6b16ec4219a770573d95b4ddace5633337d57f Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:16:36 +0000 Subject: [PATCH 10/23] 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 90a4658b9d35cc10a5e0249cc643889e5887d3ef Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:20:47 +0000 Subject: [PATCH 11/23] 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 d6506c8f..9656d188 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 ); if ( $param === false ) { // Target parameter not found. return; From e948dcb59db513cf94dddaf883a5c9dba7c99aff Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:29:56 +0000 Subject: [PATCH 12/23] Add Sniff::merge_custom_array() The previous `Sniff::merge_custom_array()` function was moved to a new Helper class in WPCS: https://github.com/WordPress/WordPress-Coding-Standards/pull/2157. However, it is marked as non-public (only meant for WPCS directly), so copying into the VIPCS sniff is easiest for now. --- .../AbstractVariableRestrictionsSniff.php | 2 +- WordPressVIPMinimum/Sniffs/Sniff.php | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php b/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php index e46373a3..dbba0a14 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. diff --git a/WordPressVIPMinimum/Sniffs/Sniff.php b/WordPressVIPMinimum/Sniffs/Sniff.php index dfad0381..315ee227 100644 --- a/WordPressVIPMinimum/Sniffs/Sniff.php +++ b/WordPressVIPMinimum/Sniffs/Sniff.php @@ -17,4 +17,45 @@ * @package VIPCS\WordPressVIPMinimum */ abstract class Sniff extends \WordPressCS\WordPress\Sniff { + /** + * Merge a pre-set array with a ruleset-provided array. + * + * - By default flips custom lists to allow for using `isset()` instead + * of `in_array()`. + * - When `$flip` is true: + * * Presumes the base array is in a `'value' => true` format. + * * Any custom items will be given the value `false` to be able to + * distinguish them from pre-set (base array) values. + * * Will filter previously added custom items out from the base array + * before merging/returning to allow for resetting to the base array. + * + * {@internal Function is static as it doesn't use any of the properties or others + * methods anyway.} + * + * @param array $custom Custom list as provided via a ruleset. + * @param array $base Optional. Base list. Defaults to an empty array. + * Expects `value => true` format when `$flip` is true. + * @param bool $flip Optional. Whether or not to flip the custom list. + * Defaults to true. + * @return array + */ + protected static function merge_custom_array( $custom, $base = array(), $flip = true ) { + if ( true === $flip ) { + $base = array_filter( $base ); + } + + if ( empty( $custom ) || ! \is_array( $custom ) ) { + return $base; + } + + if ( true === $flip ) { + $custom = array_fill_keys( $custom, false ); + } + + if ( empty( $base ) ) { + return $custom; + } + + return array_merge( $base, $custom ); + } } From 2bb34af9698fad49feb97367d0d087a0fef3acae Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 01:40:24 +0000 Subject: [PATCH 13/23] Use PHPCSUtils MessageHelper::addMessage This is more tested than the WPCS Sniff::addMessage(). --- .../Sniffs/AbstractVariableRestrictionsSniff.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php b/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php index dbba0a14..e9d4f731 100644 --- a/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php +++ b/WordPressVIPMinimum/Sniffs/AbstractVariableRestrictionsSniff.php @@ -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 03e7924da79d51579d96b59f9dbf36a3ce8c70ea Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 02:42:09 +0000 Subject: [PATCH 14/23] CS: fix coding standards for merge_custom_array() --- WordPressVIPMinimum/Sniffs/Sniff.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Sniff.php b/WordPressVIPMinimum/Sniffs/Sniff.php index 315ee227..68d8ea55 100644 --- a/WordPressVIPMinimum/Sniffs/Sniff.php +++ b/WordPressVIPMinimum/Sniffs/Sniff.php @@ -39,8 +39,8 @@ abstract class Sniff extends \WordPressCS\WordPress\Sniff { * Defaults to true. * @return array */ - protected static function merge_custom_array( $custom, $base = array(), $flip = true ) { - if ( true === $flip ) { + public static function merge_custom_array( $custom, $base = [], $flip = true ) { + if ( $flip === true ) { $base = array_filter( $base ); } @@ -48,7 +48,7 @@ protected static function merge_custom_array( $custom, $base = array(), $flip = return $base; } - if ( true === $flip ) { + if ( $flip === true ) { $custom = array_fill_keys( $custom, false ); } From 80ca8abcda6ef7d8a6a87501a704bcfcbd91601e Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 02:51:06 +0000 Subject: [PATCH 15/23] 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 913a301d..51af8448 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "require": { "php": ">=5.4", "dealerdirect/phpcodesniffer-composer-installer": "^0.4.1 || ^0.5 || ^0.6.2 || ^0.7", + "phpcsstandards/phpcsutils": "^1.0", "sirbrillig/phpcs-variable-analysis": "^2.11.1", "squizlabs/php_codesniffer": "^3.7.1", "wp-coding-standards/wpcs": "dev-develop" From 5408a4642fdc4edc9e284831940869cfb6099193 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 03:30:52 +0000 Subject: [PATCH 16/23] CI: Refresh unit tests workflow --- .../workflows/{test.yml => unit-tests.yml} | 85 +++++++------------ 1 file changed, 31 insertions(+), 54 deletions(-) rename .github/workflows/{test.yml => unit-tests.yml} (61%) diff --git a/.github/workflows/test.yml b/.github/workflows/unit-tests.yml similarity index 61% rename from .github/workflows/test.yml rename to .github/workflows/unit-tests.yml index 4ac024be..324f243b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/unit-tests.yml @@ -1,4 +1,4 @@ -name: Test +name: Unit Tests on: # Run on pushes to `master` and on all pull requests. @@ -27,10 +27,10 @@ jobs: strategy: matrix: - php: ['5.4', 'latest', '8.2'] + php: ['5.4', 'latest', '8.3'] name: "Lint: PHP ${{ matrix.php }}" - continue-on-error: ${{ matrix.php == '8.2' }} + continue-on-error: ${{ matrix.php == '8.3' }} steps: - name: Checkout code @@ -46,8 +46,8 @@ jobs: - name: Install Composer dependencies uses: "ramsey/composer-install@v2" with: - # Bust the cache at least once a month - output format: YYYY-MM-DD. - custom-cache-suffix: $(date -u -d "-0 month -$(($(date +%d)-1)) days" "+%F") + # Bust the cache at least once a month - output format: YYYY-MM. + custom-cache-suffix: $(date -u "+%Y-%m") - name: Lint against parse errors run: ./bin/php-lint --checkstyle | cs2pr @@ -70,41 +70,19 @@ jobs: # no additional versions are included in the array. # - experimental: Whether the build is "allowed to fail". matrix: - php: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4'] + php: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] phpcs_version: ['3.7.1', 'dev-master'] - wpcs_version: ['2.3.*'] + wpcs_version: ['dev-develop'] experimental: [false] include: - # Complete the matrix by adding PHP 8.0, but only test against compatible PHPCS versions. - - php: '8.0' - phpcs_version: 'dev-master' - wpcs_version: '2.3.*' - experimental: false - - php: '8.0' - # PHPCS 3.5.7 is the lowest version of PHPCS which supports PHP 8.0. - phpcs_version: '3.5.7' - wpcs_version: '2.3.*' - experimental: false - - # Complete the matrix by adding PHP 8.1, but only test against compatible PHPCS versions. - - php: '8.1' - phpcs_version: 'dev-master' - wpcs_version: '2.3.*' - experimental: false - - php: '8.1' - # PHPCS 3.6.1 is the lowest version of PHPCS which supports PHP 8.1. - phpcs_version: '3.6.1' - wpcs_version: '2.3.*' - experimental: false - # Experimental builds. These are allowed to fail. - #- php: '8.2' - # phpcs_version: 'dev-master' - # wpcs_version: '2.3.*' - # experimental: true + - php: '8.3' + phpcs_version: 'dev-master' + wpcs_version: 'dev-develop' + experimental: true - name: "Test: PHP ${{ matrix.php }} - PHPCS ${{ matrix.phpcs_version }} - WPCS ${{ matrix.wpcs_version }}" + name: "PHP ${{ matrix.php }} on PHPCS ${{ matrix.phpcs_version }}" continue-on-error: ${{ matrix.experimental }} @@ -119,14 +97,12 @@ jobs: id: set_ini run: | if [[ "${{ matrix.phpcs_version }}" != "dev-master" ]]; then - echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED' >> $GITHUB_OUTPUT - elif [[ "${{ matrix.php }}" == "8.1" ]]; then - echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED' >> $GITHUB_OUTPUT + echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On' >> $GITHUB_OUTPUT else - echo 'PHP_INI=error_reporting=-1' >> $GITHUB_OUTPUT + echo 'PHP_INI=error_reporting=-1, display_errors=On' >> $GITHUB_OUTPUT fi - - name: Install PHP + - name: Set up PHP uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} @@ -135,33 +111,34 @@ jobs: - name: 'Composer: set PHPCS and WPCS versions for tests' run: | - composer require --no-update --no-scripts squizlabs/php_codesniffer:"${{ matrix.phpcs_version }}" --no-interaction - composer require --no-update --no-scripts wp-coding-standards/wpcs:"${{ matrix.wpcs_version }}" --no-interaction + composer require squizlabs/php_codesniffer:"${{ matrix.phpcs_version }}" --no-update --no-scripts --no-interaction + composer require wp-coding-standards/wpcs:"${{ matrix.wpcs_version }}" --no-update --no-scripts --no-interaction # Install dependencies and handle caching in one go. # @link https://github.com/marketplace/actions/install-composer-dependencies - - name: Install Composer dependencies - normal - if: ${{ startsWith( matrix.php, '8' ) == false }} - uses: "ramsey/composer-install@v2" + - name: Install Composer dependencies (PHP < 8.0 ) + if: ${{ matrix.php < 8.0 }} + uses: ramsey/composer-install@v2 with: - # Bust the cache at least once a month - output format: YYYY-MM-DD. - custom-cache-suffix: $(date -u -d "-0 month -$(($(date +%d)-1)) days" "+%F") + # Bust the cache at least once a month - output format: YYYY-MM. + custom-cache-suffix: $(date -u "+%Y-%m") # PHPUnit 7.x does not allow for installation on PHP 8, so ignore platform # requirements to get PHPUnit 7.x to install on nightly. - - name: Install Composer dependencies - with ignore platform - if: ${{ startsWith( matrix.php, '8' ) }} - uses: "ramsey/composer-install@v2" + - name: Install Composer dependencies (PHP >= 8.0) + if: ${{ matrix.php >= 8.0 }} + uses: ramsey/composer-install@v2 with: composer-options: --ignore-platform-reqs - custom-cache-suffix: $(date -u -d "-0 month -$(($(date +%d)-1)) days" "+%F") + custom-cache-suffix: $(date -u "+%Y-%m") - - name: Run the unit tests - PHP 5.4 - 8.0 - if: matrix.php < '8.1' + # Simplify these next two down once bin/unit-tests.yml contains the condition (#735). + - name: Run the unit tests - PHP 5.4 — 8.0 + if: ${{ matrix.php < '8.1' }} run: ./bin/unit-tests - - name: Run the unit tests - PHP > 8.1 - if: matrix.php >= '8.1' + - name: Run the unit tests - PHP >= 8.1 + if: ${{ matrix.php >= '8.1' }} run: vendor/bin/phpunit --filter WordPressVIPMinimum ./vendor/squizlabs/php_codesniffer/tests/AllTests.php --no-coverage --no-configuration --bootstrap=./tests/bootstrap.php --dont-report-useless-tests - name: Run the ruleset tests From 8e8792dd762c72b8911cc88c50d3a958926c68d4 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 03:35:25 +0000 Subject: [PATCH 17/23] Disable ruleset test Shows up empty in Ci and locally. --- .github/workflows/unit-tests.yml | 4 ++-- composer.json | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 324f243b..5cab32c6 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -141,5 +141,5 @@ jobs: if: ${{ matrix.php >= '8.1' }} run: vendor/bin/phpunit --filter WordPressVIPMinimum ./vendor/squizlabs/php_codesniffer/tests/AllTests.php --no-coverage --no-configuration --bootstrap=./tests/bootstrap.php --dont-report-useless-tests - - name: Run the ruleset tests - run: ./bin/ruleset-tests + #- name: Run the ruleset tests + # run: ./bin/ruleset-tests diff --git a/composer.json b/composer.json index 51af8448..9d706902 100644 --- a/composer.json +++ b/composer.json @@ -47,8 +47,7 @@ "@lint", "@check-cs", "@run-tests", - "@check-complete-strict", - "@ruleset" + "@check-complete-strict" ], "check-complete": [ "@php ./vendor/phpcsstandards/phpcsdevtools/bin/phpcs-check-feature-completeness -q ./WordPressVIPMinimum" From 5ae2e2309459a2e480b0c945907feacea2a4937e Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 11:24:08 +0000 Subject: [PATCH 18/23] Ruleset tests: update for WPCS 3.0 The ruleset tests were failing mainly due to https://github.com/WordPress/WordPress-Coding-Standards/pull/2108 which renamed some violation codes that we were excluding. The other change to account for was the replacement of a WPCS StrictComparisons sniff with a PHPCSExtras Universal sniff. The sniff in PHPCSExtra contains a fixer. As this is a risky fixer, this fixer is turned off for WPCS. The sniff in PHPCSExtra will provide metrics about loose versus strict comparisons. --- WordPress-VIP-Go/ruleset-test.inc | 4 ++-- WordPress-VIP-Go/ruleset.xml | 8 ++++---- WordPressVIPMinimum/ruleset-test.inc | 4 ++-- WordPressVIPMinimum/ruleset.xml | 30 +++++++++++++++++++++++++--- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/WordPress-VIP-Go/ruleset-test.inc b/WordPress-VIP-Go/ruleset-test.inc index 61e56955..c4037d25 100644 --- a/WordPress-VIP-Go/ruleset-test.inc +++ b/WordPress-VIP-Go/ruleset-test.inc @@ -165,7 +165,7 @@ rawurlencode(); // Ok. extract( array( 'a' => 1 ) ); // Error. $obj->extract(); // Ok. -// WordPress.PHP.StrictComparisons.LooseComparison +// Universal.Operators.StrictComparisons.LooseComparison true == $true; // Warning. false === $true; // Ok. @@ -546,7 +546,7 @@ str_replace( 'foo', array( 'bar', 'foo' ), 'foobar' ); // Error. // WordPressVIPMinimum.Security.Underscorejs echo ""; diff --git a/WordPress-VIP-Go/ruleset.xml b/WordPress-VIP-Go/ruleset.xml index 58386a58..7626cf76 100644 --- a/WordPress-VIP-Go/ruleset.xml +++ b/WordPress-VIP-Go/ruleset.xml @@ -109,7 +109,7 @@ This includes potential security holes as well as functions that may bring down sites for performance reasons. --> - + File system operations only work on the `/tmp/` and `wp-content/uploads/` directories. To avoid unexpected results, please use helper functions like `get_temp_dir()` or `wp_get_upload_dir()` to get the proper directory path when using functions such as %s(). For more details, please see: https://docs.wpvip.com/technical-references/vip-go-files-system/local-file-operations/ @@ -193,7 +193,7 @@ 3 - + 3 @@ -263,10 +263,10 @@ 0 - + 0 - + 0 diff --git a/WordPressVIPMinimum/ruleset-test.inc b/WordPressVIPMinimum/ruleset-test.inc index 803e890f..ead31c64 100644 --- a/WordPressVIPMinimum/ruleset-test.inc +++ b/WordPressVIPMinimum/ruleset-test.inc @@ -72,7 +72,7 @@ new WP_Query( array( // WordPress.WP.GlobalVariablesOverride $GLOBALS['wpdb'] = 'test'; // Error. -// WordPress.PHP.StrictComparisons +// Universal.Operators.StrictComparisons if ( true == $true ) { // Warning. } @@ -555,7 +555,7 @@ str_replace( 'foo', array( 'bar', 'foo' ), 'foobar' ); // Error. // WordPressVIPMinimum.Security.Underscorejs echo ""; diff --git a/WordPressVIPMinimum/ruleset.xml b/WordPressVIPMinimum/ruleset.xml index 2ce2511c..1e968ef9 100644 --- a/WordPressVIPMinimum/ruleset.xml +++ b/WordPressVIPMinimum/ruleset.xml @@ -26,7 +26,9 @@ - + + warning + @@ -123,9 +125,31 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + From 92e10d3b3cfaeeca1dfc1f3c0c0de57ad2b03ce8 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 11:31:55 +0000 Subject: [PATCH 19/23] Ruleset tests: Add label before test runs Make it clear which ruleset being tested, even on a test failure. --- tests/RulesetTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/RulesetTest.php b/tests/RulesetTest.php index d67a3454..0aa69639 100644 --- a/tests/RulesetTest.php +++ b/tests/RulesetTest.php @@ -98,6 +98,9 @@ public function __construct( $ruleset, $expected = [] ) { $this->phpcs_bin = realpath( $phpcs_bin ); } + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + printf( 'Testing ' . $this->ruleset . ' ruleset.' . PHP_EOL ); + $output = $this->collect_phpcs_result(); if ( ! is_object( $output ) || empty( $output ) ) { @@ -148,6 +151,7 @@ private function collect_phpcs_result() { $this->phpcs_bin, $this->ruleset ); + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_shell_exec -- This is test code, not production. $output = shell_exec( $shell ); From d6df86daf295e1ac635b2c3b3c603135b3dcaf5b Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 11:32:43 +0000 Subject: [PATCH 20/23] Enable ruleset tests --- .github/workflows/unit-tests.yml | 4 ++-- composer.json | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 5cab32c6..324f243b 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -141,5 +141,5 @@ jobs: if: ${{ matrix.php >= '8.1' }} run: vendor/bin/phpunit --filter WordPressVIPMinimum ./vendor/squizlabs/php_codesniffer/tests/AllTests.php --no-coverage --no-configuration --bootstrap=./tests/bootstrap.php --dont-report-useless-tests - #- name: Run the ruleset tests - # run: ./bin/ruleset-tests + - name: Run the ruleset tests + run: ./bin/ruleset-tests diff --git a/composer.json b/composer.json index 9d706902..25bf2590 100644 --- a/composer.json +++ b/composer.json @@ -47,7 +47,8 @@ "@lint", "@check-cs", "@run-tests", - "@check-complete-strict" + "@run-ruleset-tests", + "@check-complete" ], "check-complete": [ "@php ./vendor/phpcsstandards/phpcsdevtools/bin/phpcs-check-feature-completeness -q ./WordPressVIPMinimum" @@ -60,7 +61,7 @@ "bin/php-lint", "bin/xml-lint" ], - "ruleset": "bin/ruleset-tests", + "run-ruleset-tests": "bin/ruleset-tests", "run-tests": "bin/unit-tests", "run-tests-coverage": "bin/unit-tests-coverage" }, From 1b968b77af2eace00e5d56b037b03e29c36efda7 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 11:48:45 +0000 Subject: [PATCH 21/23] Ruleset tests: Update label --- tests/RulesetTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/RulesetTest.php b/tests/RulesetTest.php index 0aa69639..0a9de437 100644 --- a/tests/RulesetTest.php +++ b/tests/RulesetTest.php @@ -99,7 +99,7 @@ public function __construct( $ruleset, $expected = [] ) { } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - printf( 'Testing ' . $this->ruleset . ' ruleset.' . PHP_EOL ); + printf( 'Testing the ' . $this->ruleset . ' ruleset.' . PHP_EOL ); $output = $this->collect_phpcs_result(); @@ -147,7 +147,7 @@ private function collect_phpcs_result() { $shell = sprintf( '%1$s%2$s --severity=1 --standard=%3$s --report=json ./%3$s/ruleset-test.inc', - $php, // Current PHP executable if avaiable. + $php, // Current PHP executable if available. $this->phpcs_bin, $this->ruleset ); From 5b33691469c0963f9048cc0091547e3ad6409d97 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 11:56:32 +0000 Subject: [PATCH 22/23] Add PHPCSExtras dependency Validate the PHPCS installed standards in the quick test. --- .github/workflows/quicktest.yml | 3 +++ composer.json | 1 + 2 files changed, 4 insertions(+) diff --git a/.github/workflows/quicktest.yml b/.github/workflows/quicktest.yml index 38336154..a2b8cfbe 100644 --- a/.github/workflows/quicktest.yml +++ b/.github/workflows/quicktest.yml @@ -87,6 +87,9 @@ jobs: composer-options: --ignore-platform-reqs custom-cache-suffix: $(date -u -d "-0 month -$(($(date +%d)-1)) days" "+%F") + - name: Display PHPCS installed standards + run: ./vendor/bin/phpcs -i + - name: Lint against parse errors if: matrix.phpcs_version == 'dev-master' run: ./bin/php-lint diff --git a/composer.json b/composer.json index 25bf2590..b478495b 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "require": { "php": ">=5.4", "dealerdirect/phpcodesniffer-composer-installer": "^0.4.1 || ^0.5 || ^0.6.2 || ^0.7", + "phpcsstandards/phpcsextra": "^1.0", "phpcsstandards/phpcsutils": "^1.0", "sirbrillig/phpcs-variable-analysis": "^2.11.1", "squizlabs/php_codesniffer": "^3.7.1", From ce7d0c46209830bc50acaa5cd60e1312f9fe2737 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Tue, 20 Dec 2022 12:12:37 +0000 Subject: [PATCH 23/23] CI: Use WPCS develop branch for quick test --- .github/workflows/quicktest.yml | 23 +++++++++++------------ .github/workflows/unit-tests.yml | 9 +++++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.github/workflows/quicktest.yml b/.github/workflows/quicktest.yml index a2b8cfbe..e8fd98b2 100644 --- a/.github/workflows/quicktest.yml +++ b/.github/workflows/quicktest.yml @@ -28,14 +28,14 @@ jobs: include: - php: '5.4' phpcs_version: 'dev-master' - wpcs_version: '2.3.*' + wpcs_version: 'dev-develop' - php: '5.4' phpcs_version: '3.7.1' - wpcs_version: '2.3.*' + wpcs_version: 'dev-develop' - php: 'latest' phpcs_version: 'dev-master' - wpcs_version: '2.3.*' + wpcs_version: 'dev-develop' name: "QTest${{ matrix.phpcs_version == 'dev-master' && ' + Lint' || '' }}: PHP ${{ matrix.php }} - PHPCS ${{ matrix.phpcs_version }}" @@ -50,24 +50,23 @@ jobs: id: set_ini run: | if [[ "${{ matrix.phpcs_version }}" != "dev-master" ]]; then - echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED' >> $GITHUB_OUTPUT - elif [[ "${{ matrix.php }}" == "latest" ]]; then - echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED' >> $GITHUB_OUTPUT + echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On' >> $GITHUB_OUTPUT else - echo 'PHP_INI=error_reporting=-1' >> $GITHUB_OUTPUT + echo 'PHP_INI=error_reporting=-1, display_errors=On' >> $GITHUB_OUTPUT fi - - name: Install PHP + - name: Set up PHP uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} ini-values: ${{ steps.set_ini.outputs.PHP_INI }} coverage: none - - name: 'Composer: set PHPCS and WPCS versions for tests' - run: | - composer require --no-update --no-scripts squizlabs/php_codesniffer:"${{ matrix.phpcs_version }}" --no-interaction - composer require --no-update --no-scripts wp-coding-standards/wpcs:"${{ matrix.wpcs_version }}" --no-interaction + - name: 'Composer: set PHPCS version for tests' + run: composer require squizlabs/php_codesniffer:"${{ matrix.phpcs_version }}" --no-update --no-scripts --no-interaction + + - name: 'Composer: set WPCS version for tests' + run: composer require wp-coding-standards/wpcs:"${{ matrix.wpcs_version }}" --no-update --no-scripts --no-interaction # Install dependencies and handle caching in one go. # @link https://github.com/marketplace/actions/install-composer-dependencies diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 324f243b..926178e9 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -109,10 +109,11 @@ jobs: ini-values: ${{ steps.set_ini.outputs.PHP_INI }} coverage: none - - name: 'Composer: set PHPCS and WPCS versions for tests' - run: | - composer require squizlabs/php_codesniffer:"${{ matrix.phpcs_version }}" --no-update --no-scripts --no-interaction - composer require wp-coding-standards/wpcs:"${{ matrix.wpcs_version }}" --no-update --no-scripts --no-interaction + - name: 'Composer: set PHPCS version for tests' + run: composer require squizlabs/php_codesniffer:"${{ matrix.phpcs_version }}" --no-update --no-scripts --no-interaction + + - name: 'Composer: set WPCS version for tests' + run: composer require wp-coding-standards/wpcs:"${{ matrix.wpcs_version }}" --no-update --no-scripts --no-interaction # Install dependencies and handle caching in one go. # @link https://github.com/marketplace/actions/install-composer-dependencies