Skip to content

Commit

Permalink
Core/Extra: tweak the inclusion of the `Modernize.FunctionCalls.Dirna…
Browse files Browse the repository at this point in the history
…me` sniff

Follow up on 2137

As  WP Core has now dropped support for PHP < 7.0, it can start using the `dirname()` `$levels` parameters.
Think:
```php
// PHP < 7.0.
$path = dirname( dirname( dirname( __DIR__ ) ) );

// PHP 7.0+.
$path = dirname( __DIR__, 3 );
```

The `Modernize.FunctionCalls.Dirname` sniff we include also includes a check (and fixer) for that, so we can now include that sniff completely in the `Core` ruleset.

For now, I'm proposing to silence the error code related to the PHP 7.0 modernization opportunity (again) for the `Extra` ruleset as not all plugins/themes will have dropped support for PHP < 7.0 yet.

To be on the safe side, I'm explicitly excluding the whole sniff from the ruleset used for WPCS itself as WPCS still has a PHP 5.4 minimum.
  • Loading branch information
jrfnl committed Jul 26, 2023
1 parent 2c593e4 commit 01d23a6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@

<!-- Linting is done in a separate CI job, no need to duplicate it. -->
<exclude name="Generic.PHP.Syntax"/>

<!-- WPCS still has a PHP 5.4 minimum. -->
<exclude name="Modernize.FunctionCalls.Dirname"/>
</rule>

<!-- Check code for cross-version PHP compatibility. -->
Expand Down
5 changes: 3 additions & 2 deletions WordPress-Core/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,9 @@
<!-- Check that class name references use the correct case. -->
<rule ref="WordPress.WP.ClassNameCase"/>

<!-- Check that __DIR__ is favoured over dirname(__FILE__).
<!-- Check that __DIR__ is favoured over dirname(__FILE__)
and that dirname( __DIR__, $levels ) is favoured over nested calls to dirname().
See: https://core.trac.wordpress.org/ticket/48082 -->
<rule ref="Modernize.FunctionCalls.Dirname.FileConstant"/>
<rule ref="Modernize.FunctionCalls.Dirname"/>

</ruleset>
6 changes: 6 additions & 0 deletions WordPress-Extra/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

<rule ref="WordPress-Core"/>

<!-- Silence the "no nested dirnames, use $levels" notice, which is included in Core,
as plugin/themes may still support PHP < 7.0. -->
<rule ref="Modernize.FunctionCalls.Dirname.Nested">
<severity>0</severity>
</rule>

<!-- Generic PHP best practices.
https://github.com/WordPress/WordPress-Coding-Standards/pull/382 -->
<rule ref="Generic.PHP.DeprecatedFunctions"/>
Expand Down

0 comments on commit 01d23a6

Please sign in to comment.