-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
StubsGenerator.php
143 lines (123 loc) · 3.39 KB
/
StubsGenerator.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
<?php
namespace StubsGenerator;
use PhpParser\Error;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
use RuntimeException;
use Symfony\Component\Finder\Finder;
/**
* Given a collection of PHP files, this class extracts function, class,
* interface, trait, and variable declarations, allowing them to be operated on
* or pretty-printed.
*/
class StubsGenerator
{
/**
* Function symbol type.
*
* @var int
*/
public const FUNCTIONS = 1;
/**
* Class symbol type.
*
* @var int
*/
public const CLASSES = 2;
/**
* Trait symbol type.
*
* @var int
*/
public const TRAITS = 4;
/**
* Interface symbol type.
*
* @var int
*/
public const INTERFACES = 8;
/**
* Global variable symbol type; will only include global variables with a
* doc comment.
*
* @var int
*/
public const DOCUMENTED_GLOBALS = 16;
/**
* Global variable symbol type; will only include global variables without a
* doc comment.
*
* @var int
*/
public const UNDOCUMENTED_GLOBALS = 32;
/**
* Shortcut to include both documented and undocumented global variables.
*
* @var int
*/
public const GLOBALS = self::DOCUMENTED_GLOBALS | self::UNDOCUMENTED_GLOBALS;
/**
* Constant symbol type.
*
* @var int
*/
public const CONSTANTS = 64;
/**
* The default set of symbol types.
*
* @var int
*/
public const DEFAULT = self::FUNCTIONS | self::CLASSES | self::TRAITS | self::INTERFACES | self::DOCUMENTED_GLOBALS | self::CONSTANTS;
/**
* Shortcut to include every symbol type.
*
* @var int
*/
public const ALL = self::FUNCTIONS | self::CLASSES | self::TRAITS | self::INTERFACES | self::GLOBALS | self::CONSTANTS;
/** @var int */
private $symbols;
/** @var array */
private $config;
/**
* @param int $symbols Bitmask of symbol types to include in the stubs.
*/
public function __construct(int $symbols = self::DEFAULT, array $config = [])
{
$this->symbols = $symbols;
$this->config = $config;
}
/**
* Iterates through all the files found by the `$finder` and returns
* pretty-printed stubs.
*
* @param Finder $finder The set of files to generate (merged) stubs for.
* @param NodeVisitor $visitor The optional node visitor to override the default.
*
* @return Result
*/
public function generate(Finder $finder, NodeVisitor $visitor = null): Result
{
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
if (!($visitor instanceof NodeVisitor)) {
$visitor = new NodeVisitor;
}
$visitor->init($this->symbols, $this->config);
$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
$traverser->addVisitor($visitor);
$unparsed = [];
foreach ($finder as $file) {
$stmts = null;
try {
$stmts = $parser->parse($file->getContents());
} catch (Error|RuntimeException $e) {
$unparsed[$file->getPathname()] = $e;
}
if ($stmts) {
$traverser->traverse($stmts);
}
}
return new Result($visitor, $unparsed);
}
}