forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-property-map.php
executable file
·197 lines (172 loc) · 6.33 KB
/
update-property-map.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
#!/usr/bin/env php
<?php
declare(strict_types=1);
// Original Idea for the code in here came from:
// https://github.com/phan/phan/blob/93c1c2/src/Phan/Language/Internal/PropertyMap.php#L49
// We differ however:
// 1. we parse the XML and extract original class and property names instead of the normalized identifiers.
// 2. We ignore non-parsable files.
//
// What we are currently missing is properly parsing of <xi:include> directives.
use PhpParser\Lexer\Emulative;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
set_error_handler(function ($num, $str, $file, $line, $context = null): void {
throw new ErrorException($str, 0, $num, $file, $line);
});
foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
if (file_exists($file)) {
require $file;
break;
}
}
$lexer = new Emulative();
$parser = (new ParserFactory)->create(
ParserFactory::PREFER_PHP7,
$lexer,
);
$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver);
function extractClassesFromStatements(array $statements): array
{
$classes = [];
foreach ($statements as $statement) {
if ($statement instanceof Class_) {
$classes[strtolower($statement->namespacedName->toString())] = true;
}
if ($statement instanceof Namespace_) {
$classes += extractClassesFromStatements($statement->stmts);
}
}
return $classes;
}
$stubbedClasses = [];
foreach (new RecursiveDirectoryIterator(
__DIR__ . '/../stubs',
FilesystemIterator::CURRENT_AS_PATHNAME|FilesystemIterator::SKIP_DOTS,
) as $file) {
if (is_dir($file)) {
continue;
}
$contents = file_get_contents($file);
$stmts = $parser->parse($contents);
$stmts = $traverser->traverse($stmts);
$stubbedClasses += extractClassesFromStatements($stmts);
}
unset($file, $contents, $stmts);
$docDir = realpath(__DIR__ . '/../build/doc-en');
if (false === $docDir) {
echo 'PHP doc not found!' . PHP_EOL;
echo 'Please execute: git clone [email protected]:php/doc-en.git ' . dirname(__DIR__) . '/build/doc-en';
}
$files = new RegexIterator(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$docDir,
FilesystemIterator::CURRENT_AS_PATHNAME|FilesystemIterator::SKIP_DOTS,
),
RecursiveIteratorIterator::LEAVES_ONLY,
),
'/.*.xml$/',
);
$classes = require_once dirname(__DIR__) . '/dictionaries/ManualPropertyMap.php';
libxml_use_internal_errors(true);
foreach ($files as $file) {
$contents = file_get_contents($file);
// FIXME: find a way to ignore custom entities, for now we strip them.
$contents = preg_replace('#&[a-zA-Z\d.\-_]+;#', '', $contents);
$contents = preg_replace('#%[a-zA-Z\d.\-_]+;#', '', $contents);
$contents = preg_replace('#<!ENTITY[^>]+>#', '', $contents);
try {
$simple = new SimpleXMLElement($contents);
} catch (Throwable $exception) {
// FIXME: we ignore files with XML errors at the moment because the input XML is not always sober.
// Examples are rpminfo/entities.functions.xml, wkhtmltox/wkhtmltox/bits/web.xml,
// wkhtmltox/wkhtmltox/bits/load.xml
echo sprintf(
"%1\$s: Ignoring %2\$s: %3\$s\n%4\$s",
$file,
get_class($exception),
$exception->getMessage(),
implode("\n", array_map(fn(LibXMLError $error): string => $error->message, libxml_get_errors())),
);
libxml_clear_errors();
continue;
}
$namespaces = $simple->getNamespaces();
$simple->registerXPathNamespace('docbook', 'http://docbook.org/ns/docbook');
foreach ($simple->xpath('//docbook:classsynopsis') as $classSynopsis) {
$classSynopsis->registerXPathNamespace('docbook', 'http://docbook.org/ns/docbook');
$class = strtolower((string) $classSynopsis->xpath('./docbook:ooclass/docbook:classname')[0]);
if (isset($stubbedClasses[$class])) {
continue;
}
foreach ($classSynopsis->xpath('//docbook:fieldsynopsis') as $item) {
assert($item instanceof SimpleXMLElement);
$property = strtolower((string) $item->varname);
if (isset($classes[$class][$property])) {
continue;
}
$type = $item->type[0];
if (null === $type) {
continue;
}
assert($type instanceof SimpleXMLElement);
$typeClass = $type->attributes(/*'http://docbook.org/ns/docbook'*/)->class;
if (null === $typeClass) {
$type = (string) $type;
} elseif ('union' === (string) $typeClass) {
$types = [];
foreach ($type as $subType) {
$types[] = (string) $subType;
}
$type = implode('|', $types);
}
switch ($type) {
case '':
// Some properties are not properly defined - we ignore them then.
continue 2;
// case 'integer':
// $type = 'int';
default:
}
$modifier = (string) $item->modifier;
// We do not want to handle constants... I guess?!
if ('const' === $modifier) {
continue;
}
$classes[$class][$property] = $type;
}
}
}
function serializeArray(array $array, string $prefix): string
{
uksort($array, fn(string $first, string $second): int => strtolower($first) <=> strtolower($second));
$result = "[\n";
$localPrefix = $prefix . ' ';
foreach ($array as $key => $value) {
$result .= $localPrefix . var_export((string) $key, true) . ' => ' .
(is_array($value)
? serializeArray($value, $localPrefix)
: var_export($value, true)) . ",\n";
}
$result .= $prefix . ']';
return $result;
}
$serialized = serializeArray($classes, '');
file_put_contents(
dirname(__DIR__) . '/dictionaries/PropertyMap.php',
<<<EOF
<?php
namespace Psalm\Internal;
/**
* Automatically created by bin/update-property-map.php
*
* Please do not modify - adapt the override constants in above file instead.
*/
return $serialized;
EOF,
);