|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace mglaman\PHPStanDrupal\Type; |
| 6 | + |
| 7 | +use Drupal\Core\Config\Entity\ConfigEntityInterface; |
| 8 | +use Drupal\Core\Entity\EntityRepositoryInterface; |
| 9 | +use mglaman\PHPStanDrupal\Drupal\EntityDataRepository; |
| 10 | +use PhpParser\Node\Expr\MethodCall; |
| 11 | +use PHPStan\Analyser\Scope; |
| 12 | +use PHPStan\Reflection\MethodReflection; |
| 13 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 14 | +use PHPStan\Type\ArrayType; |
| 15 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 16 | +use PHPStan\Type\IntegerType; |
| 17 | +use PHPStan\Type\ObjectType; |
| 18 | +use PHPStan\Type\StringType; |
| 19 | +use PHPStan\Type\Type; |
| 20 | +use PHPStan\Type\TypeCombinator; |
| 21 | + |
| 22 | +final class EntityRepositoryReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 23 | +{ |
| 24 | + |
| 25 | + /** |
| 26 | + * @var EntityDataRepository |
| 27 | + */ |
| 28 | + private $entityDataRepository; |
| 29 | + |
| 30 | + public function __construct(EntityDataRepository $entityDataRepository) |
| 31 | + { |
| 32 | + $this->entityDataRepository = $entityDataRepository; |
| 33 | + } |
| 34 | + |
| 35 | + public function getClass(): string |
| 36 | + { |
| 37 | + return EntityRepositoryInterface::class; |
| 38 | + } |
| 39 | + |
| 40 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 41 | + { |
| 42 | + return in_array( |
| 43 | + $methodReflection->getName(), |
| 44 | + [ |
| 45 | + 'getTranslationFromContext', |
| 46 | + 'loadEntityByUuid', |
| 47 | + 'loadEntityByConfigTarget', |
| 48 | + 'getActive', |
| 49 | + 'getActiveMultiple', |
| 50 | + 'getCanonical', |
| 51 | + 'getCanonicalMultiple', |
| 52 | + ], |
| 53 | + true |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + public function getTypeFromMethodCall( |
| 58 | + MethodReflection $methodReflection, |
| 59 | + MethodCall $methodCall, |
| 60 | + Scope $scope |
| 61 | + ): ?Type { |
| 62 | + $methodName = $methodReflection->getName(); |
| 63 | + $methodArgs = $methodCall->getArgs(); |
| 64 | + $returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 65 | + |
| 66 | + if (count($methodArgs) === 0) { |
| 67 | + return $returnType; |
| 68 | + } |
| 69 | + |
| 70 | + if ($methodName === 'getTranslationFromContext') { |
| 71 | + return $scope->getType($methodArgs[0]->value); |
| 72 | + } |
| 73 | + |
| 74 | + $entityObjectTypes = []; |
| 75 | + $entityIdArg = $scope->getType($methodArgs[0]->value); |
| 76 | + foreach ($entityIdArg->getConstantStrings() as $constantStringType) { |
| 77 | + $entityObjectTypes[] = $this->entityDataRepository->get($constantStringType->getValue())->getClassType() ?? $returnType; |
| 78 | + } |
| 79 | + $entityTypes = TypeCombinator::union(...$entityObjectTypes); |
| 80 | + |
| 81 | + if ($returnType->isArray()->no()) { |
| 82 | + if ($returnType->isNull()->maybe()) { |
| 83 | + $entityTypes = TypeCombinator::addNull($entityTypes); |
| 84 | + } |
| 85 | + return $entityTypes; |
| 86 | + } |
| 87 | + |
| 88 | + if ((new ObjectType(ConfigEntityInterface::class))->isSuperTypeOf($entityTypes)->yes()) { |
| 89 | + $keyType = new StringType(); |
| 90 | + } else { |
| 91 | + $keyType = new IntegerType(); |
| 92 | + } |
| 93 | + |
| 94 | + return new ArrayType($keyType, $entityTypes); |
| 95 | + } |
| 96 | +} |
0 commit comments