Problem/Motivation
The methods loadRevision() / loadMultipleRevisions() allowed NULL values as input without throwing an error or notice, and just returned NULL if no entity was found (which is obvious since there can't be an entity with a NULL ID).
In #3019030: Ensure loadMultipleRevisions() returns the revisions in the same order as the passed IDs an array_flip() call was added to loadMultipleRevisions(), which now shows a warning array_flip(): Can only flip STRING and INTEGER values!.
The methods load() and loadMultiple() already had this "issue", since loadMultiple() also contains a call to array_flip().
While you could argue the calling code should not call the method with "garbage" input, this means that calling code always needs to do 2 checks:
1. Is the input a string or int.
2. Is the return value of the specific load method not NULL.
It would be nice if you could call load() / loadMultiple() / loadRevision() / loadMultipleRevisions() without having to check the input, and let the load methods return NULL if it can't find an entity. I've seen methods (like ContentEntityStorageBase::getLatestTranslationAffectedRevisionId()) that could return a entity ID or NULL and I think the DX would be a bit better if you don't have to do the extra check.
Steps to reproduce
drush php-eval "Drupal::entityTypeManager()->getStorage('node')->loadMultiple(['x'=>['x']]);"
Proposed resolution
Check the input for load() / loadMultiple() / loadRevision() / loadMultipleRevisions() and simply return NULL if it's not a string or int.
Remaining tasks
Discuss
Patch
Review
Commit
Comments
Comment #2
seanbComment #3
jhedstromThis may be a duplicate of #3035980: Provide a better error when a NULL is passed to EntityStorageBase::load(), although I don't think that patch currently covers the revision storage.
Comment #8
manuel.adanSeems dup of #2762737: ContentEntityStorageBase::loadRevision(NULL) returns an entity.
Comment #9
jhedstromWe can probably address these methods the same as was done in #3019030: Ensure loadMultipleRevisions() returns the revisions in the same order as the passed IDs which was to use
assert(). Note that this should no longer be an issue onloadMultipleRevisionssince it is type-hinted to takearrayas its parameter, so passing null in there already results in immediate feedback to the developer.Comment #11
joachim commentedDuplicate of #3221534: throw an exception when IDs passed to loadMultiple() are badly formed?
Comment #12
seanbI guess so. Same problem, different solution :)
Comment #16
quietone commentedClosed #3048490: Warning: array_flip(): Can only flip STRING and INTEGER values! as a duplicate and adding credit. Updated IS with STR from @damondt in the other issue.