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
|
<?php
declare(strict_types=1);
namespace Shirabe\PluginClassifier;
use JetBrains\PHPStormStub\PhpStormStubsMap;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Parser;
use PhpParser\ParserFactory;
/**
* By-ref parameter positions of PHP builtin functions, resolved from
* jetbrains/phpstorm-stubs instead of a hand-maintained table. Stub files
* are parsed lazily, one file per first lookup of any function it defines;
* a function absent from the stubs map is unknown and the caller must be
* conservative.
*/
final class BuiltinSignatures
{
private Parser $parser;
/** @var array<string, string> lowercase function name => stub file relative path */
private array $functionFiles = [];
/** @var array<string, array{fixed: list<int>, variadicFrom: int|null}> lowercase function name => by-ref info */
private array $byRef = [];
/** @var array<string, true> */
private array $parsedFiles = [];
public function __construct()
{
$this->parser = (new ParserFactory())->createForNewestSupportedVersion();
foreach (PhpStormStubsMap::FUNCTIONS as $name => $file) {
$this->functionFiles[strtolower((string) $name)] = $file;
}
}
/**
* @return array{fixed: list<int>, variadicFrom: int|null}|null
* null when the function is not a known builtin
*/
public function byRefInfo(string $lowerName): ?array
{
if (isset($this->byRef[$lowerName])) {
return $this->byRef[$lowerName];
}
$file = $this->functionFiles[$lowerName] ?? null;
if ($file === null) {
return null;
}
$this->parseStubFile($file);
// Defined in the map but somehow absent from the parsed file:
// treat as unknown rather than silently non-by-ref.
return $this->byRef[$lowerName] ?? null;
}
private function parseStubFile(string $relativePath): void
{
if (isset($this->parsedFiles[$relativePath])) {
return;
}
$this->parsedFiles[$relativePath] = true;
$path = PhpStormStubsMap::DIR . '/' . $relativePath;
$code = file_get_contents($path);
if ($code === false) {
throw new \RuntimeException("cannot read stub file $path");
}
$stmts = $this->parser->parse($code);
if ($stmts === null) {
throw new \RuntimeException("cannot parse stub file $path");
}
$byRef = &$this->byRef;
$collector = new class($byRef) extends NodeVisitorAbstract {
/** @param array<string, array{fixed: list<int>, variadicFrom: int|null}> $byRef */
public function __construct(private array &$byRef)
{
}
public function enterNode(Node $node): null
{
if (!$node instanceof Node\Stmt\Function_) {
return null;
}
$name = strtolower($node->name->toString());
$fixed = [];
$variadicFrom = null;
foreach ($node->params as $i => $param) {
if (!$param->byRef) {
continue;
}
if ($param->variadic) {
$variadicFrom = $variadicFrom === null ? $i : min($variadicFrom, $i);
} else {
$fixed[] = $i;
}
}
// Stub files occasionally declare a function more than once
// (per-version signatures); merge conservatively.
if (isset($this->byRef[$name])) {
$fixed = array_values(array_unique(array_merge($this->byRef[$name]['fixed'], $fixed)));
sort($fixed);
$prev = $this->byRef[$name]['variadicFrom'];
$variadicFrom = $prev === null ? $variadicFrom : ($variadicFrom === null ? $prev : min($prev, $variadicFrom));
}
$this->byRef[$name] = ['fixed' => $fixed, 'variadicFrom' => $variadicFrom];
return null;
}
};
$traverser = new NodeTraverser();
$traverser->addVisitor($collector);
$traverser->traverse($stmts);
}
}
|