|
11 | 11 | use PhpParser\Node\Scalar\String_; |
12 | 12 | use PhpParser\Node\Stmt; |
13 | 13 | use PhpParser\Node\Stmt\Class_; |
| 14 | +use PhpParser\Node\Stmt\ClassConst; |
14 | 15 | use PhpParser\Node\Stmt\ClassLike; |
15 | 16 | use PhpParser\Node\Stmt\ClassMethod; |
16 | 17 | use PhpParser\Node\Stmt\Const_; |
|
19 | 20 | use PhpParser\Node\Stmt\If_; |
20 | 21 | use PhpParser\Node\Stmt\Interface_; |
21 | 22 | use PhpParser\Node\Stmt\Namespace_; |
| 23 | +use PhpParser\Node\Stmt\Property; |
22 | 24 | use PhpParser\Node\Stmt\Trait_; |
23 | 25 | use PhpParser\NodeTraverser; |
24 | 26 | use PhpParser\NodeVisitorAbstract; |
@@ -47,6 +49,8 @@ class NodeVisitor extends NodeVisitorAbstract |
47 | 49 | private $needsConstants; |
48 | 50 | /** @var bool */ |
49 | 51 | private $nullifyGlobals; |
| 52 | + /** @var bool */ |
| 53 | + private $includeInaccessibleClassNodes; |
50 | 54 |
|
51 | 55 | /** |
52 | 56 | * @psalm-suppress PropertyNotSetInConstructor |
@@ -101,6 +105,7 @@ public function init(int $symbols = StubsGenerator::DEFAULT, array $config = []) |
101 | 105 | $this->needsConstants = ($symbols & StubsGenerator::CONSTANTS) !== 0; |
102 | 106 |
|
103 | 107 | $this->nullifyGlobals = !empty($config['nullify_globals']); |
| 108 | + $this->includeInaccessibleClassNodes = ($config['include_inaccessible_class_nodes'] ?? false) === true; |
104 | 109 |
|
105 | 110 | $this->globalNamespace = new Namespace_(); |
106 | 111 | } |
@@ -238,6 +243,13 @@ public function leaveNode(Node $node, bool $preserveStack = false) |
238 | 243 | // Implies `$parent instanceof ClassLike`, which means $node is a |
239 | 244 | // either a method, property, or constant, or its part of the |
240 | 245 | // declaration itself (e.g., `extends`). |
| 246 | + |
| 247 | + if (!$this->includeInaccessibleClassNodes && $parent instanceof Class_ && ($node instanceof ClassMethod || $node instanceof ClassConst || $node instanceof Property)) { |
| 248 | + if ($node->isPrivate() || ($parent->isFinal() && $node->isProtected())) { |
| 249 | + return NodeTraverser::REMOVE_NODE; |
| 250 | + } |
| 251 | + } |
| 252 | + |
241 | 253 | return; |
242 | 254 | } |
243 | 255 |
|
|
0 commit comments