Skip to content
Closed
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"phpcompatibility/phpcompatibility-wp": "~2.1.3",
"phpstan/phpstan": "2.3.4",
"phpstan/phpstan-phpunit": "2.1.1",
"phpstan/phpstan-strict-rules": "2.1.1",
"yoast/phpunit-polyfills": "^1.1.0"
},
"config": {
Expand Down
20 changes: 19 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# PHPStan configuration for WordPress Core.
#
# To overload this configuration, copy this file to phpstan.neon and adjust as needed.
# To override this configuration, create a phpstan.neon which includes this file and adjusts
# its parameters as needed, rather than copying it, so that the copy does not go stale. See
# tests/phpstan/README.md for examples.
#
# https://phpstan.org/config-reference

Expand Down Expand Up @@ -58,11 +60,27 @@ includes:
- tests/phpstan/baselines/variable.undefined.neon
# phpstan:baselines end

# Rules from phpstan-strict-rules which core already complies with, or which enforce the
# WordPress Coding Standards. The others are not enabled until the code complies with them,
# but they can be enabled in a local phpstan.neon to work toward that. See
# tests/phpstan/README.md.
#
# These are registered individually, rather than by including phpstan-strict-rules' rules.neon
# with only these enabled, because that file also makes several PHPStan parameters stricter
# even when its rules are disabled.
# https://github.com/phpstan/phpstan-strict-rules
rules:
- PHPStan\Rules\DisallowedConstructs\DisallowedBacktickRule
- PHPStan\Rules\DisallowedConstructs\DisallowedShortTernaryRule
- PHPStan\Rules\Functions\ClosureUsesThisRule
- PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule

parameters:
# https://phpstan.org/user-guide/rule-levels
level: 5
reportUnmatchedIgnoredErrors: true


# The following ignored errors are not intended to be fixed, as distinct from the baselines
# included above.
#
Expand Down
7 changes: 6 additions & 1 deletion tests/phpstan/HashNotationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,12 @@ private function split_tags( string $text ): array {
$tags = array();
$current = null;

foreach ( preg_split( '#\R#', (string) $body ) ?: array() as $line ) {
$lines = preg_split( '#\R#', (string) $body );
if ( false === $lines ) {
$lines = array();
}

foreach ( $lines as $line ) {
$line = (string) preg_replace( '#^\s*\* ?#', '', $line, 1 );

if ( preg_match( '#^@([a-zA-Z][a-zA-Z0-9_-]*)[ \t]*(.*)$#', $line, $matches ) === 1 ) {
Expand Down
52 changes: 49 additions & 3 deletions tests/phpstan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PHPStan is a static analysis tool for PHP that checks your code for errors witho

## Running the tests

PHPStan requires PHP and Composer dependencies to be installed.
PHPStan requires PHP and Composer dependencies to be installed. Since there is no `composer.lock`, run `composer update` after pulling changes to `composer.json`, such as the addition of a PHPStan extension, or PHPStan will fail to load the configuration.

If you don't already have an environment ready, you can set one up by following [these instructions](https://github.com/WordPress/wordpress-develop/blob/master/README.md).

Expand Down Expand Up @@ -54,9 +54,55 @@ Some errors cannot be ignored, and so cannot be reported as warnings either. The

## The PHPStan configuration

The PHPStan configuration file is located at [`phpstan.neon.dist`](../../phpstan.neon.dist).
The PHPStan configuration file is located at [`phpstan.neon.dist`](../../phpstan.neon.dist). It sets the standards the code is held to: the rule level, the baselines, the errors ignored as not actionable, and the [strict rules](#strict-rules) enabled. It includes [`base.neon`](base.neon), which configures what PHPStan needs to know to analyze the codebase at all, such as the [WordPress-specific extensions](#wordpress-specific-extensions), the paths analyzed, and the supported PHP versions.

You can create a local copy at `phpstan.neon` to override the default configuration.
To override the default configuration, create a `phpstan.neon` file in the repository root, which PHPStan reads in place of `phpstan.neon.dist`. Include `phpstan.neon.dist` in it rather than copying it, so the override only states what it changes and keeps up with changes to the default configuration. For example, to analyze at a stricter rule level:

```neon
includes:
- phpstan.neon.dist

parameters:
level: 10
```

### Strict rules

`phpstan.neon.dist` enables only the rules from [phpstan-strict-rules](https://github.com/phpstan/phpstan-strict-rules) which the code already complies with, registering each one individually. It does not include the package's `rules.neon`, since that file also makes several PHPStan parameters stricter, such as `checkDynamicProperties` and `reportMaybesInMethodSignatures`, even when its rules are disabled.

To work toward complying with more of them, include `rules.neon` in your `phpstan.neon`. It enables every rule and makes those parameters stricter, unless `strictRules.allRules` is set to `false`, in which case only the rules set to `true` are enabled but the parameters are still made stricter. Either way, disable the rules `phpstan.neon.dist` already registers, or each error they report is reported twice:

```neon
includes:
- phpstan.neon.dist
- vendor/phpstan/phpstan-strict-rules/rules.neon

parameters:
strictRules:
allRules: false
disallowedLooseComparison: true
overwriteVariablesWithLoop: true

# Already registered in phpstan.neon.dist.
disallowedBacktick: false
disallowedShortTernary: false
closureUsesThis: false
matchingInheritedMethodNames: false
```

The rules for `while` and `do`-`while` conditions are only enabled with [bleeding edge](https://phpstan.org/blog/what-is-bleeding-edge) (as are those for unary `+` and `-` operands), so `booleansInConditions: true` alone does not cover loops. Set `booleansInLoopConditions: true` to enable them without it.

### Seeing every error

The baselines and other ignored errors cannot be removed by leaving out an include, but a key ending in `!` replaces the value it would otherwise be merged with. So to see every error, including those the baselines suppress:

```neon
includes:
- phpstan.neon.dist

parameters:
ignoreErrors!: []
```

For more information about configuring PHPStan, see the [PHPStan documentation's Config reference](https://phpstan.org/config-reference).

Expand Down
8 changes: 7 additions & 1 deletion tests/phpstan/base.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Base PHPStan configuration for WordPress Core.
#
# This is kept separate from the main PHPStan configuration file to allow for easy overloading while baseline errors are being fixed.
# This holds what PHPStan needs to know to analyze the WordPress codebase: the extensions for
# WordPress-specific conventions, the paths analyzed and excluded, the supported PHP versions,
# and the constants whose values vary. It rarely changes.
#
# The standards the code is held to, which are the rule level, the baselines, the errors
# ignored as not actionable, and the strict rules enabled, are in phpstan.neon.dist instead,
# so they can be read and reviewed apart from all of this.
#
# https://phpstan.org/config-reference

Expand Down
Loading