Skip to content

Commit 3d9394f

Browse files
committed
Return object types from entity data repository
1 parent dc90b96 commit 3d9394f

File tree

4 files changed

+49
-33
lines changed

4 files changed

+49
-33
lines changed

extension.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ parameters:
2929
parametersSchema:
3030
drupal: structure([
3131
drupal_root: string()
32-
entityMapping: listOf(
32+
entityMapping: arrayOf(
3333
structure([
3434
class: string()
3535
storage: string()
@@ -48,6 +48,7 @@ services:
4848
-
4949
class: mglaman\PHPStanDrupal\Drupal\EntityDataRepository
5050
arguments:
51+
reflectionProvider: @reflectionProvider
5152
entityData: %drupal.entityMapping%
5253

5354
-

src/Drupal/EntityDataRepository.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@
22

33
namespace mglaman\PHPStanDrupal\Drupal;
44

5+
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
6+
use Drupal\Core\Entity\ContentEntityStorageInterface;
7+
use mglaman\PHPStanDrupal\Type\EntityStorage\ConfigEntityStorageType;
8+
use mglaman\PHPStanDrupal\Type\EntityStorage\ContentEntityStorageType;
9+
use mglaman\PHPStanDrupal\Type\EntityStorage\EntityStorageType;
10+
use PHPStan\Reflection\ReflectionProvider;
11+
use PHPStan\Type\ObjectType;
12+
513
final class EntityDataRepository
614
{
15+
/**
16+
* @var ReflectionProvider
17+
*/
18+
private $reflectionProvider;
719

820
/**
921
* @var array<string, array<string, string>>
1022
*/
11-
private $entityData = [];
23+
private $entityData;
1224

13-
public function __construct(array $entityData)
25+
public function __construct(ReflectionProvider $reflectionProvider, array $entityData)
1426
{
27+
$this->reflectionProvider = $reflectionProvider;
1528
$this->entityData = $entityData;
1629
}
1730

@@ -20,15 +33,37 @@ public function get(string $entityTypeId): ?array
2033
return $this->entityData[$entityTypeId] ?? null;
2134
}
2235

23-
public function getClassName(string $entityTypeId): ?string
36+
public function getClassType(string $entityTypeId): ?ObjectType
2437
{
2538
$data = $this->get($entityTypeId);
26-
return $data['class'] ?? null;
39+
$className = $data['class'] ?? null;
40+
if ($className === null) {
41+
return null;
42+
}
43+
return new ObjectType($className);
2744
}
2845

29-
public function getStorageClassName(string $entityTypeId): ?string
46+
public function getStorageType(string $entityTypeId): ?ObjectType
3047
{
3148
$data = $this->get($entityTypeId);
32-
return $data['storage'] ?? null;
49+
$className = $data['storage'] ?? null;
50+
if ($className === null) {
51+
// @todo get entity type class reflection and return proper storage for entity type
52+
// example: config storage, sqlcontententitystorage, etc.
53+
// $className = reflectedDecision.
54+
return null;
55+
}
56+
if (!$this->reflectionProvider->hasClass($className)) {
57+
return null;
58+
}
59+
60+
$reflection = $this->reflectionProvider->getClass($className);
61+
if ($reflection->implementsInterface(ConfigEntityStorageInterface::class)) {
62+
return new ConfigEntityStorageType($entityTypeId, $className);
63+
}
64+
if ($reflection->implementsInterface(ContentEntityStorageInterface::class)) {
65+
return new ContentEntityStorageType($entityTypeId, $className);
66+
}
67+
return new EntityStorageType($entityTypeId, $className);
3368
}
3469
}

src/Type/EntityStorage/EntityStorageDynamicReturnTypeExtension.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,10 @@ public function getTypeFromMethodCall(
5959
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
6060
}
6161

62-
$entityClassName = $this->entityDataRepository->getClassName($callerType->getEntityTypeId());
63-
if (null === $entityClassName) {
62+
$type = $this->entityDataRepository->getClassType($callerType->getEntityTypeId());
63+
if ($type === null) {
6464
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
6565
}
66-
67-
$type = new ObjectType($entityClassName);
6866
if (\in_array($methodReflection->getName(), ['load', 'loadUnchanged'], true)) {
6967
return TypeCombinator::addNull($type);
7068
}

src/Type/EntityTypeManagerGetStorageDynamicReturnTypeExtension.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222

2323
class EntityTypeManagerGetStorageDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
2424
{
25-
/**
26-
* @var ReflectionProvider
27-
*/
28-
private $reflectionProvider;
2925

3026
/**
3127
* @var EntityDataRepository
@@ -35,12 +31,10 @@ class EntityTypeManagerGetStorageDynamicReturnTypeExtension implements DynamicMe
3531
/**
3632
* EntityTypeManagerGetStorageDynamicReturnTypeExtension constructor.
3733
*
38-
* @param ReflectionProvider $reflectionProvider
3934
* @param EntityDataRepository $entityDataRepository
4035
*/
41-
public function __construct(ReflectionProvider $reflectionProvider, EntityDataRepository $entityDataRepository)
36+
public function __construct(EntityDataRepository $entityDataRepository)
4237
{
43-
$this->reflectionProvider = $reflectionProvider;
4438
$this->entityDataRepository = $entityDataRepository;
4539
}
4640

@@ -87,23 +81,11 @@ public function getTypeFromMethodCall(
8781

8882
$entityTypeId = $arg1->value;
8983

90-
$storageClassName = $this->entityDataRepository->getStorageClassName($entityTypeId);
91-
if ($storageClassName !== null) {
92-
$interfaces = \array_keys($this->reflectionProvider->getClass($storageClassName)->getInterfaces());
93-
94-
if (\in_array(ConfigEntityStorageInterface::class, $interfaces, true)) {
95-
return new ConfigEntityStorageType($entityTypeId, $storageClassName);
96-
}
97-
98-
if (\in_array(ContentEntityStorageInterface::class, $interfaces, true)) {
99-
return new ContentEntityStorageType($entityTypeId, $storageClassName);
100-
}
101-
102-
return new EntityStorageType($entityTypeId, $storageClassName);
84+
$storageType = $this->entityDataRepository->getStorageType($entityTypeId);
85+
if ($storageType !== null) {
86+
return $storageType;
10387
}
10488

105-
// @todo get entity type class reflection and return proper storage for entity type
106-
// example: config storage, sqlcontententitystorage, etc.
10789
if ($returnType instanceof ObjectType) {
10890
return new EntityStorageType($entityTypeId, $returnType->getClassName());
10991
}

0 commit comments

Comments
 (0)