|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Gedmo; |
| 4 | + |
| 5 | +use Doctrine\Common\Annotations\AnnotationReader; |
| 6 | +use Gedmo\Mapping\Annotation as Gedmo; |
| 7 | +use PHPStan\Reflection\PropertyReflection; |
| 8 | +use PHPStan\Rules\Properties\ReadWritePropertiesExtension; |
| 9 | +use function get_class; |
| 10 | +use function in_array; |
| 11 | + |
| 12 | +class PropertiesExtension implements ReadWritePropertiesExtension |
| 13 | +{ |
| 14 | + |
| 15 | + private const GEDMO_WRITE_CLASSLIST = [ |
| 16 | + Gedmo\Blameable::class, |
| 17 | + Gedmo\IpTraceable::class, |
| 18 | + Gedmo\Locale::class, |
| 19 | + Gedmo\Language::class, |
| 20 | + Gedmo\Slug::class, |
| 21 | + Gedmo\SortablePosition::class, |
| 22 | + Gedmo\Timestampable::class, |
| 23 | + Gedmo\TreeLeft::class, |
| 24 | + Gedmo\TreeLevel::class, |
| 25 | + Gedmo\TreeParent::class, |
| 26 | + Gedmo\TreePath::class, |
| 27 | + Gedmo\TreePathHash::class, |
| 28 | + Gedmo\TreeRight::class, |
| 29 | + Gedmo\TreeRoot::class, |
| 30 | + Gedmo\UploadableFileMimeType::class, |
| 31 | + Gedmo\UploadableFileName::class, |
| 32 | + Gedmo\UploadableFilePath::class, |
| 33 | + Gedmo\UploadableFileSize::class, |
| 34 | + ]; |
| 35 | + |
| 36 | + private const GEDMO_READ_CLASSLIST = [ |
| 37 | + Gedmo\Locale::class, |
| 38 | + Gedmo\Language::class, |
| 39 | + ]; |
| 40 | + |
| 41 | + /** @var AnnotationReader */ |
| 42 | + private $annotationReader; |
| 43 | + |
| 44 | + public function __construct() |
| 45 | + { |
| 46 | + $this->annotationReader = new AnnotationReader(); |
| 47 | + } |
| 48 | + |
| 49 | + public function isAlwaysRead(PropertyReflection $property, string $propertyName): bool |
| 50 | + { |
| 51 | + return $this->isGedmoAnnotationOrAttribute($property, $propertyName, self::GEDMO_READ_CLASSLIST); |
| 52 | + } |
| 53 | + |
| 54 | + public function isAlwaysWritten(PropertyReflection $property, string $propertyName): bool |
| 55 | + { |
| 56 | + return $this->isGedmoAnnotationOrAttribute($property, $propertyName, self::GEDMO_WRITE_CLASSLIST); |
| 57 | + } |
| 58 | + |
| 59 | + public function isInitialized(PropertyReflection $property, string $propertyName): bool |
| 60 | + { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param array<class-string> $classList |
| 66 | + */ |
| 67 | + private function isGedmoAnnotationOrAttribute(PropertyReflection $property, string $propertyName, array $classList): bool |
| 68 | + { |
| 69 | + $propertyReflection = $property->getDeclaringClass()->getNativeReflection()->getProperty($propertyName); |
| 70 | + |
| 71 | + $annotations = $this->annotationReader->getPropertyAnnotations($propertyReflection); |
| 72 | + foreach ($annotations as $annotation) { |
| 73 | + if (in_array(get_class($annotation), $classList, true)) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + $attributes = $propertyReflection->getAttributes(); |
| 79 | + foreach ($attributes as $attribute) { |
| 80 | + if (in_array($attribute->getName(), $classList, true)) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments