diff --git a/src/GenerateStubsCommand.php b/src/GenerateStubsCommand.php index cf09232..a3d1920 100644 --- a/src/GenerateStubsCommand.php +++ b/src/GenerateStubsCommand.php @@ -29,6 +29,7 @@ class GenerateStubsCommand extends Command ['classes', StubsGenerator::CLASSES], ['interfaces', StubsGenerator::INTERFACES], ['traits', StubsGenerator::TRAITS], + ['enums', StubsGenerator::ENUMS], ['documented-globals', StubsGenerator::DOCUMENTED_GLOBALS], ['undocumented-globals', StubsGenerator::UNDOCUMENTED_GLOBALS], ['globals', StubsGenerator::GLOBALS], @@ -82,7 +83,7 @@ protected function interact(InputInterface $input, OutputInterface $output): voi } $io = new SymfonyStyle($input, $output); - $message = "The file '{$this->outFile}' already exists. Overwrite?"; + $message = "The file '{$this->outFile}' already exists. Overwrite?"; if (!$input->getOption('force') && !$io->confirm($message, false)) { exit(1); } diff --git a/src/NodeVisitor.php b/src/NodeVisitor.php index 55a5fa6..61f8176 100644 --- a/src/NodeVisitor.php +++ b/src/NodeVisitor.php @@ -16,6 +16,7 @@ use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Const_; +use PhpParser\Node\Stmt\Enum_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\If_; @@ -47,6 +48,8 @@ class NodeVisitor extends NodeVisitorAbstract /** @var bool */ private $needsInterfaces; /** @var bool */ + private $needsEnums; + /** @var bool */ private $needsDocumentedGlobals; /** @var bool */ private $needsUndocumentedGlobals; @@ -90,6 +93,7 @@ class NodeVisitor extends NodeVisitorAbstract 'classes' => [], 'interfaces' => [], 'traits' => [], + 'enums' => [], 'constants' => [], 'globals' => [], ]; @@ -105,6 +109,7 @@ public function init(int $symbols = StubsGenerator::DEFAULT, array $config = []) $this->needsClasses = ($symbols & StubsGenerator::CLASSES) !== 0; $this->needsTraits = ($symbols & StubsGenerator::TRAITS) !== 0; $this->needsInterfaces = ($symbols & StubsGenerator::INTERFACES) !== 0; + $this->needsEnums = ($symbols & StubsGenerator::ENUMS) !== 0; $this->needsDocumentedGlobals = ($symbols & StubsGenerator::DOCUMENTED_GLOBALS) !== 0; $this->needsUndocumentedGlobals = ($symbols & StubsGenerator::UNDOCUMENTED_GLOBALS) !== 0; $this->needsConstants = ($symbols & StubsGenerator::CONSTANTS) !== 0; @@ -133,6 +138,7 @@ public function enterNode(Node $node) if (($this->needsClasses && $node instanceof Class_) || ($this->needsInterfaces && $node instanceof Interface_) || ($this->needsTraits && $node instanceof Trait_) + || ($this->needsEnums && $node instanceof Enum_) ) { // We'll need to parse all descendents of these nodes (if we plan to // 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) || $node instanceof Class_ || $node instanceof Interface_ || $node instanceof Trait_ + || $node instanceof Enum_ || $node instanceof Const_ || ( $node instanceof Expression && @@ -224,7 +231,7 @@ public function leaveNode(Node $node, bool $preserveStack = false) if ($node instanceof If_) { // Replace the if statement with its set of children, but only those // that we want. Have to manually call leaveNode on each; it won't - // be called automatically.. + // be called automatically. $stmts = []; foreach ($node->stmts as $stmt) { if ($this->leaveNode($stmt, true) !== NodeTraverser::REMOVE_NODE) { @@ -248,11 +255,14 @@ public function leaveNode(Node $node, bool $preserveStack = false) if ($parent && !($parent instanceof Namespace_)) { // Implies `$parent instanceof ClassLike`, which means $node is a - // either a method, property, or constant, or its part of the - // declaration itself (e.g., `extends`). + // either a method, property, or constant, or enum case, or its part + // of the declaration itself (e.g., `extends`). - if (!$this->includeInaccessibleClassNodes && $parent instanceof Class_ && ($node instanceof ClassMethod || $node instanceof ClassConst || $node instanceof Property)) { - if ($node->isPrivate() || ($parent->isFinal() && $node->isProtected())) { + if (!$this->includeInaccessibleClassNodes && ($parent instanceof Class_ || $parent instanceof Enum_) && ($node instanceof ClassMethod || $node instanceof ClassConst || $node instanceof Property)) { + if ($node->isPrivate() + || ($parent instanceof Class_ && $parent->isFinal() && $node->isProtected()) + || ($parent instanceof Enum_ && $node->isProtected()) + ) { return NodeTraverser::REMOVE_NODE; } } @@ -378,6 +388,12 @@ private function needsNode(Node $node, string $namespace): bool && !trait_exists($fullyQualifiedName); } + if ($node instanceof Enum_) { + return $this->needsEnums + && $this->count('enums', $fullyQualifiedName) + && !enum_exists($fullyQualifiedName); + } + if ($this->needsConstants) { if ($node instanceof Const_) { $node->consts = \array_filter( diff --git a/src/StubsGenerator.php b/src/StubsGenerator.php index 343ba81..8c3a153 100644 --- a/src/StubsGenerator.php +++ b/src/StubsGenerator.php @@ -73,6 +73,13 @@ class StubsGenerator */ public const CONSTANTS = 64; + /** + * Enum symbol type. + * + * @var int + */ + public const ENUMS = 128; + /** * The default set of symbol types. * @@ -85,7 +92,7 @@ class StubsGenerator * * @var int */ - public const ALL = self::FUNCTIONS | self::CLASSES | self::TRAITS | self::INTERFACES | self::GLOBALS | self::CONSTANTS; + public const ALL = self::FUNCTIONS | self::CLASSES | self::TRAITS | self::INTERFACES | self::ENUMS | self::GLOBALS | self::CONSTANTS; /** @var int */ private $symbols; diff --git a/test/NodeVisitorTest.php b/test/NodeVisitorTest.php index d5d11db..93f9f38 100644 --- a/test/NodeVisitorTest.php +++ b/test/NodeVisitorTest.php @@ -47,6 +47,9 @@ public function inputOutputProvider(): array 'junk', 'namespaces', 'constants', + 'enums', + ['enums', 'enums-include-inaccessible-class-nodes', null, ['include_inaccessible_class_nodes' => true]], + ['enums', 'enums-default-symbols', StubsGenerator::DEFAULT], ]; $baseDir = __DIR__ . '/files/'; diff --git a/test/files/enums-default-symbols.out.php b/test/files/enums-default-symbols.out.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/test/files/enums-default-symbols.out.php @@ -0,0 +1 @@ +