-
Notifications
You must be signed in to change notification settings - Fork 650
/
ArrayPointerAdjustmentReturnTypeProvider.php
120 lines (99 loc) · 3.81 KB
/
ArrayPointerAdjustmentReturnTypeProvider.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
<?php
namespace Psalm\Internal\Provider\ReturnTypeProvider;
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ArrayFetchAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Type;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TList;
use Psalm\Type\Atomic\TNonEmptyArray;
use Psalm\Type\Atomic\TNonEmptyList;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Union;
use UnexpectedValueException;
use function array_merge;
use function array_shift;
use function in_array;
class ArrayPointerAdjustmentReturnTypeProvider implements FunctionReturnTypeProviderInterface
{
/**
* These functions are already handled by the CoreGenericFunctions stub
*/
const IGNORE_FUNCTION_IDS_FOR_FALSE_RETURN_TYPE = [
'reset',
'end',
'current',
];
/**
* @return array<lowercase-string>
*/
public static function getFunctionIds(): array
{
return ['current', 'next', 'prev', 'reset', 'end'];
}
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): Union
{
$statements_source = $event->getStatementsSource();
$call_args = $event->getCallArgs();
$function_id = $event->getFunctionId();
if (!$statements_source instanceof StatementsAnalyzer) {
return Type::getMixed();
}
$first_arg = $call_args[0]->value ?? null;
if (!$first_arg) {
return Type::getMixed();
}
$first_arg_type = $statements_source->node_data->getType($first_arg);
if (!$first_arg_type) {
return Type::getMixed();
}
$atomic_types = $first_arg_type->getAtomicTypes();
$value_type = null;
$definitely_has_items = false;
while ($atomic_type = array_shift($atomic_types)) {
if ($atomic_type instanceof TTemplateParam) {
$atomic_types = array_merge($atomic_types, $atomic_type->as->getAtomicTypes());
continue;
}
if ($atomic_type instanceof TArray) {
$value_type = clone $atomic_type->type_params[1];
$definitely_has_items = $atomic_type instanceof TNonEmptyArray;
} elseif ($atomic_type instanceof TList) {
$value_type = clone $atomic_type->type_param;
$definitely_has_items = $atomic_type instanceof TNonEmptyList;
} elseif ($atomic_type instanceof TKeyedArray) {
$value_type = $atomic_type->getGenericValueType();
$definitely_has_items = $atomic_type->getGenericArrayType() instanceof TNonEmptyArray;
} else {
return Type::getMixed();
}
}
if (!$value_type) {
throw new UnexpectedValueException('This should never happen');
}
if ($value_type->isEmpty()) {
$value_type = Type::getFalse();
} elseif (!$definitely_has_items || self::isFunctionAlreadyHandledByStub($function_id)) {
$value_type->addType(new TFalse);
$codebase = $statements_source->getCodebase();
if ($codebase->config->ignore_internal_falsable_issues) {
$value_type->ignore_falsable_issues = true;
}
}
ArrayFetchAnalyzer::taintArrayFetch(
$statements_source,
$first_arg,
null,
$value_type,
Type::getMixed()
);
return $value_type;
}
private static function isFunctionAlreadyHandledByStub(string $function_id): bool
{
return !in_array($function_id, self::IGNORE_FUNCTION_IDS_FOR_FALSE_RETURN_TYPE, true);
}
}