Skip to content

Commit cd9c8b9

Browse files
authored
ClassReflection deprecation fix (#829)
* ClassReflection deprecation fix * phpcs
1 parent 759d190 commit cd9c8b9

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

src/Reflection/EntityFieldReflection.php

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
namespace mglaman\PHPStanDrupal\Reflection;
44

5+
use Drupal\Core\Config\Entity\ConfigEntityInterface;
6+
use Drupal\Core\Entity\ContentEntityInterface;
7+
use Drupal\Core\Entity\EntityInterface;
8+
use Drupal\Core\Field\FieldItemListInterface;
59
use PHPStan\Reflection\ClassReflection;
610
use PHPStan\Reflection\PropertyReflection;
11+
use PHPStan\Reflection\ReflectionProvider;
712
use PHPStan\TrinaryLogic;
813
use PHPStan\Type\MixedType;
914
use PHPStan\Type\ObjectType;
@@ -23,50 +28,63 @@ class EntityFieldReflection implements PropertyReflection
2328
/** @var string */
2429
private $propertyName;
2530

26-
public function __construct(ClassReflection $declaringClass, string $propertyName)
31+
private ReflectionProvider $reflectionProvider;
32+
33+
public function __construct(ClassReflection $declaringClass, string $propertyName, ReflectionProvider $reflectionProvider)
2734
{
2835
$this->declaringClass = $declaringClass;
2936
$this->propertyName = $propertyName;
37+
$this->reflectionProvider = $reflectionProvider;
3038
}
3139

3240
public function getReadableType(): Type
3341
{
3442
if ($this->propertyName === 'original') {
35-
if ($this->declaringClass->isSubclassOf('Drupal\Core\Entity\ContentEntityInterface')) {
36-
$objectType = 'Drupal\Core\Entity\ContentEntityInterface';
37-
} elseif ($this->declaringClass->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
38-
$objectType = 'Drupal\Core\Config\Entity\ConfigEntityInterface';
43+
if ($this->isContentEntityType()) {
44+
$objectType = ContentEntityInterface::class;
45+
} elseif ($this->isConfigEntityType()) {
46+
$objectType = ConfigEntityInterface::class;
3947
} else {
40-
$objectType = 'Drupal\Core\Entity\EntityInterface';
48+
$objectType = EntityInterface::class;
4149
}
4250
return new ObjectType($objectType);
4351
}
4452

45-
if ($this->declaringClass->isSubclassOf('Drupal\Core\Entity\ContentEntityInterface')) {
53+
if ($this->isContentEntityType()) {
4654
// Assume the property is a field.
47-
return new ObjectType('Drupal\Core\Field\FieldItemListInterface');
55+
return new ObjectType(FieldItemListInterface::class);
4856
}
4957

5058
return new MixedType();
5159
}
5260

61+
private function isContentEntityType(): bool
62+
{
63+
return $this->declaringClass->isSubclassOfClass($this->reflectionProvider->getClass(ContentEntityInterface::class));
64+
}
65+
66+
private function isConfigEntityType(): bool
67+
{
68+
return $this->declaringClass->isSubclassOfClass($this->reflectionProvider->getClass(ConfigEntityInterface::class));
69+
}
70+
5371
public function getWritableType(): Type
5472
{
5573
if ($this->propertyName === 'original') {
56-
if ($this->declaringClass->isSubclassOf('Drupal\Core\Entity\ContentEntityInterface')) {
57-
$objectType = 'Drupal\Core\Entity\ContentEntityInterface';
58-
} elseif ($this->declaringClass->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
59-
$objectType = 'Drupal\Core\Config\Entity\ConfigEntityInterface';
74+
if ($this->isContentEntityType()) {
75+
$objectType = ContentEntityInterface::class;
76+
} elseif ($this->isConfigEntityType()) {
77+
$objectType = ConfigEntityInterface::class;
6078
} else {
61-
$objectType = 'Drupal\Core\Entity\EntityInterface';
79+
$objectType = EntityInterface::class;
6280
}
6381
return new ObjectType($objectType);
6482
}
6583

6684
// @todo Drupal allows $entity->field_myfield = 'string'; does this break that?
67-
if ($this->declaringClass->isSubclassOf('Drupal\Core\Entity\ContentEntityInterface')) {
85+
if ($this->isContentEntityType()) {
6886
// Assume the property is a field.
69-
return new ObjectType('Drupal\Core\Field\FieldItemListInterface');
87+
return new ObjectType(FieldItemListInterface::class);
7088
}
7189

7290
return new MixedType();

src/Reflection/EntityFieldsViaMagicReflectionExtension.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Reflection\ClassReflection;
77
use PHPStan\Reflection\PropertiesClassReflectionExtension;
88
use PHPStan\Reflection\PropertyReflection;
9+
use PHPStan\Reflection\ReflectionProvider;
910
use PHPStan\Type\IsSuperTypeOfResult;
1011
use PHPStan\Type\ObjectType;
1112
use function array_key_exists;
@@ -20,6 +21,13 @@
2021
class EntityFieldsViaMagicReflectionExtension implements PropertiesClassReflectionExtension
2122
{
2223

24+
private ReflectionProvider $reflectionProvider;
25+
26+
public function __construct(ReflectionProvider $reflectionProvider)
27+
{
28+
$this->reflectionProvider = $reflectionProvider;
29+
}
30+
2331
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
2432
{
2533
// @todo Have this run after PHPStan\Reflection\Annotations\AnnotationsPropertiesClassReflectionExtension
@@ -54,7 +62,7 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
5462
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
5563
{
5664
if ($classReflection->implementsInterface('Drupal\Core\Entity\EntityInterface')) {
57-
return new EntityFieldReflection($classReflection, $propertyName);
65+
return new EntityFieldReflection($classReflection, $propertyName, $this->reflectionProvider);
5866
}
5967
if (self::classObjectIsSuperOfInterface($classReflection->getName(), self::getFieldItemListInterfaceObject())->yes()) {
6068
return new FieldItemListPropertyReflection($classReflection, $propertyName);

tests/src/Reflection/EntityFieldsViaMagicReflectionExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class EntityFieldsViaMagicReflectionExtensionTest extends PHPStanTestCase
2626
protected function setUp(): void
2727
{
2828
parent::setUp();
29-
$this->extension = new EntityFieldsViaMagicReflectionExtension();
29+
$this->extension = new EntityFieldsViaMagicReflectionExtension(self::createReflectionProvider());
3030
}
3131

3232
/**

0 commit comments

Comments
 (0)