|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPStan\Reflection; |
| 4 | + |
| 5 | +use PHPStan\Type\NullType; |
| 6 | +use PHPStan\Type\ObjectType; |
| 7 | +use PHPStan\Type\StringType; |
| 8 | +use PHPStan\Type\Type; |
| 9 | + |
| 10 | +/** |
| 11 | + * Allows field access via magic methods |
| 12 | + * |
| 13 | + * See \Drupal\Core\Field\FieldItemListInterface::__get and ::__set. |
| 14 | + */ |
| 15 | +class FieldItemListPropertyReflection implements PropertyReflection |
| 16 | +{ |
| 17 | + |
| 18 | + /** @var ClassReflection */ |
| 19 | + private $declaringClass; |
| 20 | + |
| 21 | + /** @var string */ |
| 22 | + private $propertyName; |
| 23 | + |
| 24 | + public function __construct(ClassReflection $declaringClass, string $propertyName) |
| 25 | + { |
| 26 | + $this->declaringClass = $declaringClass; |
| 27 | + $this->propertyName = $propertyName; |
| 28 | + } |
| 29 | + |
| 30 | + public static function canHandleProperty(ClassReflection $classReflection, string $propertyName): bool |
| 31 | + { |
| 32 | + // @todo use the class reflection and be more specific about handled properties. |
| 33 | + // Currently \PHPStan\Reflection\EntityFieldReflection::getType always passes FieldItemListInterface. |
| 34 | + $names = ['entity', 'value', 'target_id']; |
| 35 | + return in_array($propertyName, $names, true); |
| 36 | + } |
| 37 | + |
| 38 | + public function getType(): Type |
| 39 | + { |
| 40 | + if ($this->propertyName === 'entity') { |
| 41 | + return new ObjectType('Drupal\Core\Entity\EntityInterface'); |
| 42 | + } |
| 43 | + if ($this->propertyName === 'target_id') { |
| 44 | + return new StringType(); |
| 45 | + } |
| 46 | + if ($this->propertyName === 'value') { |
| 47 | + return new StringType(); |
| 48 | + } |
| 49 | + |
| 50 | + // Fallback. |
| 51 | + return new NullType(); |
| 52 | + } |
| 53 | + |
| 54 | + public function getDeclaringClass(): ClassReflection |
| 55 | + { |
| 56 | + return $this->declaringClass; |
| 57 | + } |
| 58 | + |
| 59 | + public function isStatic(): bool |
| 60 | + { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + public function isPrivate(): bool |
| 65 | + { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + public function isPublic(): bool |
| 70 | + { |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + public function isReadable(): bool |
| 75 | + { |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + public function isWritable(): bool |
| 80 | + { |
| 81 | + return true; |
| 82 | + } |
| 83 | +} |
0 commit comments