Skip to content

Commit ac8b890

Browse files
authored
Merge pull request #300 from dpi/getstorage-with-constant
Calling getStorage with a constant does not resolve entity mapped storage class
2 parents 2d7cc0f + 27e31ee commit ac8b890

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/Type/EntityTypeManagerGetStorageDynamicReturnTypeExtension.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPStan\Reflection\ParametersAcceptorSelector;
1818
use PHPStan\Reflection\ReflectionProvider;
1919
use PHPStan\ShouldNotHappenException;
20+
use PHPStan\Type\Constant\ConstantStringType;
2021
use PHPStan\Type\DynamicMethodReturnTypeExtension;
2122
use PHPStan\Type\ObjectType;
2223

@@ -74,13 +75,15 @@ public function getTypeFromMethodCall(
7475
if ($arg1 instanceof Concat) {
7576
return $returnType;
7677
}
77-
if (!$arg1 instanceof String_) {
78+
79+
$type = $scope->getType($arg1);
80+
if ($type instanceof ConstantStringType) {
81+
$entityTypeId = $type->getValue();
82+
} else {
7883
// @todo determine what these types are, and try to resolve entity name from.
7984
return $returnType;
8085
}
8186

82-
$entityTypeId = $arg1->value;
83-
8487
$storageType = $this->entityDataRepository->get($entityTypeId)->getStorageType();
8588
if ($storageType !== null) {
8689
return $storageType;

tests/src/Type/data/entity-type-manager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616
assertType('Drupal\phpstan_fixtures\CustomContentEntityStorage', $etm->getStorage('content_entity_using_custom_storage'));
1717
assertType('Drupal\Core\Config\Entity\ConfigEntityStorage', $etm->getStorage('config_entity_using_default_storage'));
1818
assertType('Drupal\phpstan_fixtures\CustomConfigEntityStorage', $etm->getStorage('config_entity_using_custom_storage'));
19+
const ENTITY_TYPE_ID_NODE = 'node';
20+
assertType('Drupal\node\NodeStorage', $etm->getStorage(ENTITY_TYPE_ID_NODE));

0 commit comments

Comments
 (0)