Problem/Motivation
When a class supplies inconsistent or faulty configuration, even a well-tested helper can produce a misleading notice or none at all. Not even within Drupal Core can we, however, count on a dedicated test being written, say, for every property deprecated with DeprecatedServicePropertyTrait, let alone in contrib or even custom code.
Yet many mistakes are visible in the definition itself. The issue URL may name a different project from the version strings. The removal version may come before the deprecation version or refer to a minor rather than a major release. We could check these constraints at runtime directly in our __get() method, but each read of a deprecated property would then cost a few extra opcodes to validate the same definition again and again, even though we only need to validate it once for every code change: at commit time.
Now, PHP can compile assertions out entirely with zend.assertions=-1, but they would still add validation logic to the helper. More importantly, even with assertions enabled, an invalid definition can go undetected as long as no test or request reads that particular property. If none of our automated tests ever reaches this path, we would be thrown back on manual testing.
DeprecatedServicePropertyTrait is a good starting point for exploring what PHPStan level 3 can do for us. Custom PHPStan rules can check how each entry in $deprecatedProperties is structured and whether its values are consistent with one another, even if no test ever reads the corresponding property. Devs using a modern IDE with PHPStan integration can get early feedback right at the declaration. They can also get that feedback while committing locally if PHPStan is included in a Git pre-commit hook.
Several other deprecation helpers could benefit from this approach. The same checks could validate release and link metadata in the YAML definitions BackwardsCompatibilityClassLoader and LibraryDiscoveryParser use for moved classes and assets. Other candidates include DeprecationHelper, DeprecatedArray, DeprecatedServicePass, and deprecation code in Settings, State, ConfigActionManager and ModuleHandler.
Proposed resolution
- Start with
DeprecatedServicePropertyTrait. - Define reusable PHPStan types for service IDs, project versions, issue URLs, and change record URLs.
- Combine these PHPStan types in an array shape classes can import when declaring
$deprecatedProperties. - Add a PHPStan rule for constraints spanning several fields. Each entry must provide an issue URL or a change record URL. Both version strings must refer to the same project, and if the issue URL names a project, it must match as well. The removal version also has to be one of the next two major releases.
- Keep these checks as generic and reusable as possible so other deprecation helpers, and later other code, can use the ones they need.
Remaining tasks
Postponed on #3519400: Update DeprecatedServicePropertyTrait for Drupal 12+
Review.
User interface changes
None.
Introduced terminology
None.
API changes
Developers can use
string-patternandurl
in PHPDoc to describe strings and URLs with typed placeholders.
For example,
string-pattern<'{project-id}:{drupal-tagged-release}'>
describes a project-prefixed release, and
url<'https://www.drupal.org/node/{positive-int}'>
describes a Drupal.org node URL.
Developers can also use the new Drupal-specific PHPStan types
service-id,project-id,drupal-tagged-release, anddrupal-major-release
directly or combine them in custom array shapes.
Specifically for deprecation metadata, Drupal provides
deprecation-version,removal-version,deprecation-issue, andchange-record.
Data model changes
None.
Release notes snippet
Developers can now use PHPStan to catch inconsistent deprecation metadata for service properties before the code runs. The same types are available to annotate and check similar metadata in contributed and custom code.
Issue fork drupal-3642960
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
quietone commentedMoving what this is postponed on to the remaining tasks per Remining tasks.
And, it seems this should be postponed
Comment #3
pancho