Skip to content

refactor(types): analyse entity at PHPStan level 8 - #26

Merged
ogorzalka merged 4 commits into
mainfrom
chore/phpstan-level-up
Nov 26, 2026
Merged

ogorzalka merged 4 commits into
mainfrom
chore/phpstan-level-up

Conversation

@ogorzalka

Copy link
Copy Markdown
Member

Branch chore/phpstan-level-up, base main. One commit per level (6, 7, 8), then a test. The baseline (2 entries) is not emptied, and the level was raised anyway: see below.

What changes

  • Level 6: array value types and types for nine untyped properties, PHPDoc only.
  • Level 7: the @var of the properties whose setters accept null (or a boolean) now says so (publiclyQueryable, showUi, menuPosition, rewrite, templateLock, ...). These corrections also clear level 8.
  • One code line: EntityRegistrationService passes $entity->getPriority() ?? $entity->priority to add_action. getPriority() is declared ?int; only a subclass override can return null, which used to reach add_action as null. This is a guard asked for by the types, not a reported bug; a null declaration priority falls back to the declared priority fails without it (the test stub of add_action now records the priority).
  • No signature changes. Ratchet recorded with vendor/bin/pollora-qa ratchet --update (the tool also re-sorts the keys and writes 98.0 as 98).

Baseline: blocked by the public API

Both entries are constructor.unusedParameter: the first constructor argument of PostTypeService ($postTypeRegistry) and TaxonomyService ($taxonomyRegistry). The classes are public and not final, and the argument is positional (src/Taxonomy.php, tests, any application that builds them). Removing it breaks every caller, so it needs a major release:

  1. now (optional): mark the parameter @deprecated in both docblocks and the CHANGELOG/README;
  2. next major: remove the parameter in both constructors, the argument in src/Taxonomy.php (new TaxonomyService(...)) and in the two service tests;
  3. then, in the same PR, a maintainer runs:
git rm phpstan-baseline.neon          # and remove the line "- phpstan-baseline.neon" in phpstan.neon
vendor/bin/pollora-qa ratchet --update --metric=phpstan_baseline:0

Stopped before level 9

Level 9 reports 15 errors on src. Some are internal narrowing, but these cannot be satisfied without tightening the documented contract of public ports (PostTypeRegistryPort, TaxonomyRegistryPort, EntityRegistryPort), currently array<array-key, mixed>:

  • register_extended_post_type / register_extended_taxonomy: $names must be array{plural?: string, singular?: string, slug?: string}, $objectType array<string>|string;
  • register_post_type / register_taxonomy: $args must match the WordPress array shape, $slug must be a non-empty (lowercase) string.

The alternative is a validation layer in the adapters that drops values at run time. Both are decisions for the maintainer. The other reports (offsets on mixed, Entity::getSlug() returning mixed, PostType::$taxonomies) can be fixed with them.

Checked (PHP 8.4, macOS)

  • vendor/bin/phpstan analyse level 8 (src, the harness paths): 0 errors with the two baselined ones.
  • Pest: 271 passed, 2 incomplete, 643 assertions.
  • QA-05 (roave-backward-compatibility-check --from=2.0.0, in a local clone): no backwards-incompatible change.
  • Mutation score 96.91 % (floor 95).
  • composer qa:fast and composer qa: every gate that ran passed. Line coverage 98.86 % (floor 98).
  • tests/Unit/MutationRegistrationTest.php: four PHPStan reports cleared, because qa:fast analyses the changed files (two of them already existed at level 5). Other test files still report when they change.

Not verified

  • composer phpstan (hard-coded src tests) was already failing on main (81 errors at level 5, mostly deprecated-method calls in tests); its level is bumped with the rest and it reports 195 errors at level 8. CI does not run it.
  • The framework downstream job was not run. Semgrep not installed.

Pollora maintainer added 4 commits November 26, 2026 09:49
Value types for the array properties, parameters and returns, and types for the untyped flags, in PHPDoc only: no signature changes.
Correct the @var of the flags and settings whose setters accept null or a boolean, as they behave at run time (also clears the level 8 reports); a null declaration priority falls back to the declared one.
No further change needed after the corrections of level 7.
Also clears the PHPStan reports of that test file, which the fast gates analyse when it changes (two of them were already there at level 5).
@ogorzalka
ogorzalka merged commit 7eacd76 into main Nov 26, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant