Skip to content

Commit 337bc36

Browse files
authored
Merge pull request #251 from mglaman/entity-default-storage-lookup
Use entity storage class inferred from entity type
2 parents d6c8cd2 + ad6c530 commit 337bc36

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

extension.neon

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ parameters:
2525
storage: Drupal\user\UserStorage
2626
block:
2727
class: Drupal\block\Entity\Block
28-
storage: Drupal\Core\Config\Entity\ConfigEntityStorage
2928
parametersSchema:
3029
drupal: structure([
3130
drupal_root: string()
32-
entityMapping: arrayOf(
31+
entityMapping: arrayOf(anyOf(
32+
structure([
33+
class: string()
34+
]),
3335
structure([
3436
class: string()
3537
storage: string()
3638
])
37-
)
39+
))
3840
])
3941
rules:
4042
- mglaman\PHPStanDrupal\Rules\Classes\PluginManagerInspectionRule

src/Drupal/EntityData.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace mglaman\PHPStanDrupal\Drupal;
44

5+
use Drupal\Core\Config\Entity\ConfigEntityInterface;
56
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
7+
use Drupal\Core\Entity\ContentEntityInterface;
68
use Drupal\Core\Entity\ContentEntityStorageInterface;
79
use Drupal\Core\Entity\EntityStorageInterface;
810
use mglaman\PHPStanDrupal\Type\EntityStorage\ConfigEntityStorageType;
@@ -43,10 +45,17 @@ public function getClassType(): ?ObjectType
4345
public function getStorageType(): ?ObjectType
4446
{
4547
if ($this->storageClassName === null) {
46-
// @todo get entity type class reflection and return proper storage for entity type
47-
// example: config storage, sqlcontententitystorage, etc.
48-
// $className = reflectedDecision.
49-
return null;
48+
$classType = $this->getClassType();
49+
if ($classType === null) {
50+
return null;
51+
}
52+
if ((new ObjectType(ConfigEntityInterface::class))->isSuperTypeOf($classType)->yes()) {
53+
$this->storageClassName = 'Drupal\Core\Config\Entity\ConfigEntityStorage';
54+
} elseif ((new ObjectType(ContentEntityInterface::class))->isSuperTypeOf($classType)->yes()) {
55+
$this->storageClassName = 'Drupal\Core\Entity\Sql\SqlContentEntityStorage';
56+
} else {
57+
return null;
58+
}
5059
}
5160

5261
$storageType = new ObjectType($this->storageClassName);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
assertType('Drupal\taxonomy\TermStorage', $etm->getStorage('taxonomy_term'));
1313
assertType('Drupal\Core\Entity\EntityStorageInterface', $etm->getStorage('search_api_index'));
1414
assertType('Drupal\Core\Config\Entity\ConfigEntityStorage', $etm->getStorage('block'));
15-
// @todo fix this assert with a lookup on known entity class type.
16-
// assertType('Drupal\Core\Entity\Sql\SqlContentEntityStorage', $etm->getStorage('content_entity_using_default_storage'));
17-
assertType('Drupal\Core\Entity\EntityStorageInterface', $etm->getStorage('content_entity_using_default_storage'));
15+
assertType('Drupal\Core\Entity\Sql\SqlContentEntityStorage', $etm->getStorage('content_entity_using_default_storage'));
1816
assertType('Drupal\phpstan_fixtures\CustomContentEntityStorage', $etm->getStorage('content_entity_using_custom_storage'));
1917
assertType('Drupal\Core\Config\Entity\ConfigEntityStorage', $etm->getStorage('config_entity_using_default_storage'));
2018
assertType('Drupal\phpstan_fixtures\CustomConfigEntityStorage', $etm->getStorage('config_entity_using_custom_storage'));

0 commit comments

Comments
 (0)