-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
EloquentBuilderExtension.php
88 lines (70 loc) · 2.99 KB
/
EloquentBuilderExtension.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
<?php
declare(strict_types=1);
namespace NunoMaduro\Larastan\ReturnTypes;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Collection;
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\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateMixedType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
final class EloquentBuilderExtension implements DynamicMethodReturnTypeExtension
{
/** @var BuilderHelper */
private $builderHelper;
/** @var ReflectionProvider */
private $reflectionProvider;
public function __construct(ReflectionProvider $reflectionProvider, BuilderHelper $builderHelper)
{
$this->builderHelper = $builderHelper;
$this->reflectionProvider = $reflectionProvider;
}
public function getClass(): string
{
return EloquentBuilder::class;
}
public function isMethodSupported(MethodReflection $methodReflection): bool
{
$builderReflection = $this->reflectionProvider->getClass(EloquentBuilder::class);
// Don't handle dynamic wheres
if (Str::startsWith($methodReflection->getName(), 'where') &&
! $builderReflection->hasNativeMethod($methodReflection->getName())
) {
return false;
}
if (Str::startsWith($methodReflection->getName(), 'find') &&
$builderReflection->hasNativeMethod($methodReflection->getName())
) {
return false;
}
$templateTypeMap = $methodReflection->getDeclaringClass()->getActiveTemplateTypeMap();
if (! $templateTypeMap->getType('TModelClass') instanceof TypeWithClassName) {
return false;
}
return $builderReflection->hasNativeMethod($methodReflection->getName());
}
public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type {
$returnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
$templateTypeMap = $methodReflection->getDeclaringClass()->getActiveTemplateTypeMap();
/** @var Type|ObjectType|TemplateMixedType $modelType */
$modelType = $templateTypeMap->getType('TModelClass');
if ($modelType instanceof ObjectType && in_array(Collection::class, $returnType->getReferencedClasses(), true)) {
$collectionClassName = $this->builderHelper->determineCollectionClassName($modelType->getClassName());
return new GenericObjectType($collectionClassName, [new IntegerType(), $modelType]);
}
return $returnType;
}
}