Skip to content

Commit df76371

Browse files
committed
Add config flag to include inaccessible class nodes
1 parent 84bfe8c commit df76371

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/GenerateStubsCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function configure(): void
6262
->addOption('visitor', null, InputOption::VALUE_REQUIRED, 'Path to a PHP file which returns a `StubsGenerator\NodeVisitor` instance to replace the default node visitor.')
6363
->addOption('header', null, InputOption::VALUE_REQUIRED, 'A doc comment to prepend to the top of the generated stubs file. (Will be added below the opening `<?php` tag.)', '')
6464
->addOption('nullify-globals', null, InputOption::VALUE_NONE, 'Initialize all global variables with a value of `null`, instead of their assigned value.')
65+
->addOption('include-inaccessible-class-nodes', null, InputOption::VALUE_NONE, 'Include inaccessible class nodes like private members.')
6566
->addOption('stats', null, InputOption::VALUE_NONE, 'Whether to print stats instead of outputting stubs. Stats will always be printed if --out is provided.');
6667

6768
foreach (self::SYMBOL_OPTIONS as $opt) {
@@ -117,6 +118,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
117118
$finder = $this->parseSources($input);
118119
$generator = new StubsGenerator($this->parseSymbols($input), [
119120
'nullify_globals' => $input->getOption('nullify-globals'),
121+
'include_inaccessible_class_nodes' => $input->getOption('include-inaccessible-class-nodes')
120122
]);
121123

122124
$result = $generator->generate($finder, $visitor);

src/NodeVisitor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class NodeVisitor extends NodeVisitorAbstract
4949
private $needsConstants;
5050
/** @var bool */
5151
private $nullifyGlobals;
52+
/** @var bool */
53+
private $includeInaccessibleClassNodes;
5254

5355
/**
5456
* @psalm-suppress PropertyNotSetInConstructor
@@ -103,6 +105,7 @@ public function init(int $symbols = StubsGenerator::DEFAULT, array $config = [])
103105
$this->needsConstants = ($symbols & StubsGenerator::CONSTANTS) !== 0;
104106

105107
$this->nullifyGlobals = !empty($config['nullify_globals']);
108+
$this->includeInaccessibleClassNodes = ($config['include_inaccessible_class_nodes'] ?? false) === true;
106109

107110
$this->globalNamespace = new Namespace_();
108111
}
@@ -241,7 +244,7 @@ public function leaveNode(Node $node, bool $preserveStack = false)
241244
// either a method, property, or constant, or its part of the
242245
// declaration itself (e.g., `extends`).
243246

244-
if ($parent instanceof Class_ && ($node instanceof ClassMethod || $node instanceof ClassConst || $node instanceof Property)) {
247+
if (!$this->includeInaccessibleClassNodes && $parent instanceof Class_ && ($node instanceof ClassMethod || $node instanceof ClassConst || $node instanceof Property)) {
245248
if ($node->isPrivate() || ($parent->isFinal() && $node->isProtected())) {
246249
return NodeTraverser::REMOVE_NODE;
247250
}

0 commit comments

Comments
 (0)