Skip to content

Commit 84bfe8c

Browse files
committed
Ignore inaccessible class child nodes
1 parent dd405ec commit 84bfe8c

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/NodeVisitor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Scalar\String_;
1212
use PhpParser\Node\Stmt;
1313
use PhpParser\Node\Stmt\Class_;
14+
use PhpParser\Node\Stmt\ClassConst;
1415
use PhpParser\Node\Stmt\ClassLike;
1516
use PhpParser\Node\Stmt\ClassMethod;
1617
use PhpParser\Node\Stmt\Const_;
@@ -19,6 +20,7 @@
1920
use PhpParser\Node\Stmt\If_;
2021
use PhpParser\Node\Stmt\Interface_;
2122
use PhpParser\Node\Stmt\Namespace_;
23+
use PhpParser\Node\Stmt\Property;
2224
use PhpParser\Node\Stmt\Trait_;
2325
use PhpParser\NodeTraverser;
2426
use PhpParser\NodeVisitorAbstract;
@@ -238,6 +240,13 @@ public function leaveNode(Node $node, bool $preserveStack = false)
238240
// Implies `$parent instanceof ClassLike`, which means $node is a
239241
// either a method, property, or constant, or its part of the
240242
// declaration itself (e.g., `extends`).
243+
244+
if ($parent instanceof Class_ && ($node instanceof ClassMethod || $node instanceof ClassConst || $node instanceof Property)) {
245+
if ($node->isPrivate() || ($parent->isFinal() && $node->isProtected())) {
246+
return NodeTraverser::REMOVE_NODE;
247+
}
248+
}
249+
241250
return;
242251
}
243252

test/files/classes.in.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ abstract class A extends B implements C
55
/** doc */
66
protected const A = 'B';
77

8+
/** doc */
9+
private const B = 'C';
10+
811
/** doc */
912
public static $a = 'a';
1013

14+
private static $b = 'b';
15+
1116
/** doc */
1217
public static function b($a): void
1318
{
@@ -16,6 +21,21 @@ public static function b($a): void
1621

1722
/** doc */
1823
abstract public function c($a): string;
24+
25+
/** doc */
26+
protected function d($a): void
27+
{
28+
return;
29+
}
30+
31+
/** doc */
32+
abstract protected function e($a): string;
33+
34+
/** doc */
35+
private function f($a): void
36+
{
37+
return;
38+
}
1939
}
2040

2141
if (!class_exists('D')) {
@@ -27,3 +47,36 @@ public function a(string $a): string
2747
}
2848
}
2949
}
50+
51+
final class E
52+
{
53+
/** doc */
54+
public $a = 'a';
55+
56+
/** doc */
57+
protected $b = 'b';
58+
59+
/** doc */
60+
public function a($a): void
61+
{
62+
return;
63+
}
64+
65+
/** doc */
66+
protected function b($a): void
67+
{
68+
return;
69+
}
70+
}
71+
72+
trait F
73+
{
74+
/** doc */
75+
private $a = 'a';
76+
77+
/** doc */
78+
private function a($a): void
79+
{
80+
return;
81+
}
82+
}

test/files/classes.out.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public static function b($a): void
1515

1616
/** doc */
1717
abstract public function c($a): string;
18+
19+
/** doc */
20+
protected function d($a) : void
21+
{
22+
}
23+
24+
/** doc */
25+
protected abstract function e($a) : string;
1826
}
1927

2028
class D
@@ -23,3 +31,25 @@ public function a(string $a): string
2331
{
2432
}
2533
}
34+
35+
final class E
36+
{
37+
/** doc */
38+
public $a = 'a';
39+
40+
/** doc */
41+
public function a($a) : void
42+
{
43+
}
44+
}
45+
46+
trait F
47+
{
48+
/** doc */
49+
private $a = 'a';
50+
51+
/** doc */
52+
private function a($a) : void
53+
{
54+
}
55+
}

0 commit comments

Comments
 (0)