-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
RelationFindExtension.php
101 lines (82 loc) · 3.19 KB
/
RelationFindExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
namespace NunoMaduro\Larastan\ReturnTypes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Str;
use NunoMaduro\Larastan\Methods\BuilderHelper;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeWithClassName;
/**
* @internal
*/
final class RelationFindExtension implements DynamicMethodReturnTypeExtension
{
/** @var BuilderHelper */
private $builderHelper;
/** @var ReflectionProvider */
private $reflectionProvider;
public function __construct(ReflectionProvider $reflectionProvider, BuilderHelper $builderHelper)
{
$this->builderHelper = $builderHelper;
$this->reflectionProvider = $reflectionProvider;
}
/**
* {@inheritdoc}
*/
public function getClass(): string
{
return Relation::class;
}
/**
* {@inheritdoc}
*/
public function isMethodSupported(MethodReflection $methodReflection): bool
{
if (! Str::startsWith($methodReflection->getName(), 'find')) {
return false;
}
$modelType = $methodReflection->getDeclaringClass()->getActiveTemplateTypeMap()->getType('TRelatedModel');
if (! $modelType instanceof TypeWithClassName) {
return false;
}
return $methodReflection->getDeclaringClass()->hasNativeMethod($methodReflection->getName()) ||
$this->reflectionProvider->getClass(Builder::class)->hasNativeMethod($methodReflection->getName()) ||
$this->reflectionProvider->getClass(QueryBuilder::class)->hasNativeMethod($methodReflection->getName());
}
/**
* {@inheritdoc}
*/
public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type {
/** @var ObjectType $modelType */
$modelType = $methodReflection->getDeclaringClass()->getActiveTemplateTypeMap()->getType('TRelatedModel');
$argType = $scope->getType($methodCall->getArgs()[0]->value);
$returnType = $methodReflection->getVariants()[0]->getReturnType();
if (in_array(Collection::class, $returnType->getReferencedClasses(), true)) {
if ($argType->isIterable()->yes()) {
$collectionClassName = $this->builderHelper->determineCollectionClassName($modelType->getClassname());
return new GenericObjectType($collectionClassName, [new IntegerType(), $modelType]);
}
$returnType = TypeCombinator::remove($returnType, new ObjectType(Collection::class));
return TypeCombinator::remove($returnType, new ArrayType(new MixedType(), $modelType));
}
return $returnType;
}
}