Make WordPress Core

Opened 7 weeks ago

Last modified 3 weeks ago

#64680 accepted task (blessed)

Increase PHPStan rule level from zero

Reported by: westonruter's profile westonruter Owned by: westonruter's profile westonruter
Milestone: 7.1 Priority: normal
Severity: normal Version:
Component: Build/Test Tools Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

PHPStan is now integrated into the core development workflow as of #61175. The initial commit (r61699), however, set the rule level to 0 (zero).

That ticket initially had a PR which set the rule level to 6, with initial baselines to ignore existing errors but to catch new ones as they are introduced.

We can now move forward with that.

Change History (7)

#1 @westonruter
7 weeks ago

I resolved the merge conflicts in the PR which set the rule level to 6, by merging the commits from the sub-PR back into it and then merging in trunk to assist with Git's conflict resolution. So there are a ton of commits on that PR, but now the actual number of files changes is quite small.

This ticket was mentioned in PR #11037 on WordPress/wordpress-develop by @westonruter.


7 weeks ago
#2

  • Keywords has-unit-tests added

This PR is based off of https://github.com/WordPress/wordpress-develop/pull/7619, see additional commits: https://github.com/justlevine/wordpress-develop/compare/feat/phpstan...westonruter:wordpress-develop:bump/phpstan-level-4

Trac ticket: https://core.trac.wordpress.org/ticket/64680

By increasing the PHPStan rule level to 4, the following issues will begin to be detected for new code introduced to core:

  1. possibly undefined variables, unknown magic methods and properties on classes with __call and __get
  2. unknown methods checked on all expressions (not just $this), validating PHPDocs
  3. return types, types assigned to properties
  4. basic dead code checking - always false instanceof and other type checks, dead else branches, unreachable code after return; etc.

Existing errors for these levels are ignored by a baseline which has been updated to include 641 errors:

Count | PHPStan Identifier
-- | --
56 | while.alwaysTrue
55 | deadCode.unreachable
53 | property.notFound
52 | property.nonObject
36 | isset.property
34 | return.type
29 | staticClassAccess.privateMethod
23 | assign.propertyType
19 | property.defaultValue
16 | class.notFound
14 | method.unused
13 | method.nonObject
13 | function.alreadyNarrowedType
12 | booleanAnd.rightAlwaysTrue
10 | return.unusedType
10 | notIdentical.alwaysTrue
9 | empty.variable
9 | empty.property
9 | arguments.count
8 | varTag.noVariable
8 | greaterOrEqual.alwaysTrue
8 | constant.notFound
8 | booleanNot.alwaysTrue
7 | property.private
7 | parameterByRef.type
7 | method.childParameterType
6 | phpDoc.parseError
6 | if.alwaysFalse
5 | property.phpDocType
5 | parameter.defaultValue
5 | nullCoalesce.property
5 | method.notFound
5 | isset.variable
5 | isset.offset
5 | if.alwaysTrue
4 | variable.undefined
4 | parameter.notFound
4 | identical.alwaysFalse
4 | booleanAnd.leftAlwaysTrue
3 | return.missing
3 | property.protected
3 | offsetAccess.notFound
3 | booleanNot.alwaysFalse
3 | booleanAnd.alwaysFalse
3 | binaryOp.invalid
2 | ternary.alwaysFalse
2 | return.empty
2 | property.unusedType
2 | parameterByRef.unusedType
2 | offsetAccess.nonOffsetAccessible
2 | nullCoalesce.offset
2 | identical.alwaysTrue
2 | function.impossibleType
2 | empty.offset
2 | booleanOr.alwaysTrue
1 | while.alwaysFalse
1 | ternary.alwaysTrue
1 | smallerOrEqual.alwaysTrue
1 | property.onlyWritten
1 | parameter.unresolvableType
1 | parameter.phpDocType
1 | offsetAssign.valueType
1 | instanceof.alwaysTrue
1 | foreach.nonIterable
1 | encapsedStringPart.nonString
1 | constructor.unusedParameter
1 | booleanOr.rightAlwaysTrue
1 | booleanOr.alwaysFalse
1 | booleanAnd.rightAlwaysFalse
1 | booleanAnd.alwaysTrue

## Use of AI Tools

Summarizing the ignored errors in the baseline.

#3 @westonruter
7 weeks ago

See above for PR which bumps the rule level to 4.

#4 @westonruter
7 weeks ago

  • Owner set to westonruter
  • Status changed from new to accepted

This ticket was mentioned in PR #11151 on WordPress/wordpress-develop by @westonruter.


6 weeks ago
#5

By increasing the PHPStan rule level to 1, the following issues will begin to be detected for new code introduced to core:

possibly undefined variables, unknown magic methods and properties on classes with __call and __get

Here is the tally of ignored errors in tests/phpstan/baseline.php categorized by their PHPStan identifier:

Identifier Description Error Count
:--- :--- :---:
empty.variable Using empty() on variables that always exist, are always falsy, or are not falsy 9
constant.notFound Usage of constants that are not found or defined in the analyzed scope 9
isset.variable Using isset() on variables that always exist, are not nullable, or are never defined 5
variable.undefined Accessing variables that are never defined or are possibly undefined 4
arguments.count Functions or methods invoked with more parameters than they actually require 4
constructor.unusedParameter Class constructors that have parameters that are never used 1
Total 32

See also https://github.com/WordPress/wordpress-develop/pull/11037 which bumps to level 4.

Trac ticket: https://core.trac.wordpress.org/ticket/64680

## Use of AI Tools

n/a

#6 @westonruter
4 weeks ago

In 62000:

Twenty Seventeen: Restore $post_id parameter to twentyseventeen_edit_link() which is being passed in some templates.

This addresses arguments.count errors identified by PHPStan at rule level 1.

Developed in https://github.com/WordPress/wordpress-develop/pull/11242

Follow-up to WordPress/twentyseventeen commits: 1afb73c, 7dd3025, 6948288.

Props sabernhardt, westonruter.
See #64680.
Fixes #64825.

#7 @westonruter
3 weeks ago

  • Milestone changed from 7.0 to 7.1

Punting since we're at RC1.

Note: See TracTickets for help on using tickets.