Skip to content

Commit dc90b96

Browse files
committed
EntityDataRepository for entity type data
1 parent b0709df commit dc90b96

File tree

5 files changed

+84
-45
lines changed

5 files changed

+84
-45
lines changed

extension.neon

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,28 @@ parameters:
1313
- engine
1414
drupal:
1515
drupal_root: '%currentWorkingDirectory%'
16-
entityTypeStorageMapping:
17-
node: Drupal\node\NodeStorage
18-
taxonomy_term: Drupal\taxonomy\TermStorage
19-
user: Drupal\user\UserStorage
20-
block: Drupal\Core\Config\Entity\ConfigEntityStorage
21-
entityStorageMapping:
22-
node: Drupal\node\Entity\Node
23-
taxonomy_term: Drupal\taxonomy\Entity\Term
24-
user: Drupal\user\Entity\User
25-
block: Drupal\block\Entity\Block
16+
entityMapping:
17+
node:
18+
class: Drupal\node\Entity\Node
19+
storage: Drupal\node\NodeStorage
20+
taxonomy_term:
21+
class: Drupal\taxonomy\Entity\Term
22+
storage: Drupal\taxonomy\TermStorage
23+
user:
24+
class: Drupal\user\Entity\User
25+
storage: Drupal\user\UserStorage
26+
block:
27+
class: Drupal\block\Entity\Block
28+
storage: Drupal\Core\Config\Entity\ConfigEntityStorage
2629
parametersSchema:
2730
drupal: structure([
2831
drupal_root: string()
29-
entityTypeStorageMapping: arrayOf(string())
30-
entityStorageMapping: arrayOf(string())
32+
entityMapping: listOf(
33+
structure([
34+
class: string()
35+
storage: string()
36+
])
37+
)
3138
])
3239
rules:
3340
- mglaman\PHPStanDrupal\Rules\Classes\PluginManagerInspectionRule
@@ -38,17 +45,16 @@ rules:
3845
services:
3946
-
4047
class: mglaman\PHPStanDrupal\Drupal\ServiceMap
48+
-
49+
class: mglaman\PHPStanDrupal\Drupal\EntityDataRepository
50+
arguments:
51+
entityData: %drupal.entityMapping%
4152

4253
-
4354
class: mglaman\PHPStanDrupal\Type\EntityTypeManagerGetStorageDynamicReturnTypeExtension
44-
arguments:
45-
reflectionProvider: @reflectionProvider
46-
entityTypeStorageMapping: %drupal.entityTypeStorageMapping%
4755
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
4856
-
4957
class: mglaman\PHPStanDrupal\Type\EntityStorage\EntityStorageDynamicReturnTypeExtension
50-
arguments:
51-
entityStorageMapping: %drupal.entityStorageMapping%
5258
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
5359
-
5460
class: mglaman\PHPStanDrupal\Type\ContainerDynamicReturnTypeExtension

src/Drupal/EntityDataRepository.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace mglaman\PHPStanDrupal\Drupal;
4+
5+
final class EntityDataRepository
6+
{
7+
8+
/**
9+
* @var array<string, array<string, string>>
10+
*/
11+
private $entityData = [];
12+
13+
public function __construct(array $entityData)
14+
{
15+
$this->entityData = $entityData;
16+
}
17+
18+
public function get(string $entityTypeId): ?array
19+
{
20+
return $this->entityData[$entityTypeId] ?? null;
21+
}
22+
23+
public function getClassName(string $entityTypeId): ?string
24+
{
25+
$data = $this->get($entityTypeId);
26+
return $data['class'] ?? null;
27+
}
28+
29+
public function getStorageClassName(string $entityTypeId): ?string
30+
{
31+
$data = $this->get($entityTypeId);
32+
return $data['storage'] ?? null;
33+
}
34+
}

src/Type/EntityStorage/EntityStorageDynamicReturnTypeExtension.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace mglaman\PHPStanDrupal\Type\EntityStorage;
44

55
use Drupal\Core\Entity\EntityStorageInterface;
6+
use mglaman\PHPStanDrupal\Drupal\EntityDataRepository;
67
use PhpParser\Node\Expr\MethodCall;
78
use PHPStan\Analyser\Scope;
89
use PHPStan\Reflection\MethodReflection;
@@ -18,19 +19,13 @@ class EntityStorageDynamicReturnTypeExtension implements DynamicMethodReturnType
1819
{
1920

2021
/**
21-
* @var string[]
22+
* @var EntityDataRepository
2223
*/
23-
private $entityStorageMapping;
24+
private $entityDataRepository;
2425

25-
/**
26-
* EntityStorageDynamicReturnTypeExtension constructor.
27-
*
28-
* @param string[] $entityStorageMapping
29-
*/
30-
public function __construct(
31-
array $entityStorageMapping = []
32-
) {
33-
$this->entityStorageMapping = $entityStorageMapping;
26+
public function __construct(EntityDataRepository $entityDataRepository)
27+
{
28+
$this->entityDataRepository = $entityDataRepository;
3429
}
3530

3631
public function getClass(): string
@@ -64,7 +59,7 @@ public function getTypeFromMethodCall(
6459
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
6560
}
6661

67-
$entityClassName = $this->entityStorageMapping[$callerType->getEntityTypeId()] ?? null;
62+
$entityClassName = $this->entityDataRepository->getClassName($callerType->getEntityTypeId());
6863
if (null === $entityClassName) {
6964
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
7065
}

src/Type/EntityTypeManagerGetStorageDynamicReturnTypeExtension.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
66
use Drupal\Core\Entity\ContentEntityStorageInterface;
7+
use mglaman\PHPStanDrupal\Drupal\EntityDataRepository;
78
use mglaman\PHPStanDrupal\Type\EntityStorage\ConfigEntityStorageType;
89
use mglaman\PHPStanDrupal\Type\EntityStorage\ContentEntityStorageType;
910
use mglaman\PHPStanDrupal\Type\EntityStorage\EntityStorageType;
@@ -27,20 +28,20 @@ class EntityTypeManagerGetStorageDynamicReturnTypeExtension implements DynamicMe
2728
private $reflectionProvider;
2829

2930
/**
30-
* @var string[]
31+
* @var EntityDataRepository
3132
*/
32-
private $entityTypeStorageMapping;
33+
private $entityDataRepository;
3334

3435
/**
3536
* EntityTypeManagerGetStorageDynamicReturnTypeExtension constructor.
3637
*
3738
* @param ReflectionProvider $reflectionProvider
38-
* @param string[] $entityTypeStorageMapping
39+
* @param EntityDataRepository $entityDataRepository
3940
*/
40-
public function __construct(ReflectionProvider $reflectionProvider, array $entityTypeStorageMapping = [])
41+
public function __construct(ReflectionProvider $reflectionProvider, EntityDataRepository $entityDataRepository)
4142
{
4243
$this->reflectionProvider = $reflectionProvider;
43-
$this->entityTypeStorageMapping = $entityTypeStorageMapping;
44+
$this->entityDataRepository = $entityDataRepository;
4445
}
4546

4647
public function getClass(): string
@@ -86,8 +87,8 @@ public function getTypeFromMethodCall(
8687

8788
$entityTypeId = $arg1->value;
8889

89-
if (isset($this->entityTypeStorageMapping[$entityTypeId])) {
90-
$storageClassName = $this->entityTypeStorageMapping[$entityTypeId];
90+
$storageClassName = $this->entityDataRepository->getStorageClassName($entityTypeId);
91+
if ($storageClassName !== null) {
9192
$interfaces = \array_keys($this->reflectionProvider->getClass($storageClassName)->getInterfaces());
9293

9394
if (\in_array(ConfigEntityStorageInterface::class, $interfaces, true)) {
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
parameters:
22
drupal:
33
drupal_root: tests/fixtures/drupal
4-
entityTypeStorageMapping:
5-
content_entity_using_custom_storage: Drupal\phpstan_fixtures\CustomContentEntityStorage
6-
config_entity_using_default_storage: Drupal\Core\Config\Entity\ConfigEntityStorage
7-
config_entity_using_custom_storage: Drupal\phpstan_fixtures\CustomConfigEntityStorage
8-
entityStorageMapping:
9-
content_entity_using_default_storage: Drupal\phpstan_fixtures\Entity\ContentEntityUsingDefaultStorage
10-
content_entity_using_custom_storage: Drupal\phpstan_fixtures\Entity\ContentEntityUsingCustomStorage
11-
config_entity_using_default_storage: Drupal\phpstan_fixtures\Entity\ConfigEntityUsingDefaultStorage
12-
config_entity_using_custom_storage: Drupal\phpstan_fixtures\Entity\ConfigEntityUsingCustomStorage
4+
entityMapping:
5+
content_entity_using_custom_storage:
6+
class: Drupal\phpstan_fixtures\Entity\ContentEntityUsingCustomStorage
7+
storage: Drupal\phpstan_fixtures\CustomContentEntityStorage
8+
config_entity_using_default_storage:
9+
class: Drupal\phpstan_fixtures\Entity\ConfigEntityUsingDefaultStorage
10+
storage: Drupal\Core\Config\Entity\ConfigEntityStorage
11+
config_entity_using_custom_storage:
12+
class: Drupal\phpstan_fixtures\Entity\ConfigEntityUsingCustomStorage
13+
storage: Drupal\phpstan_fixtures\CustomConfigEntityStorage
14+
content_entity_using_default_storage:
15+
class: Drupal\phpstan_fixtures\Entity\ContentEntityUsingDefaultStorage
1316
includes:
1417
- ../../../extension.neon
1518
- ../../../vendor/phpstan/phpstan-deprecation-rules/rules.neon

0 commit comments

Comments
 (0)