1616use PhpParser \Node \Stmt \ClassLike ;
1717use PhpParser \Node \Stmt \ClassMethod ;
1818use PhpParser \Node \Stmt \Const_ ;
19+ use PhpParser \Node \Stmt \Enum_ ;
1920use PhpParser \Node \Stmt \Expression ;
2021use PhpParser \Node \Stmt \Function_ ;
2122use PhpParser \Node \Stmt \If_ ;
@@ -47,6 +48,8 @@ class NodeVisitor extends NodeVisitorAbstract
4748 /** @var bool */
4849 private $ needsInterfaces ;
4950 /** @var bool */
51+ private $ needsEnums ;
52+ /** @var bool */
5053 private $ needsDocumentedGlobals ;
5154 /** @var bool */
5255 private $ needsUndocumentedGlobals ;
@@ -90,6 +93,7 @@ class NodeVisitor extends NodeVisitorAbstract
9093 'classes ' => [],
9194 'interfaces ' => [],
9295 'traits ' => [],
96+ 'enums ' => [],
9397 'constants ' => [],
9498 'globals ' => [],
9599 ];
@@ -105,6 +109,7 @@ public function init(int $symbols = StubsGenerator::DEFAULT, array $config = [])
105109 $ this ->needsClasses = ($ symbols & StubsGenerator::CLASSES ) !== 0 ;
106110 $ this ->needsTraits = ($ symbols & StubsGenerator::TRAITS ) !== 0 ;
107111 $ this ->needsInterfaces = ($ symbols & StubsGenerator::INTERFACES ) !== 0 ;
112+ $ this ->needsEnums = ($ symbols & StubsGenerator::ENUMS ) !== 0 ;
108113 $ this ->needsDocumentedGlobals = ($ symbols & StubsGenerator::DOCUMENTED_GLOBALS ) !== 0 ;
109114 $ this ->needsUndocumentedGlobals = ($ symbols & StubsGenerator::UNDOCUMENTED_GLOBALS ) !== 0 ;
110115 $ this ->needsConstants = ($ symbols & StubsGenerator::CONSTANTS ) !== 0 ;
@@ -133,6 +138,7 @@ public function enterNode(Node $node)
133138 if (($ this ->needsClasses && $ node instanceof Class_)
134139 || ($ this ->needsInterfaces && $ node instanceof Interface_)
135140 || ($ this ->needsTraits && $ node instanceof Trait_)
141+ || ($ this ->needsEnums && $ node instanceof Enum_)
136142 ) {
137143 // We'll need to parse all descendents of these nodes (if we plan to
138144 // include them in the stubs at all) so we get method, property, and
@@ -209,6 +215,7 @@ public function leaveNode(Node $node, bool $preserveStack = false)
209215 || $ node instanceof Class_
210216 || $ node instanceof Interface_
211217 || $ node instanceof Trait_
218+ || $ node instanceof Enum_
212219 || $ node instanceof Const_
213220 || (
214221 $ node instanceof Expression &&
@@ -224,7 +231,7 @@ public function leaveNode(Node $node, bool $preserveStack = false)
224231 if ($ node instanceof If_) {
225232 // Replace the if statement with its set of children, but only those
226233 // that we want. Have to manually call leaveNode on each; it won't
227- // be called automatically..
234+ // be called automatically.
228235 $ stmts = [];
229236 foreach ($ node ->stmts as $ stmt ) {
230237 if ($ this ->leaveNode ($ stmt , true ) !== NodeTraverser::REMOVE_NODE ) {
@@ -248,11 +255,14 @@ public function leaveNode(Node $node, bool $preserveStack = false)
248255
249256 if ($ parent && !($ parent instanceof Namespace_)) {
250257 // Implies `$parent instanceof ClassLike`, which means $node is a
251- // either a method, property, or constant, or its part of the
252- // declaration itself (e.g., `extends`).
258+ // either a method, property, or constant, or enum case, or its part
259+ // of the declaration itself (e.g., `extends`).
253260
254- if (!$ this ->includeInaccessibleClassNodes && $ parent instanceof Class_ && ($ node instanceof ClassMethod || $ node instanceof ClassConst || $ node instanceof Property)) {
255- if ($ node ->isPrivate () || ($ parent ->isFinal () && $ node ->isProtected ())) {
261+ if (!$ this ->includeInaccessibleClassNodes && ($ parent instanceof Class_ || $ parent instanceof Enum_) && ($ node instanceof ClassMethod || $ node instanceof ClassConst || $ node instanceof Property)) {
262+ if ($ node ->isPrivate ()
263+ || ($ parent instanceof Class_ && $ parent ->isFinal () && $ node ->isProtected ())
264+ || ($ parent instanceof Enum_ && $ node ->isProtected ())
265+ ) {
256266 return NodeTraverser::REMOVE_NODE ;
257267 }
258268 }
@@ -378,6 +388,12 @@ private function needsNode(Node $node, string $namespace): bool
378388 && !trait_exists ($ fullyQualifiedName );
379389 }
380390
391+ if ($ node instanceof Enum_) {
392+ return $ this ->needsEnums
393+ && $ this ->count ('enums ' , $ fullyQualifiedName )
394+ && !enum_exists ($ fullyQualifiedName );
395+ }
396+
381397 if ($ this ->needsConstants ) {
382398 if ($ node instanceof Const_) {
383399 $ node ->consts = \array_filter (
0 commit comments