Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1599,18 +1599,6 @@ parameters:
count: 2
path: src/Type/Php/IsSubclassOfFunctionTypeSpecifyingExtension.php

-
message: '#^Doing instanceof PHPStan\\Type\\ConstantScalarType is error\-prone and deprecated\. Use Type\:\:isConstantScalarValue\(\) or Type\:\:getConstantScalarTypes\(\) or Type\:\:getConstantScalarValues\(\) instead\.$#'
identifier: phpstanApi.instanceofType
count: 1
path: src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php

-
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantStringType is error\-prone and deprecated\. Use Type\:\:getConstantStrings\(\) instead\.$#'
identifier: phpstanApi.instanceofType
count: 1
path: src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php

-
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantStringType is error\-prone and deprecated\. Use Type\:\:getConstantStrings\(\) instead\.$#'
identifier: phpstanApi.instanceofType
Expand Down
14 changes: 10 additions & 4 deletions src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
use PHPStan\Type\BitwiseFlagHelper;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\ConstantTypeHelper;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
use function is_bool;
use function json_decode;

Expand Down Expand Up @@ -87,8 +87,14 @@ private function narrowTypeForJsonDecode(FuncCall $funcCall, Scope $scope, Type
}

$firstValueType = $scope->getType($args[0]->value);
if ($firstValueType instanceof ConstantStringType) {
return $this->resolveConstantStringType($firstValueType, $isForceArray);
if ($firstValueType->getConstantStrings() !== []) {
$types = [];

foreach ($firstValueType->getConstantStrings() as $constantString) {
$types[] = $this->resolveConstantStringType($constantString, $isForceArray);
}

return TypeCombinator::union(...$types);
}

if ($isForceArray) {
Expand All @@ -109,7 +115,7 @@ private function isForceArray(FuncCall $funcCall, Scope $scope): bool
}

$secondArgType = $scope->getType($args[1]->value);
$secondArgValue = $secondArgType instanceof ConstantScalarType ? $secondArgType->getValue() : null;
$secondArgValue = count($secondArgType->getConstantScalarValues()) === 1 ? $secondArgType->getConstantScalarValues()[0] : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$secondArgType->getConstantScalarValues() with more than one value should also be properly handled here


if (is_bool($secondArgValue)) {
return $secondArgValue;
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/nsrt/json-decode/narrow_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ function ($mixed) {
$value = json_decode($mixed, false);
assertType('mixed', $value);
};

function(string $json): void {
/** @var '{}'|'null' $json */
$value = json_decode($json);
assertType('stdClass|null', $value);

$value = json_decode($json, true);
assertType('array{}|null', $value);
};
Loading