Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YoastCS: upgrade to WordPressCS 3.0.1 #323

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/basics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.1' # Can be updated to "latest" once PHPCS 3.8.0 has been released.
coverage: none
tools: cs2pr

Expand All @@ -48,14 +48,12 @@ jobs:

# Use the WIP/develop branches of all CS dependencies as an early detection system for bugs upstream.
- name: 'Composer: adjust dependencies - use dev versions of CS dependencies'
# Note: while WPCS 3.0.0 is not yet supported, WPCS will not be updated.
# Once YoastCS has been updated for WPCS 3.0.0, WPCS dev-develop should be added (back) to this command.
# wp-coding-standards/wpcs:"dev-develop"
run: >
composer require --no-update --no-scripts --no-interaction
squizlabs/php_codesniffer:"dev-master"
phpcsstandards/phpcsutils:"dev-develop"
phpcsstandards/phpcsextra:"dev-develop"
wp-coding-standards/wpcs:"dev-develop"

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/quicktest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ jobs:

- name: "Composer: set PHPCS dependencies for tests (dev)"
if: ${{ matrix.cs_dependencies == 'dev' }}
# Note: while WPCS 3.0.0 is not yet supported, WPCS will not be updated for the `dev` builds.
# Once YoastCS has been updated for WPCS 3.0.0, WPCS dev-develop should be added to this command.
run: >
composer require --no-update --no-scripts --no-interaction --ignore-platform-req=php+
squizlabs/php_codesniffer:"dev-master"
phpcsstandards/phpcsutils:"dev-develop"
wp-coding-standards/wpcs:"dev-develop"

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
Expand Down
11 changes: 3 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

# Note: while WPCS 3.0.0 is not (yet) supported by YoastCS, the "highest" release for WPCS should use
# the last stable WPCS release supported by YoastCS, i.e. `2.3.0`.
# Once support for WPCS 3.0.0 has been added, the matrix should switch (back) using `dev-develop`.
env:
PHPCS_HIGHEST: 'dev-master'
UTILS_HIGHEST: 'dev-develop'
WPCS_HIGHEST: '2.3.0'
WPCS_HIGHEST: 'dev-develop'

jobs:
#### TEST STAGE ####
Expand Down Expand Up @@ -92,12 +89,11 @@ jobs:

- name: "Composer: set PHPCS dependencies for tests (dev)"
if: ${{ matrix.cs_dependencies == 'dev' }}
# Note: while WPCS 3.0.0 is not yet supported, WPCS will not be updated for the `dev` builds.
# Once YoastCS has been updated for WPCS 3.0.0, WPCS dev-develop should be added to this command.
run: >
composer require --no-update --no-scripts --no-interaction --ignore-platform-req=php+
squizlabs/php_codesniffer:"${{ env.PHPCS_HIGHEST }}"
phpcsstandards/phpcsutils:"${{ env.UTILS_HIGHEST }}"
wp-coding-standards/wpcs:"${{ env.WPCS_HIGHEST }}"

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
Expand Down Expand Up @@ -189,12 +185,11 @@ jobs:

- name: "Composer: set PHPCS dependencies for tests (dev)"
if: ${{ matrix.cs_dependencies == 'dev' }}
# Note: while WPCS 3.0.0 is not yet supported, WPCS will not be updated for the `dev` builds.
# Once YoastCS has been updated for WPCS 3.0.0, WPCS dev-develop should be added to this command.
run: >
composer require --no-update --no-scripts --no-interaction
squizlabs/php_codesniffer:"${{ env.PHPCS_HIGHEST }}"
phpcsstandards/phpcsutils:"${{ env.UTILS_HIGHEST }}"
wp-coding-standards/wpcs:"${{ env.WPCS_HIGHEST }}"

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
Expand Down
3 changes: 3 additions & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
<!-- Sniffs are not run in the context of WordPress. -->
<exclude name="WordPress.Security"/>
<exclude name="WordPress.WP"/>

<!-- Exclude select "modern PHP" sniffs, which conflict with the minimum supported PHP version of this package. -->
<exclude name="Modernize.FunctionCalls.Dirname.Nested"/><!-- PHP 7.0+. -->
</rule>

<!-- While PHPCompatibility is already included in the Yoast ruleset, it uses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function process( File $phpcsFile, $stackPtr ) {
break;
}


if ( $tokens[ $commentEnd ]['code'] !== \T_DOC_COMMENT_CLOSE_TAG ) {
// Function without (proper) docblock. Not our concern.
return;
Expand Down
1 change: 0 additions & 1 deletion Yoast/Sniffs/Files/TestDoublesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public function process( File $phpcsFile, $stackPtr ) {
$name_contains_double_or_mock = true;
}


if ( empty( $this->target_paths ) === true ) {
if ( $name_contains_double_or_mock === true ) {
// No valid target paths found.
Expand Down
10 changes: 8 additions & 2 deletions Yoast/Sniffs/NamingConventions/ObjectNameDepthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@

use PHPCSUtils\Utils\Namespaces;
use PHPCSUtils\Utils\ObjectDeclarations;
use WordPressCS\WordPress\Helpers\IsUnitTestTrait;
use WordPressCS\WordPress\Helpers\SnakeCaseHelper;
use WordPressCS\WordPress\Sniff as WPCS_Sniff;

/**
* Check the number of words in object names declared within a namespace.
*
* @since 2.0.0
*
* @uses \WordPressCS\WordPress\Helpers\IsUnitTestTrait::$custom_test_classes
*/
final class ObjectNameDepthSniff extends WPCS_Sniff {

use IsUnitTestTrait;

/**
* Maximum number of words.
*
Expand Down Expand Up @@ -88,7 +94,7 @@ public function process_token( $stackPtr ) {

// Handle names which are potentially in CamelCaps.
if ( \strpos( $snakecase_object_name, '_' ) === false ) {
$snakecase_object_name = self::get_snake_case_name_suggestion( $snakecase_object_name );
$snakecase_object_name = SnakeCaseHelper::get_suggestion( $snakecase_object_name );
}

$parts = \explode( '_', $snakecase_object_name );
Expand All @@ -99,7 +105,7 @@ public function process_token( $stackPtr ) {
*/
$last = \array_pop( $parts );
if ( isset( $this->test_suffixes[ $last ] ) ) {
if ( $this->test_suffixes[ $last ] === true && $this->is_test_class( $stackPtr ) ) {
if ( $this->test_suffixes[ $last ] === true && $this->is_test_class( $this->phpcsFile, $stackPtr ) ) {
--$part_count;
}
else {
Expand Down
16 changes: 8 additions & 8 deletions Yoast/Sniffs/NamingConventions/ValidHookNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,33 +167,33 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
* plugin prefix for hook names and remembers whether a prefix was found to allow
* checking whether it was the correct one.
*
* @param string $string The target string.
* @param string $text_string The target string.
* @param string $regex The punctuation regular expression to use.
* @param string $transform_type Whether to do a partial or complete transform.
* Valid values are: 'full', 'case', 'punctuation'.
* @return string
*/
protected function transform( $string, $regex, $transform_type = 'full' ) {
protected function transform( $text_string, $regex, $transform_type = 'full' ) {

if ( empty( $this->validated_prefixes ) ) {
return parent::transform( $string, $regex, $transform_type );
return parent::transform( $text_string, $regex, $transform_type );
}

if ( $this->first_string === '' ) {
$this->first_string = $string;
$this->first_string = $text_string;
}

// Not the first text string.
if ( $string !== $this->first_string ) {
return parent::transform( $string, $regex, $transform_type );
if ( $text_string !== $this->first_string ) {
return parent::transform( $text_string, $regex, $transform_type );
}

// Repeated call for the first text string.
if ( $this->found_prefix !== '' ) {
$string = \substr( $string, \strlen( $this->found_prefix ) );
$text_string = \substr( $text_string, \strlen( $this->found_prefix ) );
}

return $this->found_prefix . parent::transform( $string, $regex, $transform_type );
return $this->found_prefix . parent::transform( $text_string, $regex, $transform_type );
}

/**
Expand Down
1 change: 0 additions & 1 deletion Yoast/Tests/NamingConventions/ObjectNameDepthUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,3 @@ public function getWarningList( $testFile = '' ) {
}
}
}

1 change: 0 additions & 1 deletion Yoast/Tests/NamingConventions/ValidHookNameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,3 @@ public function getWarningList() {
];
}
}

55 changes: 15 additions & 40 deletions Yoast/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
Ref: https://github.com/WordPress/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#minimum-wp-version-to-check-for-usage-of-deprecated-functions-classes-and-function-parameters
-->
<properties>
<property name="minimum_supported_version" value="6.2"/>
<property name="minimum_wp_version" value="6.2"/>
</properties>

<exclude name="Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines"/>
Expand Down Expand Up @@ -76,13 +76,13 @@
<exclude name="Yoast.Commenting.FileComment.CopyrightTagOrder"/>

<!-- WPCS demands long arrays. YoastCS demands short arrays. -->
<exclude name="Generic.Arrays.DisallowShortArraySyntax"/>
<exclude name="Universal.Arrays.DisallowShortArraySyntax"/>

<!-- Demanding Yoda conditions is stupid. -->
<exclude name="WordPress.PHP.YodaConditions"/>

<!-- A while loop is the only valid control structure where an assignment can be justified. -->
<exclude name="WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition"/>
<exclude name="Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition"/>

<!-- If a conscious choice has been made for a non-strict comparison, that's ok.
I.e. when `strict` has been explicitely set to `false` in an array comparison,
Expand Down Expand Up @@ -152,24 +152,12 @@
<!-- Demand that "else(if)" is on a new line after the scope closer of the preceding if. -->
<rule ref="Universal.ControlStructures.IfElseDeclaration"/>

<!-- Disallow namespace declarations without a namespace name. -->
<rule ref="Universal.Namespaces.DisallowDeclarationWithoutName"/>

<!-- Disallow namespace declarations using the curly brace syntax. -->
<rule ref="Universal.Namespaces.DisallowCurlyBraceSyntax"/>

<!-- Disallow declaring multiple namespaces within one file. -->
<rule ref="Universal.Namespaces.OneDeclarationPerFile"/>

<!-- Error prevention: Make sure the condition in a inline if declaration is bracketed. -->
<rule ref="Squiz.ControlStructures.InlineIfDeclaration"/>

<!-- Error prevention: Make sure arithmetics are bracketed. -->
<rule ref="Squiz.Formatting.OperatorBracket.MissingBrackets"/>

<!-- CS: no blank line between the content of a function and a function close brace.-->
<rule ref="PSR2.Methods.FunctionClosingBrace"/>

<!-- CS: ensure exactly one blank line before each property declaration. -->
<rule ref="Squiz.WhiteSpace.MemberVarSpacing"/>

Expand All @@ -192,31 +180,6 @@
</properties>
</rule>

<!-- Error prevention: Ensure no git conflicts make it into the code base. -->
<!-- PHPCS 3.4.0: This sniff will be added to WPCS in due time and can then be removed from this ruleset.
Related: https://github.com/WordPress/WordPress-Coding-Standards/issues/1500 -->
<rule ref="Generic.VersionControl.GitMergeConflict"/>

<!-- CS: no space between an increment/decrement operator and the variable it applies to. -->
<!-- PHPCS 3.4.0: This sniff will be added to WPCS in due time and can then be removed from this ruleset.
Related: https://github.com/WordPress/WordPress-Coding-Standards/issues/1511 -->
<rule ref="Generic.WhiteSpace.IncrementDecrementSpacing"/>

<!-- QA: Function declarations should not contain parameters which will never be used. -->
<!-- PHPCS 3.4.0: This sniff will be added to WPCS in due time and can then be removed from this ruleset.
Related: https://github.com/WordPress/WordPress-Coding-Standards/issues/1510 -->
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter.Found"/>
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed"/>

<!-- CS: Ensure consistent whitespace around spread operators. -->
<!-- PHPCS 3.5.0: This sniff will be added to WPCS in due time and can then be removed from this ruleset.
Related: https://github.com/WordPress/WordPress-Coding-Standards/issues/1762 -->
<rule ref="Generic.WhiteSpace.SpreadOperatorSpacingAfter"/>

<!-- CS: Disallow a leading backslash at the start of an import use statement. -->
<!-- PHPCS 3.5.0: This sniff may be added to WPCS in due time and can then be removed from this ruleset. -->
<rule ref="PSR12.Files.ImportStatement"/>

<!-- CS: Enforces that a PHP open tag is on a line by itself when used at the start of a PHP-only file. -->
<!-- PHPCS 3.5.0: This sniff may be added to WPCS in due time and can then be removed from this ruleset. -->
<rule ref="PSR12.Files.OpenTag"/>
Expand All @@ -231,4 +194,16 @@
<exclude-pattern>*/index\.php</exclude-pattern>
</rule>


<!--
#############################################################################
SNIFFS TO ENFORCE CODE MODERNIZATION
#############################################################################
-->

<!-- Undo the WPCS-Extra silencing of the "no nested dirnames, use $levels" notice (PHP 7.0+). -->
<rule ref="Modernize.FunctionCalls.Dirname.Nested">
<severity>5</severity>
</rule>

</ruleset>
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"phpcsstandards/phpcsextra": "^1.1.2",
"phpcsstandards/phpcsutils": "^1.0.8",
"squizlabs/php_codesniffer": "^3.7.2",
"wp-coding-standards/wpcs": "^2.3.0"
"wp-coding-standards/wpcs": "^3.0.1"
},
"require-dev": {
"phpcompatibility/php-compatibility": "^9.3.5",
Expand Down