|
| 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\ObjectMetadataResolver; |
| 10 | +use PHPStan\Type\IterableType; |
| 11 | +use PHPStan\Type\MixedType; |
| 12 | +use PHPStan\Type\ObjectType; |
| 13 | +use PHPStan\Type\TypeCombinator; |
| 14 | +use PHPStan\Type\VerbosityLevel; |
| 15 | +use function sprintf; |
| 16 | + |
| 17 | +class EntityRelationRule implements Rule |
| 18 | +{ |
| 19 | + |
| 20 | + /** @var \PHPStan\Type\Doctrine\ObjectMetadataResolver */ |
| 21 | + private $objectMetadataResolver; |
| 22 | + |
| 23 | + public function __construct(ObjectMetadataResolver $objectMetadataResolver) |
| 24 | + { |
| 25 | + $this->objectMetadataResolver = $objectMetadataResolver; |
| 26 | + } |
| 27 | + |
| 28 | + public function getNodeType(): string |
| 29 | + { |
| 30 | + return Node\Stmt\PropertyProperty::class; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @param \PhpParser\Node\Stmt\PropertyProperty $node |
| 35 | + * @param \PHPStan\Analyser\Scope $scope |
| 36 | + * @return string[] |
| 37 | + */ |
| 38 | + public function processNode(Node $node, Scope $scope): array |
| 39 | + { |
| 40 | + $class = $scope->getClassReflection(); |
| 41 | + if ($class === null) { |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + $objectManager = $this->objectMetadataResolver->getObjectManager(); |
| 46 | + if ($objectManager === null) { |
| 47 | + return []; |
| 48 | + } |
| 49 | + |
| 50 | + $className = $class->getName(); |
| 51 | + if ($objectManager->getMetadataFactory()->isTransient($className)) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + /** @var \Doctrine\ORM\Mapping\ClassMetadataInfo $metadata */ |
| 56 | + $metadata = $objectManager->getClassMetadata($className); |
| 57 | + $classMetadataInfo = 'Doctrine\ORM\Mapping\ClassMetadataInfo'; |
| 58 | + if (!$metadata instanceof $classMetadataInfo) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + $propertyName = (string) $node->name; |
| 63 | + try { |
| 64 | + $property = $class->getNativeProperty($propertyName); |
| 65 | + } catch (MissingPropertyFromReflectionException $e) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + if (!isset($metadata->associationMappings[$propertyName])) { |
| 70 | + return []; |
| 71 | + } |
| 72 | + $associationMapping = $metadata->associationMappings[$propertyName]; |
| 73 | + |
| 74 | + $columnType = null; |
| 75 | + if ((bool) ($associationMapping['type'] & 3)) { // ClassMetadataInfo::TO_ONE |
| 76 | + $columnType = new ObjectType($associationMapping['targetEntity']); |
| 77 | + if ($associationMapping['joinColumns'][0]['nullable'] ?? true) { |
| 78 | + $columnType = TypeCombinator::addNull($columnType); |
| 79 | + } |
| 80 | + } elseif ((bool) ($associationMapping['type'] & 12)) { // ClassMetadataInfo::TO_MANY |
| 81 | + $columnType = TypeCombinator::intersect( |
| 82 | + new ObjectType('Doctrine\Common\Collections\Collection'), |
| 83 | + new IterableType(new MixedType(), new ObjectType($associationMapping['targetEntity'])) |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + $errors = []; |
| 88 | + if ($columnType !== null) { |
| 89 | + if (!$property->getWritableType()->isSuperTypeOf($columnType)->yes()) { |
| 90 | + $errors[] = sprintf('Database can contain %s but property expects %s.', $columnType->describe(VerbosityLevel::typeOnly()), $property->getWritableType()->describe(VerbosityLevel::typeOnly())); |
| 91 | + } |
| 92 | + if (!$columnType->isSuperTypeOf($property->getReadableType())->yes()) { |
| 93 | + $errors[] = sprintf('Property can contain %s but database expects %s.', $property->getReadableType()->describe(VerbosityLevel::typeOnly()), $columnType->describe(VerbosityLevel::typeOnly())); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return $errors; |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments