-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
ModelCastHelper.php
165 lines (134 loc) · 6.15 KB
/
ModelCastHelper.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace NunoMaduro\Larastan\Properties;
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
use Illuminate\Support\Arr;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
class ModelCastHelper
{
public function __construct(protected ReflectionProvider $reflectionProvider)
{
}
public function getReadableType(string $cast, Type $originalType): Type
{
$castType = $this->parseCast($cast);
$attributeType = match ($castType) {
'int', 'integer' => new IntegerType(),
'real', 'float', 'double' => new FloatType(),
'decimal' => TypeCombinator::intersect(new StringType(), new AccessoryNumericStringType()),
'string' => new StringType(),
'bool', 'boolean' => new BooleanType(),
'object' => new ObjectType('stdClass'),
'array', 'json' => new ArrayType(new BenevolentUnionType([new IntegerType(), new StringType()]), new MixedType()),
'collection' => new ObjectType('Illuminate\Support\Collection'),
'date', 'datetime' => $this->getDateType(),
'immutable_date', 'immutable_datetime' => new ObjectType('Carbon\CarbonImmutable'),
'timestamp' => new IntegerType(),
default => null,
};
if ($attributeType) {
return $attributeType;
}
if (! $this->reflectionProvider->hasClass($cast)) {
return $originalType;
}
$classReflection = $this->reflectionProvider->getClass($cast);
if ($classReflection->isSubclassOf(Castable::class)) {
$methodReflection = $classReflection->getNativeMethod('castUsing');
$castUsingReturn = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if ($castUsingReturn instanceof ObjectType && $castReflection = $castUsingReturn->getClassReflection()) {
$classReflection = $castReflection;
}
}
if ($classReflection->isSubclassOf(CastsAttributes::class)) {
$methodReflection = $classReflection->getNativeMethod('get');
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}
if ($classReflection->isSubclassOf(CastsInboundAttributes::class)) {
return $originalType;
}
return new MixedType();
}
public function getWriteableType(string $cast, Type $originalType): Type
{
$castType = $this->parseCast($cast);
$attributeType = match ($castType) {
'int', 'integer' => new IntegerType(),
'real', 'float', 'double' => new FloatType(),
'decimal' => TypeCombinator::intersect(new StringType(), new AccessoryNumericStringType()),
'string' => new StringType(),
'bool', 'boolean' => TypeCombinator::union(new BooleanType(), new ConstantIntegerType(0), new ConstantIntegerType(1)),
'object' => new ObjectType('stdClass'),
'array', 'json' => new ArrayType(new BenevolentUnionType([new IntegerType(), new StringType()]), new MixedType()),
'collection' => new ObjectType('Illuminate\Support\Collection'),
'date', 'datetime' => $this->getDateType(),
'immutable_date', 'immutable_datetime' => new ObjectType('Carbon\CarbonImmutable'),
'timestamp' => new IntegerType(),
default => null,
};
if ($attributeType) {
return $attributeType;
}
if (! $this->reflectionProvider->hasClass($cast)) {
return $originalType;
}
$classReflection = $this->reflectionProvider->getClass($cast);
if ($classReflection->isSubclassOf(Castable::class)) {
$methodReflection = $classReflection->getNativeMethod('castUsing');
$castUsingReturn = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if ($castUsingReturn instanceof ObjectType && $castReflection = $castUsingReturn->getClassReflection()) {
$classReflection = $castReflection;
}
}
if (
$classReflection->isSubclassOf(CastsAttributes::class)
|| $classReflection->isSubclassOf(CastsInboundAttributes::class)
) {
$methodReflection = $classReflection->getNativeMethod('set');
$parameters = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getParameters();
$valueParameter = Arr::first($parameters, fn (ParameterReflection $parameterReflection) => $parameterReflection->getName() === 'value');
return $valueParameter->getType();
}
return new MixedType();
}
public function getDateType(): Type
{
$dateClass = class_exists(\Illuminate\Support\Facades\Date::class)
? \Illuminate\Support\Facades\Date::now()::class
: \Illuminate\Support\Carbon::class;
if ($dateClass === \Illuminate\Support\Carbon::class) {
return TypeCombinator::union(new ObjectType($dateClass), new ObjectType(\Carbon\Carbon::class));
}
return new ObjectType($dateClass);
}
/**
* @param string $cast
* @return string|null
*/
private function parseCast(string $cast): ?string
{
foreach (explode(':', $cast) as $part) {
// If the cast is prefixed with `encrypted:` we need to skip to the next
if ($part === 'encrypted') {
continue;
}
return $part;
}
return null;
}
}