1616use PHPStan \Reflection \ParametersAcceptorSelector ;
1717use PHPStan \Reflection \ReflectionProvider ;
1818<<<<<<< HEAD
19+ <<<<<<< HEAD
1920use PHPStan \Type \BitwiseFlagHelper;
2021use PHPStan \Type \Constant \ConstantBooleanType ;
2122=======
2223use PHPStan \Type \ArrayType ;
2324use PHPStan \Type \BooleanType ;
25+ =======
26+ >>>>>>> return mixed type
2427use PHPStan \Type \Constant \ConstantBooleanType ;
2528use PHPStan \Type \Constant \ConstantIntegerType ;
2629use PHPStan \Type \Constant \ConstantStringType ;
2730use PHPStan \Type \ConstantTypeHelper ;
2831>>>>>>> Extend JsonThrowOnErrorDynamicReturnTypeExtension to detect knonw type from contssant string value
2932use PHPStan \Type \DynamicFunctionReturnTypeExtension;
30- use PHPStan \Type \FloatType ;
31- use PHPStan \Type \IntegerType ;
3233use PHPStan \Type \MixedType ;
3334use PHPStan \Type \ObjectType ;
34- use PHPStan \Type \StringType ;
3535use PHPStan \Type \Type ;
3636use PHPStan \Type \TypeCombinator ;
37- use PHPStan \Type \UnionType ;
3837use stdClass ;
3938use function json_decode ;
4039
@@ -77,11 +76,7 @@ public function getTypeFromFunctionCall(
7776
7877 // narrow type for json_decode()
7978 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 );
8580 }
8681
8782 if (!isset ($ functionCall ->getArgs ()[$ argumentPosition ])) {
@@ -105,57 +100,34 @@ private function narrowTypeForJsonDecode(FuncCall $funcCall, Scope $scope): Type
105100 $ firstValueType = $ scope ->getType ($ firstArgValue );
106101
107102 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 );
116104 }
117105
118106 // fallback type
119107 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));
127109 }
128110
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 );
137112 }
138113
139114 /**
140115 * Is "json_decode(..., true)"?
141- * @param Arg[] $args
142116 */
143117 private function isForceArray (FuncCall $ funcCall ): bool
144118 {
145119 $ args = $ funcCall ->getArgs ();
146120
147- if (!isset ($ args [1 ])) {
121+ if (! isset ($ args [1 ])) {
148122 return false ;
149123 }
150124
151125 $ 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 ;
156128 }
157129
158- return false ;
130+ return $ secondArgValue -> name -> toLowerString () === ' true ' ;
159131 }
160132
161133 private function resolveConstantStringType (ConstantStringType $ constantStringType , bool $ isForceArray ): Type
0 commit comments