|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Doctrine\ORM; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Reflection\MissingPropertyFromReflectionException; |
| 8 | +use PHPStan\Rules\Rule; |
| 9 | +use PHPStan\Type\Doctrine\DescriptorRegistry; |
| 10 | +use PHPStan\Type\Doctrine\ObjectMetadataResolver; |
| 11 | +use PHPStan\Type\ObjectType; |
| 12 | +use PHPStan\Type\TypeCombinator; |
| 13 | +use PHPStan\Type\VerbosityLevel; |
| 14 | +use function sprintf; |
| 15 | + |
| 16 | +/** |
| 17 | + * @implements Rule<Node\Stmt\PropertyProperty> |
| 18 | + */ |
| 19 | +class EntityEmbeddableRule implements Rule |
| 20 | +{ |
| 21 | + |
| 22 | + /** @var \PHPStan\Type\Doctrine\ObjectMetadataResolver */ |
| 23 | + private $objectMetadataResolver; |
| 24 | + |
| 25 | + /** @var \PHPStan\Type\Doctrine\DescriptorRegistry */ |
| 26 | + private $descriptorRegistry; |
| 27 | + |
| 28 | + /** @var bool */ |
| 29 | + private $reportUnknownTypes; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + ObjectMetadataResolver $objectMetadataResolver, |
| 33 | + DescriptorRegistry $descriptorRegistry, |
| 34 | + bool $reportUnknownTypes |
| 35 | + ) |
| 36 | + { |
| 37 | + $this->objectMetadataResolver = $objectMetadataResolver; |
| 38 | + $this->descriptorRegistry = $descriptorRegistry; |
| 39 | + $this->reportUnknownTypes = $reportUnknownTypes; |
| 40 | + } |
| 41 | + |
| 42 | + public function getNodeType(): string |
| 43 | + { |
| 44 | + return Node\Stmt\PropertyProperty::class; |
| 45 | + } |
| 46 | + |
| 47 | + public function processNode(Node $node, Scope $scope): array |
| 48 | + { |
| 49 | + $class = $scope->getClassReflection(); |
| 50 | + if ($class === null) { |
| 51 | + return []; |
| 52 | + } |
| 53 | + |
| 54 | + $objectManager = $this->objectMetadataResolver->getObjectManager(); |
| 55 | + if ($objectManager === null) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + $className = $class->getName(); |
| 60 | + |
| 61 | + try { |
| 62 | + $metadata = $objectManager->getClassMetadata($className); |
| 63 | + } catch (\Doctrine\ORM\Mapping\MappingException $e) { |
| 64 | + return []; |
| 65 | + } |
| 66 | + |
| 67 | + $classMetadataInfo = 'Doctrine\ORM\Mapping\ClassMetadataInfo'; |
| 68 | + if (!$metadata instanceof $classMetadataInfo) { |
| 69 | + return []; |
| 70 | + } |
| 71 | + |
| 72 | + $propertyName = (string) $node->name; |
| 73 | + try { |
| 74 | + $property = $class->getNativeProperty($propertyName); |
| 75 | + } catch (MissingPropertyFromReflectionException $e) { |
| 76 | + return []; |
| 77 | + } |
| 78 | + |
| 79 | + if (!isset($metadata->embeddedClasses[$propertyName])) { |
| 80 | + return []; |
| 81 | + } |
| 82 | + |
| 83 | + $errors = []; |
| 84 | + $embeddedClass = $metadata->embeddedClasses[$propertyName]; |
| 85 | + $propertyWritableType = $property->getWritableType(); |
| 86 | + $accordingToMapping = new ObjectType($embeddedClass['class']); |
| 87 | + if (!TypeCombinator::removeNull($propertyWritableType)->equals($accordingToMapping)) { |
| 88 | + $errors[] = sprintf( |
| 89 | + 'Property %s::$%s type mapping mismatch: mapping specifies %s but property expects %s.', |
| 90 | + $class->getName(), |
| 91 | + $propertyName, |
| 92 | + $accordingToMapping->describe(VerbosityLevel::typeOnly()), |
| 93 | + $propertyWritableType->describe(VerbosityLevel::typeOnly()) |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + return $errors; |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments