Report the deprecated original property on values typed as EntityInterface

Open
#1,079 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
drupal, php

Research direction

Start by reading the existing deprecation handling in EntityFieldReflection and the rules that skip DeprecationHelper::backwardsCompatibleCall() callbacks. Add a rule for PropertyFetch nodes named original, checking entity subtypes on Drupal 11.2 and later and distinguishing reads, isset(), assignments, and unset(). Cover entity types without duplicate reports from EntityFieldReflection; done means the proposed accesses are reported correctly and compatible calls are skipped.

Written by the indexing model from the issue text.

Description

#1076 reports the deprecated original property through EntityFieldReflection. That covers values typed as a content entity class, ContentEntityInterface, or a config entity class. It misses the most common case:

function mymodule_entity_presave(EntityInterface $entity): void {
  if ($entity->original->label() !== $entity->label()) {
    // ...
  }
}

PHPStan only asks property reflection extensions about an interface when the interface is in universalObjectCratesClasses. ContentEntityInterface is there, but EntityInterface and ConfigEntityInterface aren't. Adding them would allow any property on config entities, so that isn't an option.

hook_entity_presave(), hook_entity_update(), and entity event subscribers all receive EntityInterface. That's where most code reads original, and it breaks on Drupal 12 without a warning.

Proposal

Add a rule that checks ->original directly instead of going through property reflection:

  • Report PropertyFetch nodes named original when the variable's type is a subtype of EntityInterface, on Drupal 11.2 and later.
  • Tell reads and isset() apart from assignments and unset(). Core recommends getOriginal() for the first two and setOriginal() for the others. A property reflection can't make that distinction.
  • Skip code inside DeprecationHelper::backwardsCompatibleCall() callbacks, like other deprecations.

If the rule covers every entity type, #1076's deprecation in EntityFieldReflection can be removed so the same access isn't reported twice.

Dominant language
PHP
Stars
208
Forks
88
Avg merge
2d 17h
Merged PRs (30d)
15

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mglaman/phpstan-drupal

All issues in mglaman/phpstan-drupal

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.