Problem/Motivation
There are a few places in this module where \Drupal:: calls are used in classes. Best practice is to use dependency injection instead.
References:
- https://www.drupal.org/docs/drupal-apis/services-and-dependency-injectio...
- https://drupal.stackexchange.com/questions/207151/drupal-calls-should-be...
This is a follow-up to #3524246: Fix PHPStan errors, which added a phpstan-baseline.neon file to document and ignore these errors and allow PHPStan tests to pass.
Steps to reproduce
Remove the phpstan-baseline.neon file from the root directory of the codebase and run PHPStan. Observe the following errors:
------ ------------------------------------------------------------------------------
Line web/modules/jsonapi_schema/src/Normalizer/DataDefinitionNormalizer.php
------ ------------------------------------------------------------------------------
91 \Drupal calls should be avoided in classes, use dependency injection instead
------ ------------------------------------------------------------------------------
------ -------------------------------------------------------------------------------------
Line web/modules/jsonapi_schema/src/Normalizer/RelationshipFieldDefinitionNormalizer.php
------ -------------------------------------------------------------------------------------
104 \Drupal calls should be avoided in classes, use dependency injection instead
------ -------------------------------------------------------------------------------------
Proposed resolution
We should use dependency injection instead of \Drupal calls.
However, doing so would mean changing the __construct() arguments for our DataDefinitionNormalizer class, which all of our normalizer classes extend from, and which may be extended by contrib/custom normalizers downstream. As such, this should be considered a breaking change, and should not be done until we are ready to take a major version release, in line with semantic versioning.
Here is the parent issue for version 2.0: #3456616: [Meta, Plan] Version 2.0.
Remaining tasks
- Open a merge request that removes
phpstan-baseline.neonand replaces\Drupalcalls with dependency injection. - Keep the merge request updated if new
\Drupalcalls are introduced after #3524246: Fix PHPStan errors is merged. - Merge into the
2.xbranch. - Document this as a breaking change in the 2.0.0 release notes.
User interface changes
None.
API changes
This will change the __construct() parameters of the JSON:API Schema normalizer classes. Downstream classes in contrib or custom modules may need to update their code accordingly, if they are overriding the __construct() method themselves.
Data model changes
None.
Issue fork jsonapi_schema-3524925
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
m.stentaComment #4
m.stentaOpened a merge request, and tests all passed.
Marking this as "postponed" on #3456616: [Meta, Plan] Version 2.0.
Comment #5
m.stenta