/
JsonThrowOnErrorDynamicReturnTypeExtension.php
125 lines (101 loc) · 3.83 KB
/
JsonThrowOnErrorDynamicReturnTypeExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
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\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use stdClass;
use function is_bool;
use function json_decode;
class JsonThrowOnErrorDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
/** @var array<string, int> */
private array $argumentPositions = [
'json_encode' => 1,
'json_decode' => 3,
];
public function __construct(
private ReflectionProvider $reflectionProvider,
private BitwiseFlagHelper $bitwiseFlagAnalyser,
)
{
}
public function isFunctionSupported(
FunctionReflection $functionReflection,
): bool
{
if ($functionReflection->getName() === 'json_decode') {
return true;
}
return $this->reflectionProvider->hasConstant(new FullyQualified('JSON_THROW_ON_ERROR'), null) && $functionReflection->getName() === 'json_encode';
}
public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): Type
{
$argumentPosition = $this->argumentPositions[$functionReflection->getName()];
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
if ($functionReflection->getName() === 'json_decode') {
$defaultReturnType = $this->narrowTypeForJsonDecode($functionCall, $scope, $defaultReturnType);
}
if (!isset($functionCall->getArgs()[$argumentPosition])) {
return $defaultReturnType;
}
$optionsExpr = $functionCall->getArgs()[$argumentPosition]->value;
if ($functionReflection->getName() === 'json_encode' && $this->bitwiseFlagAnalyser->bitwiseOrContainsConstant($optionsExpr, $scope, 'JSON_THROW_ON_ERROR')->yes()) {
return TypeCombinator::remove($defaultReturnType, new ConstantBooleanType(false));
}
return $defaultReturnType;
}
private function narrowTypeForJsonDecode(FuncCall $funcCall, Scope $scope, Type $fallbackType): Type
{
$args = $funcCall->getArgs();
$isArrayWithoutStdClass = $this->isForceArrayWithoutStdClass($funcCall, $scope);
$firstValueType = $scope->getType($args[0]->value);
if ($firstValueType instanceof ConstantStringType) {
return $this->resolveConstantStringType($firstValueType, $isArrayWithoutStdClass);
}
if ($isArrayWithoutStdClass) {
return TypeCombinator::remove($fallbackType, new ObjectType(stdClass::class));
}
return $fallbackType;
}
/**
* Is "json_decode(..., true)"?
*/
private function isForceArrayWithoutStdClass(FuncCall $funcCall, Scope $scope): bool
{
$args = $funcCall->getArgs();
if (!isset($args[1])) {
return false;
}
$secondArgType = $scope->getType($args[1]->value);
$secondArgValue = $secondArgType instanceof ConstantScalarType ? $secondArgType->getValue() : null;
if (is_bool($secondArgValue)) {
return $secondArgValue;
}
if ($secondArgValue !== null || !isset($args[3])) {
return false;
}
// depends on used constants, @see https://www.php.net/manual/en/json.constants.php#constant.json-object-as-array
return $this->bitwiseFlagAnalyser->bitwiseOrContainsConstant($args[3]->value, $scope, 'JSON_OBJECT_AS_ARRAY')->yes();
}
private function resolveConstantStringType(ConstantStringType $constantStringType, bool $isForceArray): Type
{
$decodedValue = json_decode($constantStringType->getValue(), $isForceArray);
return ConstantTypeHelper::getTypeFromValue($decodedValue);
}
}