-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTemplatedStringParser.php
205 lines (174 loc) · 6.72 KB
/
TemplatedStringParser.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
declare(strict_types=1);
namespace Boesing\PsalmPluginStringf\Parser\TemplatedStringParser;
use Boesing\PsalmPluginStringf\Parser\PhpParser\ArgumentValueParser;
use PhpParser\Node\Arg;
use Psalm\Context;
use Psalm\StatementsSource;
use Webmozart\Assert\Assert;
use function array_filter;
use function assert;
use function count;
use function in_array;
use function max;
use function preg_match_all;
use function sprintf;
use function strlen;
use function substr_replace;
use const ARRAY_FILTER_USE_BOTH;
use const PREG_OFFSET_CAPTURE;
use const PREG_SET_ORDER;
/**
* This class was heavily inspired by {@link https://github.com/phpstan/phpstan-src/blob/c471c7b050e0929daf432288770de673b394a983/src/Rules/Functions/PrintfParametersRule.php PHPStan}.
* Part of this file therefore belongs to the work of Ondřej Mirtes.
*/
final class TemplatedStringParser
{
private const ARGUMENT_SCAN_REGEX_PATTERN_PREFIX = '(?<before>%*)%(?:(?<position>\d+)\$)?[-+]?(?:[ 0]|'
. '(?:\'[^%]))?-?\d*(?:\.\d*)?';
private const PRINTF_SPECIFIERS_REGEX_PATTERN_TEMPLATE = '[bcdeEfFgGosuxX%s]';
private const SCANF_SPECIFIERS_REGEX_PATTERN_TEMPLATE = '(?:[cdDeEfinosuxX%s]|\[[^\]]+\])';
private const SPECIFIER_SINCE_PHP8 = 'hH';
private string $templateWithoutPlaceholder;
/** @psalm-var array<positive-int,Placeholder> */
private array $placeholders;
private string $template;
private StatementsSource $statementsSource;
/**
* @psalm-param positive-int $phpVersion
*/
private function __construct(
string $functionName,
string $template,
int $phpVersion,
bool $allowIntegerForStringPlaceholder,
StatementsSource $statementsSource
) {
$this->template = $template;
$this->templateWithoutPlaceholder = $template;
$this->placeholders = [];
$this->statementsSource = $statementsSource;
$this->parse($functionName, $template, $phpVersion, $allowIntegerForStringPlaceholder);
}
private function parse(
string $functionName,
string $template,
int $phpVersion,
bool $allowIntegerForStringPlaceholder
): void {
$additionalSpecifierDependingOnPhpVersion = '';
if ($phpVersion >= 80000) {
$additionalSpecifierDependingOnPhpVersion .= self::SPECIFIER_SINCE_PHP8;
}
$specifiers = sprintf(
in_array($functionName, ['sprintf', 'printf'], true)
? self::PRINTF_SPECIFIERS_REGEX_PATTERN_TEMPLATE : self::SCANF_SPECIFIERS_REGEX_PATTERN_TEMPLATE,
$additionalSpecifierDependingOnPhpVersion,
);
$pattern = self::ARGUMENT_SCAN_REGEX_PATTERN_PREFIX . $specifiers;
$potentialPlaceholders = [];
preg_match_all(
sprintf('~%s~', $pattern),
$template,
$potentialPlaceholders,
PREG_SET_ORDER | PREG_OFFSET_CAPTURE,
);
if ($potentialPlaceholders === []) {
return;
}
$placeholders = array_filter(
$potentialPlaceholders,
/** @param array{before:array{0:string}} $placeholder */
static function (
array $placeholder
): bool {
$patternPrefix = $placeholder['before'][0];
return strlen($patternPrefix) % 2 === 0;
},
ARRAY_FILTER_USE_BOTH,
);
if ($placeholders === []) {
return;
}
/** @var array<positive-int,Placeholder> $placeholderInstances */
$placeholderInstances = [];
$removedCharacters = 0;
$maximumOrdinalPosition = 1;
$maximumPositionByPlaceholder = 0;
$templateWithoutPlaceholders = $template;
foreach ($placeholders as $placeholder) {
assert(isset($placeholder[0]));
[$placeholderValue, $placeholderIndex] = $placeholder[0];
$placeholderLength = strlen($placeholderValue);
$templateWithoutPlaceholders = substr_replace(
$templateWithoutPlaceholders,
'',
$placeholderIndex - $removedCharacters,
$placeholderLength,
);
$removedCharacters += $placeholderLength;
$placeholderPosition = (int) ($placeholder['position'][0] ?? 0);
$maximumPositionByPlaceholder = max($maximumPositionByPlaceholder, $placeholderPosition);
if ($placeholderPosition === 0) {
$placeholderPosition = $maximumOrdinalPosition;
$maximumOrdinalPosition++;
}
Assert::positiveInteger($placeholderPosition);
assert($placeholderValue !== '');
$initialPlaceholderInstance = $placeholderInstances[$placeholderPosition] ?? null;
$placeholderInstance = Placeholder::create(
$placeholderValue,
$placeholderPosition,
$allowIntegerForStringPlaceholder,
$this->statementsSource,
);
if ($initialPlaceholderInstance !== null) {
$placeholderInstance = $initialPlaceholderInstance
->withRepeatedPlaceholder($placeholderInstance);
}
$placeholderInstances[$placeholderPosition] = $placeholderInstance;
}
$this->placeholders = $placeholderInstances;
$this->templateWithoutPlaceholder = $templateWithoutPlaceholders;
}
/** @psalm-param positive-int $phpVersion */
public static function fromArgument(
string $functionName,
Arg $templateArgument,
Context $context,
int $phpVersion,
bool $allowIntegerForStringPlaceholder,
StatementsSource $statementsSource
): self {
return new self(
$functionName,
ArgumentValueParser::create($templateArgument->value, $context, $statementsSource)->toString(),
$phpVersion,
$allowIntegerForStringPlaceholder,
$statementsSource,
);
}
public function getTemplate(): string
{
return $this->template;
}
public function getTemplateWithoutPlaceholder(): string
{
return $this->templateWithoutPlaceholder;
}
/**
* @psalm-return array<positive-int,Placeholder>
*/
public function getPlaceholders(): array
{
return $this->placeholders;
}
/**
* @return 0|positive-int
*/
public function getPlaceholderCount(): int
{
// TODO: normalize as %1$s is the same as %s, e.g.
return count($this->placeholders);
}
}