16
16
use PHPStan \Reflection \ParametersAcceptorSelector ;
17
17
use PHPStan \Reflection \ReflectionProvider ;
18
18
<<<<<<< HEAD
19
+ <<<<<<< HEAD
19
20
use PHPStan \Type \BitwiseFlagHelper;
20
21
use PHPStan \Type \Constant \ConstantBooleanType ;
21
22
=======
22
23
use PHPStan \Type \ArrayType ;
23
24
use PHPStan \Type \BooleanType ;
25
+ =======
26
+ >>>>>>> return mixed type
24
27
use PHPStan \Type \Constant \ConstantBooleanType ;
25
28
use PHPStan \Type \Constant \ConstantIntegerType ;
26
29
use PHPStan \Type \Constant \ConstantStringType ;
27
30
use PHPStan \Type \ConstantTypeHelper ;
28
31
>>>>>>> Extend JsonThrowOnErrorDynamicReturnTypeExtension to detect knonw type from contssant string value
29
32
use PHPStan \Type \DynamicFunctionReturnTypeExtension;
30
- use PHPStan \Type \FloatType ;
31
- use PHPStan \Type \IntegerType ;
32
33
use PHPStan \Type \MixedType ;
33
34
use PHPStan \Type \ObjectType ;
34
- use PHPStan \Type \StringType ;
35
35
use PHPStan \Type \Type ;
36
36
use PHPStan \Type \TypeCombinator ;
37
- use PHPStan \Type \UnionType ;
38
37
use stdClass ;
39
38
use function json_decode ;
40
39
@@ -77,11 +76,7 @@ public function getTypeFromFunctionCall(
77
76
78
77
// narrow type for json_decode()
79
78
if ($ functionReflection ->getName () === 'json_decode ' ) {
80
- $ jsonDecodeNarrowedType = $ this ->narrowTypeForJsonDecode ($ functionCall , $ scope );
81
- // improve type
82
- if (! $ jsonDecodeNarrowedType instanceof MixedType) {
83
- $ defaultReturnType = $ jsonDecodeNarrowedType ;
84
- }
79
+ $ defaultReturnType = $ this ->narrowTypeForJsonDecode ($ functionCall , $ scope );
85
80
}
86
81
87
82
if (!isset ($ functionCall ->getArgs ()[$ argumentPosition ])) {
@@ -105,57 +100,34 @@ private function narrowTypeForJsonDecode(FuncCall $funcCall, Scope $scope): Type
105
100
$ firstValueType = $ scope ->getType ($ firstArgValue );
106
101
107
102
if ($ firstValueType instanceof ConstantStringType) {
108
- $ resolvedType = $ this ->resolveConstantStringType ($ firstValueType , $ isForceArray );
109
- } else {
110
- $ resolvedType = new MixedType ();
111
- }
112
-
113
- // prefer specific type
114
- if (! $ resolvedType instanceof MixedType) {
115
- return $ resolvedType ;
103
+ return $ this ->resolveConstantStringType ($ firstValueType , $ isForceArray );
116
104
}
117
105
118
106
// fallback type
119
107
if ($ isForceArray ) {
120
- return new UnionType ([
121
- new ArrayType (new MixedType (), new MixedType ()),
122
- new StringType (),
123
- new FloatType (),
124
- new IntegerType (),
125
- new BooleanType (),
126
- ]);
108
+ return new MixedType (true , new ObjectType (stdClass::class));
127
109
}
128
110
129
- // scalar types with stdClass
130
- return new UnionType ([
131
- new ObjectType (stdClass::class),
132
- new StringType (),
133
- new FloatType (),
134
- new IntegerType (),
135
- new BooleanType (),
136
- ]);
111
+ return new MixedType (true );
137
112
}
138
113
139
114
/**
140
115
* Is "json_decode(..., true)"?
141
- * @param Arg[] $args
142
116
*/
143
117
private function isForceArray (FuncCall $ funcCall ): bool
144
118
{
145
119
$ args = $ funcCall ->getArgs ();
146
120
147
- if (!isset ($ args [1 ])) {
121
+ if (! isset ($ args [1 ])) {
148
122
return false ;
149
123
}
150
124
151
125
$ secondArgValue = $ args [1 ]->value ;
152
- if ($ secondArgValue instanceof ConstFetch) {
153
- if ($ secondArgValue ->name ->toLowerString () === 'true ' ) {
154
- return true ;
155
- }
126
+ if (! $ secondArgValue instanceof ConstFetch) {
127
+ return false ;
156
128
}
157
129
158
- return false ;
130
+ return $ secondArgValue -> name -> toLowerString () === ' true ' ;
159
131
}
160
132
161
133
private function resolveConstantStringType (ConstantStringType $ constantStringType , bool $ isForceArray ): Type
0 commit comments