|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App; |
| 6 | + |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Reflection\MethodReflection; |
| 9 | +use PHPStan\Reflection\ParameterReflection; |
| 10 | +use PHPStan\Reflection\PassedByReference; |
| 11 | +use PHPStan\Type\ClosureType; |
| 12 | +use PHPStan\Type\DynamicMethodParameterTypeExtension; |
| 13 | +use PHPStan\Type\MixedType; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | +use PHPStan\Type\Type; |
| 16 | +use PhpParser\Node\Expr\MethodCall; |
| 17 | + |
| 18 | +final class ParameterTypeExtension implements DynamicMethodParameterTypeExtension |
| 19 | +{ |
| 20 | + public function isMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool |
| 21 | + { |
| 22 | + if (! $methodReflection->getDeclaringClass()->is(Builder::class)) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + return $methodReflection->getName() === 'with'; |
| 27 | + } |
| 28 | + |
| 29 | + public function getTypeFromMethodCall( |
| 30 | + MethodReflection $methodReflection, |
| 31 | + MethodCall $methodCall, |
| 32 | + ParameterReflection $parameter, |
| 33 | + Scope $scope, |
| 34 | + ): Type|null { |
| 35 | + $arg = $methodCall->getArgs()[0] ?? null; |
| 36 | + if (!$arg) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + $type = $scope->getType($arg->value)->getConstantArrays()[0] ?? null; |
| 41 | + if (!$type) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + $model = $scope->getType($methodCall->var) |
| 46 | + ->getTemplateType(Builder::class, 'TModel') |
| 47 | + ->getObjectClassNames()[0] ?? null; |
| 48 | + if (!$model) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + foreach ($type->getKeyTypes() as $keyType) { |
| 53 | + $relationType = $this->getRelationTypeFromModel($model, (string) $keyType->getValue(), $scope); |
| 54 | + if (!$relationType) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + $newType = new ClosureType([ |
| 59 | + new class('test', $relationType) implements ParameterReflection { |
| 60 | + public function __construct(private string $name, private Type $type) {} |
| 61 | + public function getName(): string |
| 62 | + { |
| 63 | + return $this->name; |
| 64 | + } |
| 65 | + public function isOptional(): bool |
| 66 | + { |
| 67 | + return false; |
| 68 | + } |
| 69 | + public function getType(): Type |
| 70 | + { |
| 71 | + return $this->type; |
| 72 | + } |
| 73 | + public function passedByReference(): PassedByReference |
| 74 | + { |
| 75 | + return PassedByReference::createNo(); |
| 76 | + } |
| 77 | + public function isVariadic(): bool |
| 78 | + { |
| 79 | + return false; |
| 80 | + } |
| 81 | + public function getDefaultValue(): ?Type |
| 82 | + { |
| 83 | + return null; |
| 84 | + } |
| 85 | + }, |
| 86 | + ], new MixedType(), false); |
| 87 | + |
| 88 | + $type = $type->setOffsetValueType($keyType, $newType, false); |
| 89 | + } |
| 90 | + |
| 91 | + return $type; |
| 92 | + } |
| 93 | + |
| 94 | + public function getRelationTypeFromModel(string $model, string $relation, Scope $scope): ?Type |
| 95 | + { |
| 96 | + $modelType = new ObjectType($model); |
| 97 | + |
| 98 | + if (! $modelType->hasMethod($relation)->yes()) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + $relationType = $modelType->getMethod($relation, $scope)->getVariants()[0]->getReturnType(); |
| 103 | + |
| 104 | + if (! (new ObjectType(Relation::class))->isSuperTypeOf($relationType)->yes()) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + return $relationType; |
| 109 | + } |
| 110 | +} |
0 commit comments