Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/GenerateStubsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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);
}
Expand Down
26 changes: 21 additions & 5 deletions src/NodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -47,6 +48,8 @@ class NodeVisitor extends NodeVisitorAbstract
/** @var bool */
private $needsInterfaces;
/** @var bool */
private $needsEnums;
/** @var bool */
private $needsDocumentedGlobals;
/** @var bool */
private $needsUndocumentedGlobals;
Expand Down Expand Up @@ -90,6 +93,7 @@ class NodeVisitor extends NodeVisitorAbstract
'classes' => [],
'interfaces' => [],
'traits' => [],
'enums' => [],
'constants' => [],
'globals' => [],
];
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 &&
Expand All @@ -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) {
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 8 additions & 1 deletion src/StubsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions test/NodeVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Expand Down
1 change: 1 addition & 0 deletions test/files/enums-default-symbols.out.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
41 changes: 41 additions & 0 deletions test/files/enums-include-inaccessible-class-nodes.out.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/** doc */
enum A
{
case A;
case B;

public const C = 'C';
protected const D = 'D';
private const E = 'E';

/** doc */
public function a(): void
{
}

/** doc */
protected function b(): void
{
}

/** doc */
private function c(): void
{
}

/** doc */
public static function d(): self
{
}

/** doc */
protected static function e(): string
{
}

/** doc */
private static function e(): void
{
}
}
47 changes: 47 additions & 0 deletions test/files/enums.in.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/** doc */
enum A
{
case A;
case B;

public const C = 'C';
protected const D = 'D';
private const E = 'E';

/** doc */
public function a(): void
{
return;
}

/** doc */
protected function b(): void
{
return;
}

/** doc */
private function c(): void
{
return;
}

/** doc */
public static function d(): self
{
return self::A;
}

/** doc */
protected static function e(): string
{
return self::D;
}

/** doc */
private static function e(): void
{
return;
}
}
19 changes: 19 additions & 0 deletions test/files/enums.out.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/** doc */
enum A
{
case A;
case B;

public const C = 'C';

/** doc */
public function a(): void
{
}

/** doc */
public static function d(): self
{
}
}