Problem/Motivation

phpstan-drupal 2.1.1 introduced two rules that flag a pre-existing pattern across Patternkit's production service classes:

  • drupal.entityStoragePropertyAssignment: fires when an EntityStorageInterface object is stored as a class property.
  • drupal.entityStorageDirectInjection: fires when entity storage is injected directly as a constructor parameter rather than via EntityTypeManagerInterface.

The Drupal best practice is to inject EntityTypeManagerInterface and call $this->entityTypeManager->getStorage('entity_type') at each call site. Storing a resolved storage instance as a property can cause stale references after cache clears and is incompatible with DependencySerializationTrait.

Issue #3612106: PHPStan CI fails on 9.1.x after phpstan-drupal package update temporarily suppressed all 22 violations in phpstan-baseline.neon as part of a CI-hotfix. The suppression comment marks them for future refactoring. This issue tracks that refactoring for production files.

Proposed resolution

For each affected file:

  1. Replace the injected EntityStorageInterface parameter with EntityTypeManagerInterface.
  2. Remove the stored storage property.
  3. Call $this->entityTypeManager->getStorage('entity_type') at each former call site.
  4. Update constructor signatures, service definitions, and any unit test factories that construct the class directly.

After all files are updated, remove the corresponding entries from phpstan-baseline.neon and confirm PHPStan CI passes without them.

Affected files

All violations are drupal.entityStoragePropertyAssignment unless noted.

  • src/Controller/PatternkitController.php (1x entityStoragePropertyAssignment, 1x entityStorageDirectInjection)
  • src/Plugin/Block/PatternkitBlock.php (2x entityStorageDirectInjection)
  • src/Plugin/Derivative/PatternkitBlock.php (2x entityStorageDirectInjection)
  • src/Hook/PatternkitBlockEntityHooks.php (2x)
  • src/UpdateHelper.php (2x)
  • src/Asset/PatternDependencyResolver.php (1x)
  • src/Asset/PatternLibraryParserBase.php (1x)
  • src/BulkOperations/Worker/LayoutDiscoveryBatchWorker.php (2x)
  • src/JsonEditorFormBuilder.php (1x)
  • src/LayoutHelper.php (1x)
  • src/PatternRepository.php (1x)
  • modules/patternkit_usage_tracking/src/Commands/PatternkitUsageTrackingCommands.php (1x)
  • modules/patternkit_usage_tracking/src/PatternUsageManager.php (1x)

Total: 17x entityStoragePropertyAssignment, 5x entityStorageDirectInjection across 13 files.

Remaining tasks

  • Refactor each affected file as described above.
  • Remove corresponding baseline entries from phpstan-baseline.neon.
  • Review and test the merge request.
  • CI validation.

User interface changes

None.

Introduced terminology

None.

API changes

Constructor signatures will change for the affected classes (storage parameters removed, EntityTypeManagerInterface retained or added). These classes are internal services not intended for external extension; no public API break is expected. Any custom code that constructs these classes directly will need to be updated.

Data model changes

None.

Release notes snippet

Internal refactoring of service constructors to follow Drupal's entity storage injection best practices. No behavior changes. Sites using only Patternkit's public API are unaffected.

Comments

slucero created an issue.