-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
BuilderModelFindExtension.php
114 lines (94 loc) · 3.38 KB
/
BuilderModelFindExtension.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
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace NunoMaduro\Larastan\ReturnTypes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Str;
use NunoMaduro\Larastan\Methods\BuilderHelper;
use NunoMaduro\Larastan\Methods\ModelTypeHelper;
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 BuilderModelFindExtension 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 Builder::class;
}
/**
* {@inheritdoc}
*/
public function isMethodSupported(MethodReflection $methodReflection): bool
{
$methodName = $methodReflection->getName();
if (! Str::startsWith($methodName, 'find')) {
return false;
}
$model = $methodReflection->getDeclaringClass()->getActiveTemplateTypeMap()->getType('TModelClass');
if (! $model instanceof TypeWithClassName) {
return false;
}
if (! $this->reflectionProvider->getClass(Builder::class)->hasNativeMethod($methodName) &&
! $this->reflectionProvider->getClass(QueryBuilder::class)->hasNativeMethod($methodName)) {
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type {
/** @var ObjectType $model */
$model = $methodReflection->getDeclaringClass()->getActiveTemplateTypeMap()->getType('TModelClass');
$returnType = $methodReflection->getVariants()[0]->getReturnType();
$argType = $scope->getType($methodCall->getArgs()[0]->value);
$returnType = ModelTypeHelper::replaceStaticTypeWithModel($returnType, $model->getClassName());
if ($argType->isIterable()->yes()) {
if (in_array(Collection::class, $returnType->getReferencedClasses(), true)) {
$collectionClassName = $this->builderHelper->determineCollectionClassName($model->getClassName());
return new GenericObjectType($collectionClassName, [new IntegerType(), $model]);
}
return TypeCombinator::remove($returnType, $model);
}
if ($argType instanceof MixedType) {
return $returnType;
}
return TypeCombinator::remove(
TypeCombinator::remove(
$returnType,
new ArrayType(new MixedType(), $model)
),
new ObjectType(Collection::class)
);
}
}