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
|
<?php
declare(strict_types=1);
namespace Shirabe\PluginClassifier;
/**
* Extracts class-like type names from phpdoc @param / @return / @var /
* @throws tags and resolves them against the file's namespace and use map.
*
* This is deliberately not a full phpdoc type grammar. Composer's docblocks
* are PHPStan-checked, so tokenizing the type expression and keeping the
* class-like tokens is sufficient. Tokens are considered class-like only
* when they contain a namespace separator or start with an uppercase letter;
* this filters array-shape keys and scalar keywords. Resolved names that do
* not exist in the symbol table or a known vendor namespace are reported by
* the caller rather than silently dropped.
*/
final class DocblockTypeExtractor
{
private const KEYWORDS = [
'int', 'integer', 'float', 'double', 'string', 'bool', 'boolean',
'true', 'false', 'null', 'void', 'never', 'mixed', 'scalar', 'array',
'iterable', 'object', 'callable', 'resource', 'self', 'static',
'parent', 'this', 'list', 'non-empty-list', 'non-empty-array',
'non-empty-string', 'class-string', 'callable-string',
'numeric-string', 'lowercase-string', 'literal-string', 'key-of',
'value-of', 'array-key', 'positive-int', 'negative-int',
'non-negative-int', 'non-positive-int', 'int-mask-of', 'Closure',
'Generator', 'Traversable', 'Iterator', 'IteratorAggregate',
'ArrayAccess', 'Countable', 'Stringable', 'JsonSerializable',
'Throwable', 'Exception', 'SplFileInfo', 'ArrayObject',
];
/**
* Names declared by @template / @phpstan-type / @phpstan-import-type
* anywhere in the tree; they look like class names inside type
* expressions but are not classes. Filled by a pre-scan.
*
* @var array<string, true>
*/
public array $aliasNames = [];
public function collectAliases(string $code): void
{
if (preg_match_all(
'/@(?:phpstan-|psalm-)?(?:template(?:-covariant|-contravariant)?|type|import-type)\s+([A-Za-z_][A-Za-z0-9_]*)/',
$code,
$m,
) > 0) {
foreach ($m[1] as $name) {
$this->aliasNames[$name] = true;
}
}
}
/**
* @param array<string, string> $useMap lowercase alias => FQCN
* @return array{params: array<string, list<string>>, return: list<string>, var: list<string>, throws: list<string>}
*/
public function extract(?string $docblock, string $namespace, array $useMap): array
{
$result = ['params' => [], 'return' => [], 'var' => [], 'throws' => []];
if ($docblock === null) {
return $result;
}
$pattern = '/@(param|return|var|throws|phpstan-param|phpstan-return|phpstan-var)[ \t]+(.+)$/m';
if (preg_match_all($pattern, $docblock, $matches, PREG_SET_ORDER) === false) {
return $result;
}
foreach ($matches as $m) {
$tag = str_replace('phpstan-', '', $m[1]);
$rest = rtrim($m[2]);
// The type expression ends at the first whitespace at bracket
// depth zero; spaces inside array{...} / array<...> shapes are
// part of the type. Whatever follows is the variable name
// (for @param) and/or a free-text description.
$typeExpr = $this->cutAtToplevelSpace($rest);
$after = substr($rest, strlen($typeExpr));
$paramName = null;
if (preg_match('/^\s*\$(\w+)/', $after, $nm) === 1) {
$paramName = $nm[1];
}
$types = $this->classLikeTokens($typeExpr, $namespace, $useMap);
if ($types === []) {
continue;
}
switch ($tag) {
case 'param':
if ($paramName !== null) {
$result['params'][$paramName] = array_values(array_unique(array_merge(
$result['params'][$paramName] ?? [],
$types,
)));
}
break;
case 'return':
$result['return'] = array_values(array_unique(array_merge($result['return'], $types)));
break;
case 'var':
$result['var'] = array_values(array_unique(array_merge($result['var'], $types)));
break;
case 'throws':
$result['throws'] = array_values(array_unique(array_merge($result['throws'], $types)));
break;
}
}
return $result;
}
private function cutAtToplevelSpace(string $expr): string
{
$depth = 0;
$len = strlen($expr);
for ($i = 0; $i < $len; $i++) {
$c = $expr[$i];
if ($c === '<' || $c === '{' || $c === '(' || $c === '[') {
$depth++;
} elseif ($c === '>' || $c === '}' || $c === ')' || $c === ']') {
$depth--;
} elseif (($c === ' ' || $c === "\t") && $depth === 0) {
return substr($expr, 0, $i);
}
}
return $expr;
}
/**
* @param array<string, string> $useMap
* @return list<string>
*/
private function classLikeTokens(string $typeExpr, string $namespace, array $useMap): array
{
// Constant references (self::STABILITY_*, BasePackage::STABILITIES,
// PATHINFO_EXTENSION|...) are not class names: drop everything after
// `::`, then drop all-caps tokens.
$typeExpr = preg_replace('/::[A-Za-z0-9_*]*/', '', $typeExpr) ?? $typeExpr;
if (preg_match_all('/\\\\?[A-Za-z_][A-Za-z0-9_]*(?:\\\\[A-Za-z_][A-Za-z0-9_]*)*/', $typeExpr, $m) === false) {
return [];
}
$out = [];
foreach ($m[0] as $token) {
$isQualified = str_contains($token, '\\');
if (!$isQualified) {
if (in_array($token, self::KEYWORDS, true) || in_array(strtolower($token), self::KEYWORDS, true)) {
continue;
}
// Array-shape keys and phpdoc keywords are lowercase;
// Composer class names are StudlyCaps.
if (!ctype_upper($token[0])) {
continue;
}
// All-caps tokens are constants, not classes.
if (preg_match('/[a-z]/', $token) !== 1) {
continue;
}
// Generic parameters and phpstan type aliases.
if (isset($this->aliasNames[$token])) {
continue;
}
}
$out[] = $this->resolve($token, $namespace, $useMap);
}
return array_values(array_unique($out));
}
/** @param array<string, string> $useMap */
private function resolve(string $name, string $namespace, array $useMap): string
{
if (str_starts_with($name, '\\')) {
return ltrim($name, '\\');
}
$parts = explode('\\', $name);
$firstLower = strtolower($parts[0]);
if (isset($useMap[$firstLower])) {
$parts[0] = $useMap[$firstLower];
return implode('\\', $parts);
}
if ($namespace === '') {
return $name;
}
return $namespace . '\\' . $name;
}
}
|