Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more specific typehint for EntityStorageInterface::loadMultiple() #827

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Niklan
Copy link
Contributor

@Niklan Niklan commented Feb 17, 2025

Problem/motivation

Drupal has received these changes to the core: https://git.drupalcode.org/project/drupal/-/commit/bc6599bf54ebd08b971db297ba0153a1d5711efb

One of them is about EntityStorageInterface:

  /**
   * Resets the internal entity cache.
   *
-  * @param $ids
+  * @param int[]|null $ids
   *   (optional) If specified, the cache is reset for the entities with the
   *   given ids only.
   */
  /**
   * Loads one or more entities.
   *
-  * @param $ids
+  * @param int[]|null $ids
   *   An array of entity IDs, or NULL to load all entities.
   *
   * @return \Drupal\Core\Entity\EntityInterface[]

This is a completely incorrect type, which already leads to issues with PHPStan (the next major version). For example, we encountered this today in a custom project:

Parameter #1 $ids of method Drupal\Core\Entity\EntityStorageInterface::loadMultiple() expects array<int>|null, array<int, int<1, max>|string> given.  

There are multiple problems at the same time:

  • Entity IDs are not guaranteed to be integer, more likely they will be strings.
  • Drupal\Core\Entity\EntityInterface::id() specifies return types as @return string|int|null, which already makes a simple use of ::loadMultiple([$node->id()]) completely incorrect.
  • Drupal\Core\Config\Entity\ConfigEntityStorageInterface extends this interface, making it problematic as well, because in general, Drupal configurations use strings as their IDs, not integers.

This PR makes the types wider and more appropriate, which can actually be used for entity storage. I think it should remain in this package until Drupal core resolves this issue. It appears that this is not the only issue that needs fixing, and it was simply pushed into the upstream "as is".

@Niklan
Copy link
Contributor Author

Niklan commented Feb 17, 2025

Reported to upstream: https://www.drupal.org/project/drupal/issues/3507191

@Niklan
Copy link
Contributor Author

Niklan commented Feb 17, 2025

Also, I'm interested in how to "test" such cases in terms of PHPStan? I couldn't find any documentation about how we can test a specific stub. All the tests assert types for the variables, but there doesn't seem to be a way to assert reports like this, unless you test a rule?

@Niklan
Copy link
Contributor Author

Niklan commented Feb 17, 2025

The same issues as in #826; again, it looks like an upstream issue, and this is a false positive.

@mglaman
Copy link
Owner

mglaman commented Feb 20, 2025

For testing, there are the data type tests. I'm mobile but will try to link later. There should be a test which verifies our field stubs as an example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants