Skip to content

Commit ec36772

Browse files
committed
Do not resolve entity type for generic storages
1 parent 61902bd commit ec36772

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Drupal/EntityDataRepository.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace mglaman\PHPStanDrupal\Drupal;
44

5+
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
6+
use Drupal\Core\Entity\ContentEntityStorageInterface;
7+
use Drupal\Core\Entity\EntityStorageInterface;
58
use PHPStan\Type\ObjectType;
69

710
final class EntityDataRepository
@@ -34,6 +37,15 @@ public function get(string $entityTypeId): EntityData
3437

3538
public function resolveFromStorage(ObjectType $callerType): ?EntityData
3639
{
40+
if ($callerType->equals(new ObjectType(EntityStorageInterface::class))) {
41+
return null;
42+
}
43+
if ($callerType->equals(new ObjectType(ConfigEntityStorageInterface::class))) {
44+
return null;
45+
}
46+
if ($callerType->equals(new ObjectType(ContentEntityStorageInterface::class))) {
47+
return null;
48+
}
3749
foreach ($this->entityData as $entityData) {
3850
$storageType = $entityData->getStorageType();
3951
if ($storageType !== null && $callerType->isSuperTypeOf($storageType)->yes()) {

tests/src/Type/data/bug-377.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,49 @@
22

33
namespace bug377;
44

5+
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
6+
use Drupal\Core\Entity\ContentEntityStorageInterface;
57
use Drupal\Core\Entity\EntityStorageInterface;
68
use Drupal\Core\Entity\EntityTypeManagerInterface;
9+
use Drupal\node\NodeStorageInterface;
710
use function PHPStan\Testing\assertType;
811

912
class Foo {
1013
private EntityTypeManagerInterface $entityTypeManager;
1114
private EntityStorageInterface $myEntityStorage;
15+
private ConfigEntityStorageInterface $configEntityStorage;
16+
private ContentEntityStorageInterface $contentEntityStorage;
17+
private NodeStorageInterface $nodeStorage;
1218

1319
public function __construct(EntityTypeManagerInterface $entityTypeManager)
1420
{
1521
$this->entityTypeManager = $entityTypeManager;
1622
$this->myEntityStorage = $entityTypeManager->getStorage('node');
23+
$this->configEntityStorage = $entityTypeManager->getStorage('block');
24+
$this->contentEntityStorage = $entityTypeManager->getStorage('node');
25+
$this->nodeStorage = $entityTypeManager->getStorage('node');
1726
}
1827

1928
public function storageType() {
2029
// @todo property typing overrides our internal typing to determine entity storage type.
21-
assertType('Drupal\Core\Entity\EntityStorageInterface', $this->myEntityStorage);
30+
assertType(EntityStorageInterface::class, $this->myEntityStorage);
2231
assertType('Drupal\node\NodeStorage', $this->entityTypeManager->getStorage('node'));
32+
assertType(ConfigEntityStorageInterface::class, $this->configEntityStorage);
33+
assertType(ContentEntityStorageInterface::class, $this->contentEntityStorage);
34+
assertType(NodeStorageInterface::class, $this->nodeStorage);
2335
}
2436

2537
public function entityType() {
2638
$entity = $this->myEntityStorage->load('123');
2739
assertType('Drupal\Core\Entity\EntityInterface|null', $entity);
40+
$entity = $this->configEntityStorage->load('123');
41+
// @todo this can safely say it is ConfigEntityInterface as return type.
42+
assertType('Drupal\Core\Entity\EntityInterface|null', $entity);
43+
$entity = $this->contentEntityStorage->load('123');
44+
// @todo this can safely say it is ConfigEntityInterface as return type.
45+
assertType('Drupal\Core\Entity\EntityInterface|null', $entity);
46+
$entity = $this->nodeStorage->load('123');
47+
assertType('Drupal\node\Entity\Node|null', $entity);
2848
}
2949

3050
}

0 commit comments

Comments
 (0)