forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-function-metadata.php
executable file
·118 lines (100 loc) · 3.09 KB
/
generate-function-metadata.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
#!/usr/bin/env php
<?php declare(strict_types=1);
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\NodeConnectingVisitor;
use PhpParser\ParserFactory;
(function () {
require_once __DIR__ . '/../vendor/autoload.php';
$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
$finder = new Symfony\Component\Finder\Finder();
$finder->in(__DIR__ . '/../vendor/jetbrains/phpstorm-stubs')->files()->name('*.php');
$visitor = new class() extends \PhpParser\NodeVisitorAbstract {
/** @var string[] */
public $functions = [];
/** @var string[] */
public $methods = [];
public function enterNode(Node $node)
{
if ($node instanceof Node\Stmt\Function_) {
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === \JetBrains\PhpStorm\Pure::class) {
$this->functions[] = $node->namespacedName->toLowerString();
break 2;
}
}
}
}
if ($node instanceof Node\Stmt\ClassMethod) {
$class = $node->getAttribute('parent');
if (!$class instanceof Node\Stmt\ClassLike) {
throw new \PHPStan\ShouldNotHappenException($node->name->toString());
}
$className = $class->namespacedName->toString();
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === \JetBrains\PhpStorm\Pure::class) {
$this->methods[] = sprintf('%s::%s', $className, $node->name->toString());
break 2;
}
}
}
}
return null;
}
};
foreach ($finder as $stubFile) {
$path = $stubFile->getPathname();
$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
$traverser->addVisitor(new NodeConnectingVisitor());
$traverser->addVisitor($visitor);
$traverser->traverse(
$parser->parse(\PHPStan\File\FileReader::read($path))
);
}
$metadata = require __DIR__ . '/functionMetadata_original.php';
foreach ($visitor->functions as $functionName) {
if (array_key_exists($functionName, $metadata)) {
if ($metadata[$functionName]['hasSideEffects']) {
if (in_array($functionName, [
'mt_rand',
'rand',
'random_bytes',
'random_int',
], true)) {
continue;
}
throw new \PHPStan\ShouldNotHappenException($functionName);
}
}
$metadata[$functionName] = ['hasSideEffects' => false];
}
foreach ($visitor->methods as $methodName) {
if (array_key_exists($methodName, $metadata)) {
if ($metadata[$methodName]['hasSideEffects']) {
throw new \PHPStan\ShouldNotHappenException($methodName);
}
}
$metadata[$methodName] = ['hasSideEffects' => false];
}
ksort($metadata);
$template = <<<'php'
<?php declare(strict_types = 1);
return [
%s
];
php;
$content = '';
foreach ($metadata as $name => $meta) {
$content .= sprintf(
"\t%s => [%s => %s],\n",
var_export($name, true),
var_export('hasSideEffects', true),
var_export($meta['hasSideEffects'], true),
);
}
\PHPStan\File\FileWriter::write(__DIR__ . '/../resources/functionMetadata.php', sprintf($template, $content));
})();