|
| 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\Native\NativeParameterReflection; |
| 10 | +use PHPStan\Reflection\ParameterReflection; |
| 11 | +use PHPStan\Reflection\PassedByReference; |
| 12 | +use PHPStan\Type\ClosureType; |
| 13 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 14 | +use PHPStan\Type\Constant\ConstantStringType; |
| 15 | +use PHPStan\Type\DynamicMethodParameterTypeExtension; |
| 16 | +use PHPStan\Type\MixedType; |
| 17 | +use PHPStan\Type\NeverType; |
| 18 | +use PHPStan\Type\ObjectType; |
| 19 | +use PHPStan\Type\StringType; |
| 20 | +use PHPStan\Type\Type; |
| 21 | +use PHPStan\Type\TypeCombinator; |
| 22 | +use PhpParser\Node\Expr\MethodCall; |
| 23 | +use PhpParser\Node\Expr\StaticCall; |
| 24 | +use PhpParser\Node\Name; |
| 25 | +use PhpParser\Node\VariadicPlaceholder; |
| 26 | + |
| 27 | +final class ParameterTypeExtension implements DynamicMethodParameterTypeExtension |
| 28 | +{ |
| 29 | + public function isMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool |
| 30 | + { |
| 31 | + if (! $methodReflection->getDeclaringClass()->is(Builder::class)) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + return $methodReflection->getName() === 'with'; |
| 36 | + } |
| 37 | + |
| 38 | + public function getTypeFromMethodCall( |
| 39 | + MethodReflection $methodReflection, |
| 40 | + MethodCall $methodCall, |
| 41 | + ParameterReflection $parameter, |
| 42 | + Scope $scope, |
| 43 | + ): Type|null { |
| 44 | + $arg = $methodCall->getArgs()[0] ?? null; |
| 45 | + if (!$arg) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + $type = $scope->getType($arg->value)->getConstantArrays()[0] ?? null; |
| 50 | + if (!$type) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + $model = $scope->getType($methodCall->var) |
| 55 | + ->getTemplateType(Builder::class, 'TModel') |
| 56 | + ->getObjectClassNames()[0] ?? null; |
| 57 | + if (!$model) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + foreach ($type->getKeyTypes() as $keyType) { |
| 62 | + $relationType = $this->getRelationTypeFromModel($model, (string) $keyType->getValue(), $scope); |
| 63 | + if (!$relationType) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + $newType = new ClosureType([ |
| 68 | + /** @phpstan-ignore phpstanApi.constructor */ |
| 69 | + new NativeParameterReflection('test', false, $relationType, PassedByReference::createNo(), false, null), |
| 70 | + ], new MixedType(), false); |
| 71 | + |
| 72 | + $type = $type->setOffsetValueType($keyType, $newType, false); |
| 73 | + } |
| 74 | + |
| 75 | + return $type; |
| 76 | + } |
| 77 | + |
| 78 | + public function getRelationTypeFromModel(string $model, string $relation, Scope $scope): ?Type |
| 79 | + { |
| 80 | + $modelType = new ObjectType($model); |
| 81 | + |
| 82 | + if (! $modelType->hasMethod($relation)->yes()) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + $relationType = $modelType->getMethod($relation, $scope)->getVariants()[0]->getReturnType(); |
| 87 | + |
| 88 | + if (! (new ObjectType(Relation::class))->isSuperTypeOf($relationType)->yes()) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + return $relationType; |
| 93 | + } |
| 94 | +} |
0 commit comments