/
ArgumentMapPopulator.php
145 lines (124 loc) · 4.84 KB
/
ArgumentMapPopulator.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Psalm\Internal\Analyzer\Statements\Expression\Call;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use Psalm\Codebase;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use function array_reverse;
use function array_shift;
use function is_string;
use function reset;
use function strlen;
use function substr;
use function token_get_all;
/**
* @internal
*/
class ArgumentMapPopulator
{
/**
* @param MethodCall|StaticCall|FuncCall|New_ $stmt
*/
public static function recordArgumentPositions(
StatementsAnalyzer $statements_analyzer,
Expr $stmt,
Codebase $codebase,
string $function_reference
): void {
$file_content = $codebase->file_provider->getContents($statements_analyzer->getFilePath());
// Find opening paren
$first_argument = $stmt->getArgs()[0] ?? null;
$first_argument_character = $first_argument !== null
? $first_argument->getStartFilePos()
: $stmt->getEndFilePos();
$method_name_and_first_paren_source_code_length = $first_argument_character - $stmt->getStartFilePos();
// FIXME: There are weird ::__construct calls in the AST for `extends`
if ($method_name_and_first_paren_source_code_length <= 0) {
return;
}
$method_name_and_first_paren_source_code = substr(
$file_content,
$stmt->getStartFilePos(),
$method_name_and_first_paren_source_code_length
);
$method_name_and_first_paren_tokens = token_get_all('<?php ' . $method_name_and_first_paren_source_code);
$opening_paren_position = $first_argument_character;
foreach (array_reverse($method_name_and_first_paren_tokens) as $token) {
$token = is_string($token) ? $token : $token[1];
$opening_paren_position -= strlen($token);
if ($token === '(') {
break;
}
}
// New instances can be created without parens
if ($opening_paren_position < $stmt->getStartFilePos()) {
return;
}
// Record ranges of the source code that need to be tokenized to find commas
/** @var array{0: int, 1: int}[] $ranges */
$ranges = [];
// Add range between opening paren and first argument
$first_argument = $stmt->getArgs()[0] ?? null;
$first_argument_starting_position = $first_argument !== null
? $first_argument->getStartFilePos()
: $stmt->getEndFilePos();
$first_range_starting_position = $opening_paren_position + 1;
if ($first_range_starting_position !== $first_argument_starting_position) {
$ranges[] = [$first_range_starting_position, $first_argument_starting_position];
}
// Add range between arguments
foreach ($stmt->getArgs() as $i => $argument) {
$range_start = $argument->getEndFilePos() + 1;
$next_argument = $stmt->getArgs()[$i + 1] ?? null;
$range_end = $next_argument !== null
? $next_argument->getStartFilePos()
: $stmt->getEndFilePos();
if ($range_start !== $range_end) {
$ranges[] = [$range_start, $range_end];
}
}
$commas = [];
foreach ($ranges as $range) {
$position = $range[0];
$length = $range[1] - $position;
if ($length > 0) {
$range_source_code = substr($file_content, $position, $length);
$range_tokens = token_get_all('<?php ' . $range_source_code);
array_shift($range_tokens);
$current_position = $position;
foreach ($range_tokens as $token) {
$token = is_string($token) ? $token : $token[1];
if ($token === ',') {
$commas[] = $current_position;
}
$current_position += strlen($token);
}
}
}
$argument_start_position = $opening_paren_position + 1;
$argument_number = 0;
while (!empty($commas)) {
$comma = reset($commas);
array_shift($commas);
$codebase->analyzer->addNodeArgument(
$statements_analyzer->getFilePath(),
$argument_start_position,
$comma,
$function_reference,
$argument_number
);
++$argument_number;
$argument_start_position = $comma + 1;
}
$codebase->analyzer->addNodeArgument(
$statements_analyzer->getFilePath(),
$argument_start_position,
$stmt->getEndFilePos(),
$function_reference,
$argument_number
);
}
}