1414use PHPStan \Type \ConstantScalarType ;
1515use PHPStan \Type \ConstantTypeHelper ;
1616use PHPStan \Type \DynamicFunctionReturnTypeExtension ;
17- use PHPStan \Type \MixedType ;
1817use PHPStan \Type \ObjectType ;
1918use PHPStan \Type \Type ;
2019use PHPStan \Type \TypeCombinator ;
@@ -59,7 +58,7 @@ public function getTypeFromFunctionCall(
5958 $ defaultReturnType = ParametersAcceptorSelector::selectSingle ($ functionReflection ->getVariants ())->getReturnType ();
6059
6160 if ($ functionReflection ->getName () === 'json_decode ' ) {
62- $ defaultReturnType = $ this ->narrowTypeForJsonDecode ($ functionCall , $ scope );
61+ $ defaultReturnType = $ this ->narrowTypeForJsonDecode ($ functionCall , $ scope, $ defaultReturnType );
6362 }
6463
6564 if (!isset ($ functionCall ->getArgs ()[$ argumentPosition ])) {
@@ -74,7 +73,7 @@ public function getTypeFromFunctionCall(
7473 return $ defaultReturnType ;
7574 }
7675
77- private function narrowTypeForJsonDecode (FuncCall $ funcCall , Scope $ scope ): Type
76+ private function narrowTypeForJsonDecode (FuncCall $ funcCall , Scope $ scope, Type $ fallbackType ): Type
7877 {
7978 $ args = $ funcCall ->getArgs ();
8079 $ isArrayWithoutStdClass = $ this ->isForceArrayWithoutStdClass ($ funcCall , $ scope );
@@ -86,12 +85,11 @@ private function narrowTypeForJsonDecode(FuncCall $funcCall, Scope $scope): Type
8685 return $ this ->resolveConstantStringType ($ firstValueType , $ isArrayWithoutStdClass );
8786 }
8887
89- // fallback type
90- if ($ isArrayWithoutStdClass === true ) {
91- return new MixedType (true , new ObjectType (stdClass::class));
88+ if ($ isArrayWithoutStdClass ) {
89+ return TypeCombinator::remove ($ fallbackType , new ObjectType (stdClass::class));
9290 }
9391
94- return new MixedType ( true ) ;
92+ return $ fallbackType ;
9593 }
9694
9795 /**
0 commit comments