FILE: /var/www/html/drupal_core/core/modules/jsonapi/src/Controller/EntryPoint.php
-------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
-------------------------------------------------------------------------------------------------
107 | WARNING | User::load calls should be avoided in classes, use dependency injection instead

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

swatichouhan012 created an issue. See original summary.

swatichouhan012’s picture

Issue summary: View changes
swatichouhan012’s picture

Title: User::load calls should be avoided in classes, use dependency injection instead in user module » User::load calls should be avoided in classes, use dependency injection instead in jsonapi module
Component: user.module » jsonapi.module
Wim Leers’s picture

Issue tags: +Novice

This would be a great novice patch :)

Why is this marked 9.0.x?

Wim Leers’s picture

Issue tags: +API-First Initiative
saurabh.rocksoul’s picture

Assigned: swatichouhan012 » Unassigned
Status: Active » Needs review
FileSize
2.16 KB

Thanks @swatichouhan012 for creating the issue.
Here is the patch for the same. Please review.

swatichouhan012’s picture

Thanks @Wim Leers @saurabh.rocksoul to check this issue. @Wim Leers i tagged this with 9.0.x because i checked in latest version should i check this with another one and then reroll ?

Wim Leers’s picture

Version: 9.0.x-dev » 8.9.x-dev
Status: Needs review » Needs work
  1. +++ b/core/modules/jsonapi/src/Controller/EntryPoint.php
    @@ -51,9 +59,10 @@ class EntryPoint extends ControllerBase {
        * @param \Drupal\Core\Session\AccountInterface $user
        *   The current user.
        */
    -  public function __construct(ResourceTypeRepositoryInterface $resource_type_repository, AccountInterface $user) {
    +  public function __construct(ResourceTypeRepositoryInterface $resource_type_repository, AccountInterface $user, EntityTypeManagerInterface $entityTypeManager) {
    

    This is updating the constructor, but not its documentation. Could you update that too? :)

  2. +++ b/core/modules/jsonapi/src/Controller/EntryPoint.php
    @@ -51,9 +59,10 @@ class EntryPoint extends ControllerBase {
    +    $this->entityTypeManager = $entityTypeManager;
    

    In Drupal core, we don't camelcase function arguments. So it should not be $entityTypeManager, but $entity_type_manager.


#7 I think it can go in 8.9.x too — I think it can be committed to both 8.9.x and 9.0.x 😊

swatichouhan012’s picture

Assigned: Unassigned » swatichouhan012
swatichouhan012’s picture

Assigned: swatichouhan012 » Unassigned
Status: Needs work » Needs review
FileSize
2.46 KB
1.57 KB

I have updated code wrt 8.9.x and 9.0.x , also removed unused use case, kindly review.

Wim Leers’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/jsonapi/src/Controller/EntryPoint.php
    @@ -43,6 +43,13 @@ class EntryPoint extends ControllerBase {
    +  protected $entity_type_manager;
    

    The naming here is incorrect. This should be $entityTypeManager. (I know, it's weird and confusing, but all of Drupal does it this way for historical reasons!)

  2. +++ b/core/modules/jsonapi/src/Controller/EntryPoint.php
    @@ -50,10 +57,13 @@ class EntryPoint extends ControllerBase {
    +   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
    

    The naming here is correct. That's indeed what I asked in #8.2 :)

  3. +++ b/core/modules/jsonapi/src/Controller/EntryPoint.php
    @@ -50,10 +57,13 @@ class EntryPoint extends ControllerBase {
    +    $this->entityTypeManager = $entity_type_manager;
    

    This is correct too :)

Just one more reroll! 🤞

swatichouhan012’s picture

Status: Needs work » Needs review
FileSize
2.46 KB
424 bytes

@Wim Leers i have updated patch according comment #11, kindly review.

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community

Perfect! :)

Thank you, and thanks for your patience, @swatichouhan012!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/jsonapi/src/Controller/EntryPoint.php
@@ -50,10 +57,13 @@ class EntryPoint extends ControllerBase {
-  public function __construct(ResourceTypeRepositoryInterface $resource_type_repository, AccountInterface $user) {
+  public function __construct(ResourceTypeRepositoryInterface $resource_type_repository, AccountInterface $user, EntityTypeManagerInterface $entity_type_manager) {
     $this->resourceTypeRepository = $resource_type_repository;
     $this->user = $user;
+    $this->entityTypeManager = $entity_type_manager;
   }

@@ -104,7 +115,7 @@ public function index() {
+      $current_user_uuid = $this->entityTypeManager->getStorage('user')->load($this->user->id())->uuid();

We need to account for backwards compatibility - ie. we need to allow passing in a NULL for $entity_type_manager... but this is extending ControllerBase - so we could avoid this and do $this->entityTypeManager()->getStorage('user')->load() instead.

Also I think we need to think about scope here. Given that you're using a code scanner and coder rule to find this I don;t think a scope of 1 issue per warning is the way to go.

saurabh.rocksoul’s picture

Status: Needs work » Needs review
FileSize
2.02 KB
1.46 KB

Hey @alexpott, Thanks for pointing out the ControllerBase class. I have chnage the code keeping in mind this class.
Please review.

saurabh.rocksoul’s picture

Forgot to remove a comment from the code.
Uploading new patch. Please review this.
Thanks.

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community

#14:
Oh, hah, didn't realize it already lived on ControllerBase 😅🙈 Agreed with using that instead. (Now irrelevant, but I disagree with the need for BC for EntryPoint's constructor: it's a controller owned by the JSON:API module and marked as @internal.)

Marking RTBC for the code changes.

Also I think we need to think about scope here. Given that you're using a code scanner and coder rule to find this I don;t think a scope of 1 issue per warning is the way to go.

Agreed — but in this case they didn't file dozens of issues for dozens of core modules. So … I think this is okay. But you're used to dealing with this much more often, so I obviously defer to you. Feel free to un-RTBC over this and ask to expand the scope to all of core of course.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

  • xjm committed 8b7aee5 on 9.1.x
    Issue #3115772 by saurabh.rocksoul, swatichouhan012, Wim Leers, alexpott...

  • xjm committed acfa2e5 on 9.0.x
    Issue #3115772 by saurabh.rocksoul, swatichouhan012, Wim Leers, alexpott...

  • xjm committed 26bb167 on 8.9.x
    Issue #3115772 by saurabh.rocksoul, swatichouhan012, Wim Leers, alexpott...
xjm’s picture

Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Fixed

OK, that turned out to be very simple. :)

It's a very small cleanup, and usually I would prefer to see this done as a part of the coding standards cleanup initiative, where we replace the various load() calls across all of core according to the type of issue. In order to fix core coding standards in a maintainable way, all our coding standards issues should be done on a per-rule basis across all of core, rather than fixing standards in individual modules or files. We should also separate fixes where we need to write new documentation from fixes where we need to correct existing standards. This all should be done as part of #2571965: [meta] Fix PHP coding standards in core. A good place to start is the child issues of #2572645: [Meta] Fix 'Drupal.Commenting.FunctionComment' coding standard.

For background information on why we usually will not commit coding standards fixes that aren't scoped in that way, see the core issue scope guidelines, especially the note about coding standards cleanups. That document also includes numerous suggestions for scoping issues including documentation coding standards cleanups.

Contributing to the overall plan above will help ensure that your fixes for core's coding standards remain in core the long term.

So, usually I would mark this as a duplicate of a large issue, but this one time I'll go ahead and commit it, since it's done. And since the change should be purely transparent, it's safe to backport during beta, so backporting as far as 8.9.x.

Thanks! I encourage you to submit complete fixes in the future, rather than one-line fixes.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.