diff --git a/src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php b/src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php index be7c236364..a06e2b3370 100644 --- a/src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php +++ b/src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php @@ -14,10 +14,9 @@ use PHPStan\Type\ConstantScalarType; use PHPStan\Type\ConstantTypeHelper; use PHPStan\Type\DynamicFunctionReturnTypeExtension; -use PHPStan\Type\ObjectType; +use PHPStan\Type\ObjectWithoutClassType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; -use stdClass; use function is_bool; use function json_decode; @@ -76,18 +75,18 @@ public function getTypeFromFunctionCall( private function narrowTypeForJsonDecode(FuncCall $funcCall, Scope $scope, Type $fallbackType): Type { $args = $funcCall->getArgs(); - $isArrayWithoutStdClass = $this->isForceArrayWithoutStdClass($funcCall, $scope); + $isForceArray = $this->isForceArray($funcCall, $scope); if (!isset($args[0])) { return $fallbackType; } $firstValueType = $scope->getType($args[0]->value); if ($firstValueType instanceof ConstantStringType) { - return $this->resolveConstantStringType($firstValueType, $isArrayWithoutStdClass); + return $this->resolveConstantStringType($firstValueType, $isForceArray); } - if ($isArrayWithoutStdClass) { - return TypeCombinator::remove($fallbackType, new ObjectType(stdClass::class)); + if ($isForceArray) { + return TypeCombinator::remove($fallbackType, new ObjectWithoutClassType()); } return $fallbackType; @@ -96,7 +95,7 @@ private function narrowTypeForJsonDecode(FuncCall $funcCall, Scope $scope, Type /** * Is "json_decode(..., true)"? */ - private function isForceArrayWithoutStdClass(FuncCall $funcCall, Scope $scope): bool + private function isForceArray(FuncCall $funcCall, Scope $scope): bool { $args = $funcCall->getArgs(); if (!isset($args[1])) { diff --git a/tests/PHPStan/Analyser/data/json-decode/json_object_as_array.php b/tests/PHPStan/Analyser/data/json-decode/json_object_as_array.php index cf8b093fae..517c0bcbf0 100644 --- a/tests/PHPStan/Analyser/data/json-decode/json_object_as_array.php +++ b/tests/PHPStan/Analyser/data/json-decode/json_object_as_array.php @@ -7,19 +7,19 @@ // @see https://3v4l.org/YFlHF function ($mixed) { $value = json_decode($mixed, null, 512, JSON_OBJECT_AS_ARRAY); - assertType('mixed~stdClass', $value); + assertType('mixed~object', $value); }; function ($mixed) { $flagsAsVariable = JSON_OBJECT_AS_ARRAY; $value = json_decode($mixed, null, 512, $flagsAsVariable); - assertType('mixed~stdClass', $value); + assertType('mixed~object', $value); }; function ($mixed) { $value = json_decode($mixed, null, 512, JSON_OBJECT_AS_ARRAY | JSON_BIGINT_AS_STRING); - assertType('mixed~stdClass', $value); + assertType('mixed~object', $value); }; function ($mixed) { diff --git a/tests/PHPStan/Analyser/data/json-decode/narrow_type_with_force_array.php b/tests/PHPStan/Analyser/data/json-decode/narrow_type_with_force_array.php index 479085c814..cb0917cdf0 100644 --- a/tests/PHPStan/Analyser/data/json-decode/narrow_type_with_force_array.php +++ b/tests/PHPStan/Analyser/data/json-decode/narrow_type_with_force_array.php @@ -24,5 +24,5 @@ function ($mixed) { $value = json_decode($mixed, true); - assertType('mixed~stdClass', $value); + assertType('mixed~object', $value); };