13
13
14
14
namespace CodeIgniter \PHPStan \Type ;
15
15
16
+ use CodeIgniter \PHPStan \NodeVisitor \ModelReturnTypeTransformVisitor ;
17
+ use PhpParser \Node \Expr ;
18
+ use PhpParser \Node \Expr \MethodCall ;
16
19
use PHPStan \Analyser \Scope ;
17
20
use PHPStan \Reflection \ClassReflection ;
18
21
use PHPStan \Reflection \ReflectionProvider ;
@@ -33,8 +36,11 @@ final class ModelFetchedReturnTypeHelper
33
36
* @var array<string, class-string<Type>>
34
37
*/
35
38
private static array $ notStringFormattedFields = [
36
- 'success ' => BooleanType::class,
37
- 'user_id ' => IntegerType::class,
39
+ 'active ' => BooleanType::class,
40
+ 'force_reset ' => BooleanType::class,
41
+ 'id ' => IntegerType::class,
42
+ 'success ' => BooleanType::class,
43
+ 'user_id ' => IntegerType::class,
38
44
];
39
45
40
46
/**
@@ -66,9 +72,15 @@ public function __construct(
66
72
}
67
73
}
68
74
69
- public function getFetchedReturnType (ClassReflection $ classReflection , Scope $ scope ): Type
75
+ public function getFetchedReturnType (ClassReflection $ classReflection , ? MethodCall $ methodCall , Scope $ scope ): Type
70
76
{
71
- $ returnType = $ this ->getNativeStringPropertyValue ($ classReflection , $ scope , 'returnType ' );
77
+ $ returnType = $ this ->getNativeStringPropertyValue ($ classReflection , $ scope , ModelReturnTypeTransformVisitor::RETURN_TYPE );
78
+
79
+ if ($ methodCall !== null && $ methodCall ->hasAttribute (ModelReturnTypeTransformVisitor::RETURN_TYPE )) {
80
+ /** @var Expr $returnExpr */
81
+ $ returnExpr = $ methodCall ->getAttribute (ModelReturnTypeTransformVisitor::RETURN_TYPE );
82
+ $ returnType = $ this ->getStringValueFromExpr ($ returnExpr , $ scope );
83
+ }
72
84
73
85
if ($ returnType === 'object ' ) {
74
86
return new ObjectType (stdClass::class);
@@ -88,7 +100,9 @@ public function getFetchedReturnType(ClassReflection $classReflection, Scope $sc
88
100
private function getArrayReturnType (ClassReflection $ classReflection , Scope $ scope ): Type
89
101
{
90
102
$ this ->fillDateFields ($ classReflection , $ scope );
91
- $ fieldsTypes = $ this ->getNativePropertyType ($ classReflection , $ scope , 'allowedFields ' )->getConstantArrays ();
103
+ $ fieldsTypes = $ scope ->getType (
104
+ $ classReflection ->getNativeProperty ('allowedFields ' )->getNativeReflection ()->getDefaultValueExpression ()
105
+ )->getConstantArrays ();
92
106
93
107
if ($ fieldsTypes === []) {
94
108
return new ConstantArrayType ([], []);
@@ -131,20 +145,23 @@ private function fillDateFields(ClassReflection $classReflection, Scope $scope):
131
145
}
132
146
}
133
147
134
- private function getNativePropertyType (ClassReflection $ classReflection , Scope $ scope , string $ property ): Type
148
+ private function getNativeStringPropertyValue (ClassReflection $ classReflection , Scope $ scope , string $ property ): string
135
149
{
136
150
if (! $ classReflection ->hasNativeProperty ($ property )) {
137
151
throw new ShouldNotHappenException (sprintf ('Native property %s::$%s does not exist. ' , $ classReflection ->getDisplayName (), $ property ));
138
152
}
139
153
140
- return $ scope ->getType ($ classReflection ->getNativeProperty ($ property )->getNativeReflection ()->getDefaultValueExpression ());
154
+ return $ this ->getStringValueFromExpr (
155
+ $ classReflection ->getNativeProperty ($ property )->getNativeReflection ()->getDefaultValueExpression (),
156
+ $ scope
157
+ );
141
158
}
142
159
143
- private function getNativeStringPropertyValue ( ClassReflection $ classReflection , Scope $ scope, string $ property ): string
160
+ private function getStringValueFromExpr ( Expr $ expr , Scope $ scope ): string
144
161
{
145
- $ propertyType = $ this -> getNativePropertyType ( $ classReflection , $ scope , $ property )->getConstantStrings ();
146
- assert (count ($ propertyType ) === 1 );
162
+ $ exprType = $ scope -> getType ( $ expr )->getConstantStrings ();
163
+ assert (count ($ exprType ) === 1 );
147
164
148
- return current ($ propertyType )->getValue ();
165
+ return current ($ exprType )->getValue ();
149
166
}
150
167
}
0 commit comments