1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Abstract builder package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2017 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\AbstractBuilder\Ast\Visitor; |
11
|
|
|
|
12
|
|
|
use PhpParser\Node; |
13
|
|
|
use PhpParser\NodeVisitorAbstract; |
14
|
|
|
use PhpParser\Node\Stmt; |
15
|
|
|
use RunOpenCode\AbstractBuilder\Exception\NotSupportedException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ClassIntrospectionVisitor |
19
|
|
|
* |
20
|
|
|
* @package RunOpenCode\AbstractBuilder\Ast\Visitor |
21
|
|
|
*/ |
22
|
|
|
class ClassIntrospectionVisitor extends NodeVisitorAbstract |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $namespace; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $class; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
private $final; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $abstract; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
* |
47
|
|
|
* Cleans up internal state |
48
|
|
|
* |
49
|
|
|
* @param array $nodes |
50
|
|
|
*/ |
51
|
|
|
public function beforeTraverse(array $nodes) |
52
|
|
|
{ |
53
|
|
|
$this->namespace = null; |
54
|
|
|
$this->class = null; |
55
|
|
|
$this->final = false; |
56
|
|
|
$this->abstract = false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
* |
62
|
|
|
* @throws \RunOpenCode\AbstractBuilder\Exception\NotSupportedException |
63
|
|
|
*/ |
64
|
|
|
public function enterNode(Node $node) |
65
|
|
|
{ |
66
|
|
|
if ($node instanceof Stmt\Namespace_) { |
67
|
|
|
|
68
|
|
|
if (null !== $this->namespace) { |
69
|
|
|
throw new NotSupportedException(sprintf('Multiple namespaces in single file are not supported.')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->namespace = $node->name->toString(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($node instanceof Stmt\Class_) { |
76
|
|
|
|
77
|
|
|
if (null !== $this->class) { |
78
|
|
|
throw new NotSupportedException(sprintf('Multiple namespaces in single file are not supported.')); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->class = $node->name; |
82
|
|
|
$this->final = $node->isFinal(); |
83
|
|
|
$this->abstract = $node->isAbstract(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($node instanceof Stmt\Property) { |
|
|
|
|
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getNamespace() |
95
|
|
|
{ |
96
|
|
|
return $this->namespace; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getClass() |
103
|
|
|
{ |
104
|
|
|
return $this->class; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function isFinal() |
111
|
|
|
{ |
112
|
|
|
return $this->final; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function isAbstract() |
119
|
|
|
{ |
120
|
|
|
return $this->abstract; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.