|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonk\DoctrineEntityPreloader\PHPStan; |
| 4 | + |
| 5 | +use Doctrine\ORM\Mapping\ManyToMany; |
| 6 | +use Doctrine\ORM\Mapping\ManyToOne; |
| 7 | +use Doctrine\ORM\Mapping\OneToMany; |
| 8 | +use Doctrine\ORM\Mapping\OneToOne; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PHPStan\Analyser\Scope; |
| 11 | +use PHPStan\Type\Accessory\AccessoryArrayListType; |
| 12 | +use PHPStan\Type\ArrayType; |
| 13 | +use PHPStan\Type\IntegerType; |
| 14 | +use PHPStan\Type\IntersectionType; |
| 15 | +use PHPStan\Type\ObjectType; |
| 16 | +use PHPStan\Type\Type; |
| 17 | +use PHPStan\Type\TypeCombinator; |
| 18 | +use PHPStan\Type\TypeWithClassName; |
| 19 | +use PHPStan\Type\UnionType; |
| 20 | +use ReflectionNamedType; |
| 21 | +use ReflectionProperty; |
| 22 | +use function count; |
| 23 | +use function is_string; |
| 24 | + |
| 25 | +abstract class EntityPreloaderCore |
| 26 | +{ |
| 27 | + |
| 28 | + protected function getPreloadedPropertyName( |
| 29 | + MethodCall $methodCall, |
| 30 | + Scope $scope, |
| 31 | + ): ?string |
| 32 | + { |
| 33 | + $args = $methodCall->getArgs(); |
| 34 | + |
| 35 | + if (!isset($args[1])) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + $type = $scope->getType($args[1]->value); |
| 40 | + $constantValues = $type->getConstantScalarValues(); |
| 41 | + |
| 42 | + if (count($constantValues) !== 1) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + if (!is_string($constantValues[0])) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + return $constantValues[0]; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @throws EntityPreloaderRuleException |
| 55 | + */ |
| 56 | + protected function getPreloadedEntityType( |
| 57 | + MethodCall $methodCall, |
| 58 | + Scope $scope, |
| 59 | + string $propertyName, |
| 60 | + ): Type |
| 61 | + { |
| 62 | + $args = $methodCall->getArgs(); |
| 63 | + |
| 64 | + $sourceEntityListType = $scope->getType($args[0]->value); |
| 65 | + $sourceEntityType = $sourceEntityListType->getIterableValueType(); |
| 66 | + |
| 67 | + return $this->getAssociationTargetType($sourceEntityType, $propertyName); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @throws EntityPreloaderRuleException |
| 72 | + */ |
| 73 | + private function getAssociationTargetType( |
| 74 | + Type $type, |
| 75 | + string $propertyName, |
| 76 | + ): Type |
| 77 | + { |
| 78 | + if ($type instanceof UnionType) { |
| 79 | + return $this->getAssociationTargetTypeFromUnion($type, $propertyName); |
| 80 | + |
| 81 | + } elseif ($type instanceof IntersectionType) { // @phpstan-ignore phpstanApi.instanceofType |
| 82 | + return $this->getAssociationTargetTypeFromIntersection($type, $propertyName); |
| 83 | + |
| 84 | + } elseif ($type instanceof TypeWithClassName) { // @phpstan-ignore phpstanApi.instanceofType |
| 85 | + return $this->getAssociationTargetTypeFromObjectType($type, $propertyName); |
| 86 | + |
| 87 | + } else { |
| 88 | + throw EntityPreloaderRuleException::propertyNotFound('object', $propertyName); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @throws EntityPreloaderRuleException |
| 94 | + */ |
| 95 | + private function getAssociationTargetTypeFromUnion( |
| 96 | + UnionType $type, |
| 97 | + string $propertyName, |
| 98 | + ): Type |
| 99 | + { |
| 100 | + $propertyTypes = []; |
| 101 | + |
| 102 | + foreach ($type->getTypes() as $innerType) { |
| 103 | + $propertyTypes[] = $this->getAssociationTargetType($innerType, $propertyName); |
| 104 | + } |
| 105 | + |
| 106 | + return TypeCombinator::union(...$propertyTypes); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @throws EntityPreloaderRuleException |
| 111 | + */ |
| 112 | + private function getAssociationTargetTypeFromIntersection( |
| 113 | + IntersectionType $type, |
| 114 | + string $propertyName, |
| 115 | + ): Type |
| 116 | + { |
| 117 | + $propertyTypes = []; |
| 118 | + $exceptions = []; |
| 119 | + |
| 120 | + foreach ($type->getTypes() as $innerType) { |
| 121 | + try { |
| 122 | + $propertyTypes[] = $this->getAssociationTargetType($innerType, $propertyName); |
| 123 | + |
| 124 | + } catch (EntityPreloaderRuleException $e) { |
| 125 | + $exceptions[] = $e; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (count($propertyTypes) === 0 && count($exceptions) > 0) { |
| 130 | + throw $exceptions[0]; |
| 131 | + } |
| 132 | + |
| 133 | + return TypeCombinator::intersect(...$propertyTypes); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @throws EntityPreloaderRuleException |
| 138 | + */ |
| 139 | + private function getAssociationTargetTypeFromObjectType( |
| 140 | + TypeWithClassName $type, |
| 141 | + string $propertyName, |
| 142 | + ): Type |
| 143 | + { |
| 144 | + $classReflection = $type->getClassReflection(); |
| 145 | + |
| 146 | + if ($classReflection === null) { |
| 147 | + throw EntityPreloaderRuleException::classNotFound($type->getClassName()); |
| 148 | + } |
| 149 | + |
| 150 | + for ($currentClassReflection = $classReflection; $currentClassReflection !== null; $currentClassReflection = $currentClassReflection->getParentClass()) { |
| 151 | + if ($currentClassReflection->hasInstanceProperty($propertyName)) { |
| 152 | + $propertyReflection = $currentClassReflection->getNativeProperty($propertyName)->getNativeReflection(); |
| 153 | + return $this->getAssociationTargetTypeFromPropertyReflection($propertyReflection); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + throw EntityPreloaderRuleException::propertyNotFound($classReflection->getName(), $propertyName); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @throws EntityPreloaderRuleException |
| 162 | + */ |
| 163 | + private function getAssociationTargetTypeFromPropertyReflection(ReflectionProperty $propertyReflection): Type |
| 164 | + { |
| 165 | + $associationAttributes = [ |
| 166 | + OneToOne::class, |
| 167 | + OneToMany::class, |
| 168 | + ManyToOne::class, |
| 169 | + ManyToMany::class, |
| 170 | + ]; |
| 171 | + |
| 172 | + foreach ($associationAttributes as $attributeClass) { |
| 173 | + foreach ($propertyReflection->getAttributes($attributeClass) as $attributeReflection) { |
| 174 | + $attribute = $attributeReflection->newInstance(); |
| 175 | + |
| 176 | + if ($attribute->targetEntity !== null) { |
| 177 | + return new ObjectType($attribute->targetEntity); |
| 178 | + |
| 179 | + } elseif ($attributeClass === OneToOne::class && $propertyReflection->getType() instanceof ReflectionNamedType) { |
| 180 | + return new ObjectType($propertyReflection->getType()->getName()); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + throw EntityPreloaderRuleException::invalidAssociations($propertyReflection->getDeclaringClass()->getName(), $propertyReflection->getName()); |
| 186 | + } |
| 187 | + |
| 188 | + protected function createListType(Type $type): Type |
| 189 | + { |
| 190 | + return TypeCombinator::intersect( |
| 191 | + new ArrayType(new IntegerType(), $type), |
| 192 | + new AccessoryArrayListType(), |
| 193 | + ); |
| 194 | + } |
| 195 | + |
| 196 | +} |
0 commit comments