Make WordPress Core

Changeset 56695


Ignore:
Timestamp:
09/26/2023 12:24:43 AM (5 months ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Upgrade WPCS to version 3.0.0.

This is an important release which makes significant changes to improve the accuracy, performance, stability and maintainability of all sniffs, as well as making WordPressCS much better at handling modern PHP.

WordPressCS 3.0.0 contains breaking changes, both for people using ignore annotations, people maintaining custom rulesets, as well as for sniff developers who maintain a custom PHPCS standard based on WordPressCS.

If you are an end-user or maintain a custom WordPressCS based ruleset, please start by reading the Upgrade Guide to WordPressCS 3.0.0 for ruleset maintainers which lists the most important changes and contains a step by step guide for upgrading.

If you are a maintainer of an external standard based on WordPressCS and any of your custom sniffs are based on or extend WordPressCS sniffs, please read the Upgrade Guide to WordPressCS 3.0.0 for Developers.

In all cases, please read the complete changelog carefully before you upgrade.

This commit:

  • Updates the Composer dependencies to use the new version, including updating the underlying PHP_CodeSniffer dependency to the new minimum supported version for WPCS.
    Note: the Composer PHPCS installer plugin is no longer explicitly required as it is now a dependency of WPCS, so the dependency is inherited automatically.
  • Updates the ruleset for WPCS 3.0.0. This includes:
    • Raising the memory limit to be on the safe side as WPCS 3.0.0 contains a lot more sniffs.
    • Removing explicit inclusions of extra rules, which have now been added to the WordPress-Core ruleset..
    • Updating property names for select sniffs.
    • Updating one exclusion — the WordPress.CodeAnalysis.AssignmentInCondition sniff has been (partially) replaced by the Generic.CodeAnalysis.AssignmentInCondition sniff.
    • Adding one new exclusion.
  • Downgrades one new error to a warning.
    The Generic.Files.OneObjectStructurePerFile sniff enforces that there is only one OO structure declaration per file. At this time, this sniff would yield 29 errors. By downgrading the sniff to a warning, the build can pass and the issues can be fixed in due time. For now, the test directory will be excluded until the issues are fixed (as the test directory CS run does not allow for warnings).
  • Updates ignore annotations for WPCS 3.0.0.

Reference: WPCS 3.0.0 release notes.

Follow-up to [43571], [44574], [45600], [47927].

Props jrf, jorbin, desrosj.
See #59161.

Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/composer.json

    r56687 r56695  
    1717    },
    1818    "require-dev": {
    19         "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
    20         "squizlabs/php_codesniffer": "3.6.0",
    21         "wp-coding-standards/wpcs": "~2.3.0",
     19        "squizlabs/php_codesniffer": "3.7.2",
     20        "wp-coding-standards/wpcs": "~3.0.0",
    2221        "phpcompatibility/phpcompatibility-wp": "~2.1.3",
    2322        "yoast/phpunit-polyfills": "^1.1.0"
  • trunk/phpcs.xml.dist

    r56669 r56695  
    1616    <arg name="cache" value=".cache/phpcs.json"/>
    1717
    18     <!-- Set the memory limit to 256M.
     18    <!-- Set the memory limit to 512M.
    1919         For most standard PHP configurations, this means the memory limit will temporarily be raised.
    2020         Ref: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#specifying-phpini-settings
    2121    -->
    22     <ini name="memory_limit" value="256M"/>
     22    <ini name="memory_limit" value="512M"/>
    2323
    2424    <!-- Strip the filepaths down to the relevant bit. -->
     
    114114
    115115    <rule ref="WordPress-Core"/>
    116     <rule ref="WordPress.CodeAnalysis.EmptyStatement"/>
    117 
    118     <!-- Enforce no parenthesis for language constructs, and only one space immediately after.
    119          Including this rule is temporary until it is moved from the WordPress-Extra ruleset to
    120          the WordPress-Core ruleset upstream.
    121     -->
    122     <rule ref="PEAR.Files.IncludingFile.BracketsNotRequired">
    123         <type>warning</type>
    124     </rule>
    125     <rule ref="PEAR.Files.IncludingFile.UseRequire">
    126         <type>warning</type>
    127     </rule>
    128     <rule ref="PEAR.Files.IncludingFile.UseRequireOnce">
    129         <type>warning</type>
    130     </rule>
    131     <rule ref="Squiz.WhiteSpace.LanguageConstructSpacing"/>
    132116
    133117
     
    139123
    140124    <!-- These rules are being set as warnings instead of errors, so we can error check the entire codebase. -->
     125    <rule ref="Generic.Files.OneObjectStructurePerFile.MultipleFound">
     126        <type>warning</type>
     127        <!-- Exclude the unit tests as no warnings are allowed there. Note: these issues should be fixed and the exclude removed! -->
     128        <exclude-pattern>/tests/phpunit/*</exclude-pattern>
     129    </rule>
    141130    <rule ref="WordPress.DB.PreparedSQL.InterpolatedNotPrepared">
    142131        <type>warning</type>
     
    154143    <rule ref="WordPress.NamingConventions.ValidVariableName">
    155144        <properties>
    156             <property name="customPropertiesWhitelist" type="array">
     145            <property name="allowed_custom_properties" type="array">
    157146                <!-- From database structure queries. -->
    158147                <element value="Collation"/>
     
    210199    <rule ref="WordPress.PHP.NoSilencedErrors">
    211200        <properties>
    212             <property name="custom_whitelist" type="array">
     201            <property name="customAllowedFunctionsList" type="array">
    213202                <element value="ssh2_connect"/>
    214203                <element value="ssh2_auth_password"/>
     
    240229    -->
    241230
     231    <!-- Assignments in while conditions are a valid method of looping over iterables. -->
     232    <rule ref="Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition">
     233        <exclude-pattern>*</exclude-pattern>
     234    </rule>
     235
    242236    <!-- Exclude checking of line endings when reporting errors, but fix them when running phpcbf.
    243237         Git and SVN manage these pretty well cross-platform as "native".
     
    253247        <exclude-pattern>/wp-config-sample\.php</exclude-pattern>
    254248        <exclude-pattern>/wp-tests-config\.php</exclude-pattern>
     249        <exclude-pattern>/wp-tests-config-sample\.php</exclude-pattern>
     250    </rule>
     251
     252    <!-- Exclude sample config from modernization to prevent breaking CI workflows based on WP-CLI scaffold.
     253         See: https://core.trac.wordpress.org/ticket/48082#comment:16 -->
     254    <rule ref="Modernize.FunctionCalls.Dirname.FileConstant">
    255255        <exclude-pattern>/wp-tests-config-sample\.php</exclude-pattern>
    256256    </rule>
     
    275275    </rule>
    276276
    277     <!-- Assignments in while conditions are a valid method of looping over iterables. -->
    278     <rule ref="WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition">
    279         <exclude-pattern>*</exclude-pattern>
     277    <!-- Temporarily ignore this sniff for block files until the changes from upstream make it into core.
     278         This can be removed once the block-library package is updated for WP 6.4. -->
     279    <rule ref="Squiz.Functions.MultiLineFunctionDeclaration.SpaceAfterFunction">
     280        <exclude-pattern>/src/wp-includes/blocks/*</exclude-pattern>
    280281    </rule>
    281282
  • trunk/src/wp-admin/includes/class-wp-site-health-auto-updates.php

    r56559 r56695  
    229229        foreach ( $vcs_dirs as $vcs_dir ) {
    230230            foreach ( $check_dirs as $check_dir ) {
    231                 // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition,Squiz.PHP.DisallowMultipleAssignments
     231                // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition,Squiz.PHP.DisallowMultipleAssignments
    232232                if ( $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" ) ) {
    233233                    break 2;
  • trunk/src/wp-admin/includes/image.php

    r56548 r56695  
    701701
    702702    // The denominator must not be zero.
    703     if ( 0 == $denominator ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- Deliberate loose comparison.
     703    if ( 0 == $denominator ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- Deliberate loose comparison.
    704704        return 0;
    705705    }
  • trunk/src/wp-admin/plugins.php

    r56680 r56695  
    512512                // Return early if all selected plugins already have auto-updates enabled or disabled.
    513513                // Must use non-strict comparison, so that array order is not treated as significant.
    514                 if ( $new_auto_updates == $auto_updates ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
     514                if ( $new_auto_updates == $auto_updates ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
    515515                    wp_redirect( $redirect );
    516516                    exit;
  • trunk/src/wp-includes/category-template.php

    r56360 r56695  
    882882    } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
    883883        // If no callback exists, look for the old-style single_text and multiple_text arguments.
    884         // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural
     884        // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingular,WordPress.WP.I18n.NonSingularStringLiteralPlural
    885885        $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] );
    886886    } else {
  • trunk/src/wp-includes/formatting.php

    r56693 r56695  
    832832    $spaces    = wp_spaces_regexp();
    833833
    834     // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound,WordPress.WhiteSpace.PrecisionAlignment.Found -- don't remove regex indentation
     834    // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound,Universal.WhiteSpace.PrecisionAlignment.Found -- don't remove regex indentation
    835835    $pattern =
    836836        '/'
     
    56845684}
    56855685
    5686 // phpcs:disable WordPress.WP.CapitalPDangit.Misspelled, WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid -- 8-)
     5686// phpcs:disable WordPress.WP.CapitalPDangit.MisspelledInComment,WordPress.WP.CapitalPDangit.MisspelledInText,WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid -- 8-)
    56875687/**
    56885688 * Forever eliminate "Wordpress" from the planet (or at least the little bit we can influence).
  • trunk/src/wp-includes/post.php

    r56623 r56695  
    13831383
    13841384    if ( false === $args->label_count ) {
    1385         // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural
     1385        // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingular,WordPress.WP.I18n.NonSingularStringLiteralPlural
    13861386        $args->label_count = _n_noop( $args->label, $args->label );
    13871387    }
  • trunk/tests/phpunit/tests/customize/manager.php

    r56687 r56695  
    13461346        // User saved as one who can bypass content_save_pre filter.
    13471347        $this->assertStringContainsString( '<script>', get_option( 'custom_html_1' ) );
    1348         $this->assertStringContainsString( 'Wordpress', get_option( 'custom_html_1' ) ); // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
     1348        $this->assertStringContainsString( 'Wordpress', get_option( 'custom_html_1' ) ); // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText
    13491349
    13501350        // User saved as one who cannot bypass content_save_pre filter.
  • trunk/tests/phpunit/tests/formatting/capitalPDangit.php

    r53562 r56695  
    11<?php
    2 // phpcs:disable WordPress.WP.CapitalPDangit.Misspelled -- 🙃
     2// phpcs:disable WordPress.WP.CapitalPDangit.MisspelledInText -- 🙃
    33
    44/**
  • trunk/tests/phpunit/tests/meta.php

    r54891 r56695  
    379379        $string_mid = "{$meta_id}.0";
    380380
    381         // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- intentional implicit casting check
     381        // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- intentional implicit casting check
    382382        $this->assertTrue( floor( $string_mid ) == $string_mid );
    383383        $this->assertNotFalse( get_metadata_by_mid( 'user', $string_mid ) );
Note: See TracChangeset for help on using the changeset viewer.