Skip to content

Commit ab14e2a

Browse files
committed
use isSuperOf instead of instanceof checks for storage types
1 parent ce4f652 commit ab14e2a

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

src/Type/DrupalStaticEntityQueryDynamicReturnTypeExtension.php

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

33
namespace mglaman\PHPStanDrupal\Type;
44

5+
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
6+
use Drupal\Core\Entity\ContentEntityStorageInterface;
57
use mglaman\PHPStanDrupal\Drupal\EntityDataRepository;
68
use mglaman\PHPStanDrupal\Type\EntityQuery\ConfigEntityQueryType;
79
use mglaman\PHPStanDrupal\Type\EntityQuery\ContentEntityQueryType;
810
use mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryType;
9-
use mglaman\PHPStanDrupal\Type\EntityStorage\ConfigEntityStorageType;
10-
use mglaman\PHPStanDrupal\Type\EntityStorage\ContentEntityStorageType;
1111
use PhpParser\Node\Expr\StaticCall;
1212
use PHPStan\Analyser\Scope;
1313
use PHPStan\Reflection\MethodReflection;
@@ -70,14 +70,14 @@ public function getTypeFromStaticMethodCall(
7070
return $returnType;
7171
}
7272

73-
if ($entityStorageType instanceof ContentEntityStorageType) {
73+
if ((new ObjectType(ContentEntityStorageInterface::class))->isSuperTypeOf($entityStorageType)->yes()) {
7474
return new ContentEntityQueryType(
7575
$returnType->getClassName(),
7676
$returnType->getSubtractedType(),
7777
$returnType->getClassReflection()
7878
);
7979
}
80-
if ($entityStorageType instanceof ConfigEntityStorageType) {
80+
if ((new ObjectType(ConfigEntityStorageInterface::class))->isSuperTypeOf($entityStorageType)->yes()) {
8181
return new ConfigEntityQueryType(
8282
$returnType->getClassName(),
8383
$returnType->getSubtractedType(),

src/Type/EntityStorage/EntityStorageDynamicReturnTypeExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace mglaman\PHPStanDrupal\Type\EntityStorage;
44

5+
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
56
use Drupal\Core\Entity\EntityStorageInterface;
67
use mglaman\PHPStanDrupal\Drupal\EntityDataRepository;
78
use PhpParser\Node\Expr\MethodCall;
@@ -76,7 +77,7 @@ public function getTypeFromMethodCall(
7677
}
7778

7879
if (\in_array($methodReflection->getName(), ['loadMultiple', 'loadByProperties'], true)) {
79-
if ($callerType instanceof ConfigEntityStorageType) {
80+
if ((new ObjectType(ConfigEntityStorageInterface::class))->isSuperTypeOf($callerType)->yes()) {
8081
return new ArrayType(new StringType(), $type);
8182
}
8283

tests/src/Type/EntityQuery/EntityQueryDynamicReturnTypeExtensionTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ final class EntityQueryDynamicReturnTypeExtensionTest extends TypeInferenceTestC
1313

1414
public function dataFileAsserts(): iterable
1515
{
16-
yield from $this->gatherAssertTypes(__DIR__ . '/../data/entity-query-execute.php');
17-
yield from $this->gatherAssertTypes(__DIR__ . '/../data/bug-355-entity-query.php');
16+
yield from self::gatherAssertTypes(__DIR__ . '/../data/entity-query-execute.php');
17+
yield from self::gatherAssertTypes(__DIR__ . '/../data/bug-355-entity-query.php');
18+
yield from self::gatherAssertTypes(__DIR__ . '/../data/bug-522.php');
19+
1820
}
1921

2022
/**

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Bug522;
4+
5+
use Drupal\Core\Entity\EntityTypeManagerInterface;
6+
use Drupal\user\RoleInterface;
7+
use function PHPStan\Testing\assertType;
8+
9+
class Foo {
10+
/**
11+
* The role storage used when changing the admin role.
12+
*
13+
* @var \Drupal\user\RoleStorageInterface
14+
*/
15+
protected $roleStorage;
16+
17+
public function __construct(EntityTypeManagerInterface $entityTypeManager)
18+
{
19+
$this->roleStorage = $entityTypeManager->getStorage('user_role');
20+
}
21+
22+
public function foo()
23+
{
24+
$roles = $this->roleStorage->loadMultiple();
25+
assertType('array<string, Drupal\user\Entity\Role>', $roles);
26+
unset($roles[RoleInterface::ANONYMOUS_ID]);
27+
}
28+
}

0 commit comments

Comments
 (0)