From dbf537a2334843f120d7799424e5170bacb1a2a0 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 14 Sep 2023 04:12:11 +0200 Subject: [PATCH 1/3] Composer: add PHPCSExtra Add PHPCSExtra as a new dependency. PHPCSExtra is an external standard for PHP_CodeSniffer and is also a requirement for WPCS 3.0.0. The reason to add it as a direct requirement for YoastCS is that we'll not only be using sniffs from PHPCSExtra via WPCS, but also want to use additional sniffs which PHPCSExtra has to offer, which makes it not just a requirement of WPCS, but also a requirement of YoastCS. Ref: https://github.com/PHPCSStandards/PHPCSExtra --- .github/workflows/basics.yml | 1 + README.md | 1 + composer.json | 1 + 3 files changed, 3 insertions(+) diff --git a/.github/workflows/basics.yml b/.github/workflows/basics.yml index 966155b8..2ef7146a 100644 --- a/.github/workflows/basics.yml +++ b/.github/workflows/basics.yml @@ -54,6 +54,7 @@ jobs: run: > composer require --no-update --no-scripts --no-interaction squizlabs/php_codesniffer:"dev-master" + phpcsstandards/phpcsextra:"dev-develop" # Install dependencies and handle caching in one go. # @link https://github.com/marketplace/actions/install-composer-dependencies diff --git a/README.md b/README.md index a9992f40..cacf2234 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ The `Yoast` standard for PHP_CodeSniffer is comprised of the following: * The `WordPress` ruleset from the [WordPress Coding Standards](https://github.com/WordPress/WordPress-Coding-Standards) implementing the official [WordPress PHP Coding Standards](https://make.wordpress.org/core/handbook/coding-standards/php/), with some [select exclusions](https://github.com/Yoast/yoastcs/blob/develop/Yoast/ruleset.xml#L29-L75). * The [`PHPCompatibilityWP`](https://github.com/PHPCompatibility/PHPCompatibilityWP) ruleset which checks code for PHP cross-version compatibility while preventing false positives for functionality polyfilled within WordPress. * Select additional sniffs taken from [`PHP_CodeSniffer`](https://github.com/squizlabs/PHP_CodeSniffer). +* Select additional sniffs taken from [`PHPCSExtra`](https://github.com/PHPCSStandards/PHPCSExtra). * A number of custom Yoast specific sniffs. Files within version management and dependency related directories, such as the Composer `vendor` directory, are excluded from the scans by default. diff --git a/composer.json b/composer.json index 66b237ba..3874d100 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "php-parallel-lint/php-console-highlighter": "^1.0.0", "php-parallel-lint/php-parallel-lint": "^1.3.2", "phpcompatibility/phpcompatibility-wp": "^2.1.4", + "phpcsstandards/phpcsextra": "^1.1.2", "squizlabs/php_codesniffer": "^3.7.2", "wp-coding-standards/wpcs": "^2.3.0" }, From b2f1773e43ce95a32a87f48b03b6677e6826edad Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 14 Sep 2023 04:41:07 +0200 Subject: [PATCH 2/3] YoastCS: remove `IfElseDeclaration` sniff in favour of similar from PHPCSExtra This commit removes the YoastCS native `Yoast.ControlStructures.IfElseDeclaration` sniff in favour of a similar sniff from PHPCSExtra. The PHPCSExtra `Universal.ControlStructures.IfElseDeclaration` sniff includes a fixer, making it more powerful than the YoastCS sniff. --- .../IfElseDeclarationStandard.xml | 59 ------------ .../IfElseDeclarationSniff.php | 89 ------------------- .../IfElseDeclarationUnitTest.inc | 86 ------------------ .../IfElseDeclarationUnitTest.php | 46 ---------- Yoast/ruleset.xml | 4 + 5 files changed, 4 insertions(+), 280 deletions(-) delete mode 100644 Yoast/Docs/ControlStructures/IfElseDeclarationStandard.xml delete mode 100644 Yoast/Sniffs/ControlStructures/IfElseDeclarationSniff.php delete mode 100644 Yoast/Tests/ControlStructures/IfElseDeclarationUnitTest.inc delete mode 100644 Yoast/Tests/ControlStructures/IfElseDeclarationUnitTest.php diff --git a/Yoast/Docs/ControlStructures/IfElseDeclarationStandard.xml b/Yoast/Docs/ControlStructures/IfElseDeclarationStandard.xml deleted file mode 100644 index c2f9b211..00000000 --- a/Yoast/Docs/ControlStructures/IfElseDeclarationStandard.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - - - -elseif ($bar) { - $var = 2; -} - ]]> - - - elseif ($bar) { - $var = 2; -} - ]]> - - - - - - - - elseif ($bar) { - $var = 2; -} - ]]> - - - else { - $var = 2; - } - ]]> - - - diff --git a/Yoast/Sniffs/ControlStructures/IfElseDeclarationSniff.php b/Yoast/Sniffs/ControlStructures/IfElseDeclarationSniff.php deleted file mode 100644 index 78476b86..00000000 --- a/Yoast/Sniffs/ControlStructures/IfElseDeclarationSniff.php +++ /dev/null @@ -1,89 +0,0 @@ -getTokens(); - - if ( isset( $tokens[ $stackPtr ]['scope_opener'] ) ) { - $scope_open = $tokens[ $stackPtr ]['scope_opener']; - } - else { - // Deal with "else if". - $next = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); - if ( $tokens[ $next ]['code'] === \T_IF && isset( $tokens[ $next ]['scope_opener'] ) ) { - $scope_open = $tokens[ $next ]['scope_opener']; - } - } - - if ( ! isset( $scope_open ) || $tokens[ $scope_open ]['code'] === \T_COLON ) { - // No scope opener found or alternative syntax (not our concern). - return; - } - - $previous_scope_closer = $phpcsFile->findPrevious( \T_CLOSE_CURLY_BRACKET, ( $stackPtr - 1 ) ); - - if ( $tokens[ $previous_scope_closer ]['line'] === $tokens[ $stackPtr ]['line'] ) { - $phpcsFile->addError( - '%s statement must be on a new line', - $stackPtr, - 'NewLine', - [ \ucfirst( $tokens[ $stackPtr ]['content'] ) ] - ); - } - elseif ( $tokens[ $previous_scope_closer ]['column'] !== $tokens[ $stackPtr ]['column'] ) { - $phpcsFile->addError( - '%s statement not aligned with previous part of the control structure', - $stackPtr, - 'Alignment', - [ \ucfirst( $tokens[ $stackPtr ]['content'] ) ] - ); - } - - $previous_non_empty = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true ); - - if ( $previous_scope_closer !== $previous_non_empty ) { - $error = 'Nothing but whitespace and comments allowed between closing bracket and %s statement, found "%s"'; - $data = [ - $tokens[ $stackPtr ]['content'], - \trim( $phpcsFile->getTokensAsString( ( $previous_scope_closer + 1 ), ( $stackPtr - ( $previous_scope_closer + 1 ) ) ) ), - ]; - $phpcsFile->addError( $error, $stackPtr, 'StatementFound', $data ); - } - } -} diff --git a/Yoast/Tests/ControlStructures/IfElseDeclarationUnitTest.inc b/Yoast/Tests/ControlStructures/IfElseDeclarationUnitTest.inc deleted file mode 100644 index fa42db12..00000000 --- a/Yoast/Tests/ControlStructures/IfElseDeclarationUnitTest.inc +++ /dev/null @@ -1,86 +0,0 @@ - => - */ - public function getErrorList() { - return [ - 22 => 1, - 28 => 1, - 30 => 1, - 34 => 1, - 37 => 1, - 44 => 1, - 51 => 1, - 76 => 1, - 84 => 2, - ]; - } - - /** - * Returns the lines where warnings should occur. - * - * @return array => - */ - public function getWarningList() { - return []; - } -} diff --git a/Yoast/ruleset.xml b/Yoast/ruleset.xml index e7db917e..72acbfb9 100644 --- a/Yoast/ruleset.xml +++ b/Yoast/ruleset.xml @@ -138,6 +138,10 @@ These may make it into WPCS at some point. If so, they can be removed here. ############################################################################# --> + + + + From 9ab69a90dcff6cee61b748f076590777dddf6d04 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 14 Sep 2023 04:46:51 +0200 Subject: [PATCH 3/3] YoastCS: remove `NamespaceDeclaration` sniff in favour of similar from PHPCSExtra This commit removes the `Yoast.Namespaces.NamespaceDeclaration` sniff in favour of three sniffs from PHPCSExtra, which combined do the same thing. --- .../NamespaceDeclarationStandard.xml | 65 ----------- .../Namespaces/NamespaceDeclarationSniff.php | 110 ------------------ .../NamespaceDeclarationUnitTest.1.inc | 8 -- .../NamespaceDeclarationUnitTest.2.inc | 11 -- .../NamespaceDeclarationUnitTest.php | 49 -------- Yoast/ruleset.xml | 9 ++ 6 files changed, 9 insertions(+), 243 deletions(-) delete mode 100644 Yoast/Docs/Namespaces/NamespaceDeclarationStandard.xml delete mode 100644 Yoast/Sniffs/Namespaces/NamespaceDeclarationSniff.php delete mode 100644 Yoast/Tests/Namespaces/NamespaceDeclarationUnitTest.1.inc delete mode 100644 Yoast/Tests/Namespaces/NamespaceDeclarationUnitTest.2.inc delete mode 100644 Yoast/Tests/Namespaces/NamespaceDeclarationUnitTest.php diff --git a/Yoast/Docs/Namespaces/NamespaceDeclarationStandard.xml b/Yoast/Docs/Namespaces/NamespaceDeclarationStandard.xml deleted file mode 100644 index 2bd53679..00000000 --- a/Yoast/Docs/Namespaces/NamespaceDeclarationStandard.xml +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - - - ; - ]]> - - - { - // Code. -} - ]]> - - - - - - - - - Yoast\Sub; - ]]> - - - ; - ]]> - - - - - - - - - - - - namespace Yoast\Sub\B { -} - ]]> - - - diff --git a/Yoast/Sniffs/Namespaces/NamespaceDeclarationSniff.php b/Yoast/Sniffs/Namespaces/NamespaceDeclarationSniff.php deleted file mode 100644 index 1d9179a3..00000000 --- a/Yoast/Sniffs/Namespaces/NamespaceDeclarationSniff.php +++ /dev/null @@ -1,110 +0,0 @@ -getTokens(); - - $statements = []; - - while ( ( $stackPtr = $phpcsFile->findNext( \T_NAMESPACE, ( $stackPtr + 1 ) ) ) !== false ) { - - $next_non_empty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); - if ( $next_non_empty === false ) { - // Live coding or parse error. - break; - } - - if ( $tokens[ $next_non_empty ]['code'] === \T_NS_SEPARATOR ) { - // Not a namespace declaration, but the use of the namespace keyword as operator. - continue; - } - - // OK, found a namespace declaration. - $statements[] = $stackPtr; - - if ( isset( $tokens[ $stackPtr ]['scope_condition'] ) - && $tokens[ $stackPtr ]['scope_condition'] === $stackPtr - ) { - // Scoped namespace declaration. - $phpcsFile->addError( - 'Scoped namespace declarations are not allowed.', - $stackPtr, - 'ScopedForbidden' - ); - } - - if ( $tokens[ $next_non_empty ]['code'] === \T_SEMICOLON - || $tokens[ $next_non_empty ]['code'] === \T_OPEN_CURLY_BRACKET - ) { - // Namespace declaration without namespace name (= global namespace). - $phpcsFile->addError( - 'Namespace declarations without a namespace name are not allowed.', - $stackPtr, - 'NoNameForbidden' - ); - } - } - - $count = \count( $statements ); - if ( $count > 1 ) { - $data = [ - $count, - $tokens[ $statements[0] ]['line'], - ]; - - for ( $i = 1; $i < $count; $i++ ) { - $phpcsFile->addError( - 'There should be only one namespace declaration per file. Found %d namespace declarations. The first declaration was found on line %d', - $statements[ $i ], - 'MultipleFound', - $data - ); - } - } - - return ( $phpcsFile->numTokens + 1 ); - } -} diff --git a/Yoast/Tests/Namespaces/NamespaceDeclarationUnitTest.1.inc b/Yoast/Tests/Namespaces/NamespaceDeclarationUnitTest.1.inc deleted file mode 100644 index 0127313d..00000000 --- a/Yoast/Tests/Namespaces/NamespaceDeclarationUnitTest.1.inc +++ /dev/null @@ -1,8 +0,0 @@ - => - */ - public function getErrorList( $testFile = '' ) { - switch ( $testFile ) { - case 'NamespaceDeclarationUnitTest.2.inc': - return [ - 3 => 1, - 5 => 3, - 7 => 2, - 9 => 3, - 11 => 2, - ]; - - default: - return []; - } - } - - /** - * Returns the lines where warnings should occur. - * - * @return array => - */ - public function getWarningList() { - return []; - } -} diff --git a/Yoast/ruleset.xml b/Yoast/ruleset.xml index 72acbfb9..82a53fc3 100644 --- a/Yoast/ruleset.xml +++ b/Yoast/ruleset.xml @@ -142,6 +142,15 @@ + + + + + + + + +