|
15 | 15 |
|
16 | 16 | use PhpParser\Node\Expr\FuncCall; |
17 | 17 | use PHPStan\Analyser\Scope; |
18 | | -use PHPStan\Reflection\ClassReflection; |
19 | 18 | use PHPStan\Reflection\FunctionReflection; |
20 | | -use PHPStan\Reflection\ReflectionProvider; |
21 | | -use PHPStan\ShouldNotHappenException; |
22 | | -use PHPStan\Type\BooleanType; |
23 | | -use PHPStan\Type\Constant\ConstantArrayType; |
24 | | -use PHPStan\Type\Constant\ConstantStringType; |
25 | 19 | use PHPStan\Type\DynamicFunctionReturnTypeExtension; |
26 | | -use PHPStan\Type\IntegerType; |
27 | 20 | use PHPStan\Type\NonAcceptingNeverType; |
28 | | -use PHPStan\Type\ObjectType; |
29 | | -use PHPStan\Type\ObjectWithoutClassType; |
30 | | -use PHPStan\Type\StringType; |
31 | 21 | use PHPStan\Type\Type; |
32 | | -use stdClass; |
33 | 22 |
|
34 | 23 | final class FakeFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension |
35 | 24 | { |
36 | | - /** |
37 | | - * @var array<string, class-string<Type>> |
38 | | - */ |
39 | | - private static array $notStringFormattedFields = [ |
40 | | - 'success' => BooleanType::class, |
41 | | - 'user_id' => IntegerType::class, |
42 | | - ]; |
43 | | - |
44 | | - /** |
45 | | - * @var array<string, class-string<Type>> |
46 | | - */ |
47 | | - private static array $typeInterpolations = [ |
48 | | - 'bool' => BooleanType::class, |
49 | | - 'int' => IntegerType::class, |
50 | | - ]; |
51 | | - |
52 | | - /** |
53 | | - * @var list<string> |
54 | | - */ |
55 | | - private array $dateFields = []; |
56 | | - |
57 | | - /** |
58 | | - * @param array<string, string> $notStringFormattedFieldsArray |
59 | | - */ |
60 | 25 | public function __construct( |
| 26 | + private readonly ModelFetchedReturnTypeHelper $modelFetchedReturnTypeHelper, |
61 | 27 | private readonly FactoriesReturnTypeHelper $factoriesReturnTypeHelper, |
62 | | - private readonly ReflectionProvider $reflectionProvider, |
63 | | - array $notStringFormattedFieldsArray |
64 | | - ) { |
65 | | - foreach ($notStringFormattedFieldsArray as $field => $type) { |
66 | | - if (! isset(self::$typeInterpolations[$type])) { |
67 | | - continue; |
68 | | - } |
69 | | - |
70 | | - self::$notStringFormattedFields[$field] = self::$typeInterpolations[$type]; |
71 | | - } |
72 | | - } |
| 28 | + ) {} |
73 | 29 |
|
74 | 30 | public function isFunctionSupported(FunctionReflection $functionReflection): bool |
75 | 31 | { |
@@ -98,83 +54,6 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, |
98 | 54 |
|
99 | 55 | $classReflection = current($classReflections); |
100 | 56 |
|
101 | | - $returnType = $this->getNativeStringPropertyValue($classReflection, $scope, 'returnType'); |
102 | | - |
103 | | - if ($returnType === 'object') { |
104 | | - return new ObjectType(stdClass::class); |
105 | | - } |
106 | | - |
107 | | - if ($returnType === 'array') { |
108 | | - return $this->getArrayReturnType($classReflection, $scope); |
109 | | - } |
110 | | - |
111 | | - if ($this->reflectionProvider->hasClass($returnType)) { |
112 | | - return new ObjectType($returnType); |
113 | | - } |
114 | | - |
115 | | - return new ObjectWithoutClassType(); |
116 | | - } |
117 | | - |
118 | | - private function getArrayReturnType(ClassReflection $classReflection, Scope $scope): Type |
119 | | - { |
120 | | - $this->fillDateFields($classReflection, $scope); |
121 | | - $fieldsTypes = $this->getNativePropertyType($classReflection, $scope, 'allowedFields')->getConstantArrays(); |
122 | | - |
123 | | - if ($fieldsTypes === []) { |
124 | | - return new ConstantArrayType([], []); |
125 | | - } |
126 | | - |
127 | | - $fields = array_filter(array_map( |
128 | | - static fn (Type $type) => current($type->getConstantStrings()), |
129 | | - current($fieldsTypes)->getValueTypes() |
130 | | - )); |
131 | | - |
132 | | - return new ConstantArrayType( |
133 | | - $fields, |
134 | | - array_map(function (ConstantStringType $fieldType) use ($classReflection, $scope): Type { |
135 | | - $field = $fieldType->getValue(); |
136 | | - |
137 | | - if (array_key_exists($field, self::$notStringFormattedFields)) { |
138 | | - $type = self::$notStringFormattedFields[$field]; |
139 | | - |
140 | | - return new $type(); |
141 | | - } |
142 | | - |
143 | | - if ( |
144 | | - in_array($field, $this->dateFields, true) |
145 | | - && $this->getNativeStringPropertyValue($classReflection, $scope, 'dateFormat') === 'int' |
146 | | - ) { |
147 | | - return new IntegerType(); |
148 | | - } |
149 | | - |
150 | | - return new StringType(); |
151 | | - }, $fields) |
152 | | - ); |
153 | | - } |
154 | | - |
155 | | - private function fillDateFields(ClassReflection $classReflection, Scope $scope): void |
156 | | - { |
157 | | - foreach (['createdAt', 'updatedAt', 'deletedAt'] as $property) { |
158 | | - if ($classReflection->hasNativeProperty($property)) { |
159 | | - $this->dateFields[] = $this->getNativeStringPropertyValue($classReflection, $scope, $property); |
160 | | - } |
161 | | - } |
162 | | - } |
163 | | - |
164 | | - private function getNativePropertyType(ClassReflection $classReflection, Scope $scope, string $property): Type |
165 | | - { |
166 | | - if (! $classReflection->hasNativeProperty($property)) { |
167 | | - throw new ShouldNotHappenException(sprintf('Native property %s::$%s does not exist.', $classReflection->getDisplayName(), $property)); |
168 | | - } |
169 | | - |
170 | | - return $scope->getType($classReflection->getNativeProperty($property)->getNativeReflection()->getDefaultValueExpression()); |
171 | | - } |
172 | | - |
173 | | - private function getNativeStringPropertyValue(ClassReflection $classReflection, Scope $scope, string $property): string |
174 | | - { |
175 | | - $propertyType = $this->getNativePropertyType($classReflection, $scope, $property)->getConstantStrings(); |
176 | | - assert(count($propertyType) === 1); |
177 | | - |
178 | | - return current($propertyType)->getValue(); |
| 57 | + return $this->modelFetchedReturnTypeHelper->getFetchedReturnType($classReflection, $scope); |
179 | 58 | } |
180 | 59 | } |
0 commit comments