forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-function-metadata.php
executable file
·137 lines (118 loc) · 3.96 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env php
<?php declare(strict_types = 1);
use JetBrains\PhpStorm\Pure;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\NodeConnectingVisitor;
use PhpParser\NodeVisitorAbstract;
use PhpParser\ParserFactory;
use PHPStan\File\FileReader;
use PHPStan\File\FileWriter;
use PHPStan\ShouldNotHappenException;
(function (): void {
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 NodeVisitorAbstract {
/** @var string[] */
public array $functions = [];
/** @var string[] */
public array $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() === 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 ShouldNotHappenException($node->name->toString());
}
$className = $class->namespacedName->toString();
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === 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(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 ShouldNotHappenException($functionName);
}
}
$metadata[$functionName] = ['hasSideEffects' => false];
}
foreach ($visitor->methods as $methodName) {
if (array_key_exists($methodName, $metadata)) {
if ($metadata[$methodName]['hasSideEffects']) {
throw new ShouldNotHappenException($methodName);
}
}
$metadata[$methodName] = ['hasSideEffects' => false];
}
ksort($metadata);
$template = <<<'php'
<?php declare(strict_types = 1);
/**
* GENERATED FILE - DO NOT EDIT!
*
* This file is generated automatically when running bin/generate-function-metadata.php
* and the result is merged from bin/functionMetadata_original.php and by looking at jetbrains/phpstorm-stubs methods
* and functions with the #[Pure] attribute.
*
* If you want to add new entries here follow these steps:
* 1) verify on https://phpstan.org/try whether the entry you are going to add does not already work as expected.
* 2) Contribute the functions that have 'hasSideEffects' => true as a modification to bin/functionMetadata_original.php.
* 3) Contribute the #[Pure] functions without side effects to https://github.com/JetBrains/phpstorm-stubs
* 4) Once the PR from 3) is merged, please update the package here and run ./bin/generate-function-metadata.php.
*/
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),
);
}
FileWriter::write(__DIR__ . '/../resources/functionMetadata.php', sprintf($template, $content));
})();