Skip to content

Commit 741abd2

Browse files
herndlmondrejmirtes
authored andcommitted
Simplify fallback type handling
1 parent 3ad68aa commit 741abd2

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPStan\Type\ConstantScalarType;
1515
use PHPStan\Type\ConstantTypeHelper;
1616
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
17-
use PHPStan\Type\MixedType;
1817
use PHPStan\Type\ObjectType;
1918
use PHPStan\Type\Type;
2019
use 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

Comments
 (0)