Skip to content

Commit d3f15d3

Browse files
Splits FileVisitor::enterNode in smaller methods (#485)
1 parent d34dd6d commit d3f15d3

File tree

1 file changed

+127
-82
lines changed

1 file changed

+127
-82
lines changed

src/Analyzer/FileVisitor.php

Lines changed: 127 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Arkitect\Analyzer;
66

7-
use PhpParser\Comment\Doc;
87
use PhpParser\Node;
98
use PhpParser\Node\NullableType;
109
use PhpParser\NodeVisitorAbstract;
@@ -28,34 +27,97 @@ public function setFilePath(?string $filePath): void
2827

2928
public function enterNode(Node $node): void
3029
{
31-
if ($node instanceof Node\Stmt\Class_) {
32-
if (!$node->isAnonymous() && null !== $node->namespacedName) {
33-
$this->classDescriptionBuilder->setClassName($node->namespacedName->toCodeString());
34-
}
30+
$this->handleClassNode($node);
3531

36-
foreach ($node->implements as $interface) {
37-
$this->classDescriptionBuilder
38-
->addInterface($interface->toString(), $interface->getLine());
39-
}
32+
$this->handleEnumNode($node);
4033

41-
if (!$node->isAnonymous() && null !== $node->extends) {
42-
$this->classDescriptionBuilder
43-
->addExtends($node->extends->toString(), $node->getLine());
44-
}
34+
$this->handleStaticClassConstantNode($node);
4535

46-
if ($node->isFinal()) {
47-
$this->classDescriptionBuilder->setFinal(true);
48-
}
36+
$this->handleStaticClassCallsNode($node);
4937

50-
if ($node->isReadonly()) {
51-
$this->classDescriptionBuilder->setReadonly(true);
52-
}
38+
$this->handleInstanceOf($node);
5339

54-
if ($node->isAbstract()) {
55-
$this->classDescriptionBuilder->setAbstract(true);
56-
}
40+
$this->handleNewExpression($node);
41+
42+
$this->handleTypedProperty($node);
43+
44+
$this->handleDocComment($node);
45+
46+
$this->handleParamDependency($node);
47+
48+
$this->handleInterfaceNode($node);
49+
50+
$this->handleTraitNode($node);
51+
52+
$this->handleReturnTypeDependency($node);
53+
54+
$this->handleAttributeNode($node);
55+
}
56+
57+
public function getClassDescriptions(): array
58+
{
59+
return $this->classDescriptions;
60+
}
61+
62+
public function clearParsedClassDescriptions(): void
63+
{
64+
$this->classDescriptions = [];
65+
$this->classDescriptionBuilder->setFilePath(null);
66+
$this->classDescriptionBuilder->clear();
67+
}
68+
69+
public function leaveNode(Node $node): void
70+
{
71+
if ($node instanceof Node\Stmt\Class_ && !$node->isAnonymous()) {
72+
$this->classDescriptions[] = $this->classDescriptionBuilder->build();
73+
$this->classDescriptionBuilder->clear();
74+
}
75+
76+
if ($node instanceof Node\Stmt\Enum_) {
77+
$this->classDescriptions[] = $this->classDescriptionBuilder->build();
78+
$this->classDescriptionBuilder->clear();
79+
}
80+
81+
if ($node instanceof Node\Stmt\Interface_) {
82+
$this->classDescriptions[] = $this->classDescriptionBuilder->build();
83+
$this->classDescriptionBuilder->clear();
5784
}
5885

86+
if ($node instanceof Node\Stmt\Trait_) {
87+
$this->classDescriptions[] = $this->classDescriptionBuilder->build();
88+
$this->classDescriptionBuilder->clear();
89+
}
90+
}
91+
92+
private function handleClassNode(Node $node): void
93+
{
94+
if (!($node instanceof Node\Stmt\Class_)) {
95+
return;
96+
}
97+
98+
if (!$node->isAnonymous() && null !== $node->namespacedName) {
99+
$this->classDescriptionBuilder->setClassName($node->namespacedName->toCodeString());
100+
}
101+
102+
foreach ($node->implements as $interface) {
103+
$this->classDescriptionBuilder
104+
->addInterface($interface->toString(), $interface->getLine());
105+
}
106+
107+
if (!$node->isAnonymous() && null !== $node->extends) {
108+
$this->classDescriptionBuilder
109+
->addExtends($node->extends->toString(), $node->getLine());
110+
}
111+
112+
$this->classDescriptionBuilder->setFinal($node->isFinal());
113+
114+
$this->classDescriptionBuilder->setReadonly($node->isReadonly());
115+
116+
$this->classDescriptionBuilder->setAbstract($node->isAbstract());
117+
}
118+
119+
private function handleEnumNode(Node $node): void
120+
{
59121
if ($node instanceof Node\Stmt\Enum_ && null !== $node->namespacedName) {
60122
$this->classDescriptionBuilder->setClassName($node->namespacedName->toCodeString());
61123
$this->classDescriptionBuilder->setEnum(true);
@@ -65,7 +127,10 @@ public function enterNode(Node $node): void
65127
->addInterface($interface->toString(), $interface->getLine());
66128
}
67129
}
130+
}
68131

132+
private function handleStaticClassConstantNode(Node $node): void
133+
{
69134
/**
70135
* adding static classes as dependencies
71136
* $constantValue = StaticClass::constant;.
@@ -83,7 +148,10 @@ public function enterNode(Node $node): void
83148
$this->classDescriptionBuilder
84149
->addDependency(new ClassDependency($node->class->toString(), $node->getLine()));
85150
}
151+
}
86152

153+
private function handleStaticClassCallsNode(Node $node): void
154+
{
87155
/**
88156
* adding static function classes as dependencies
89157
* $static = StaticClass::foo();.
@@ -101,7 +169,10 @@ public function enterNode(Node $node): void
101169
$this->classDescriptionBuilder
102170
->addDependency(new ClassDependency($node->class->toString(), $node->getLine()));
103171
}
172+
}
104173

174+
private function handleInstanceOf(Node $node): void
175+
{
105176
if (
106177
$node instanceof Node\Expr\Instanceof_
107178
&& method_exists($node->class, 'toString')
@@ -112,7 +183,10 @@ public function enterNode(Node $node): void
112183
$this->classDescriptionBuilder
113184
->addDependency(new ClassDependency($node->class->toString(), $node->getLine()));
114185
}
186+
}
115187

188+
private function handleNewExpression(Node $node): void
189+
{
116190
if (
117191
$node instanceof Node\Expr\New_
118192
&& !($node->class instanceof Node\Expr\Variable)
@@ -130,56 +204,53 @@ public function enterNode(Node $node): void
130204
$this->classDescriptionBuilder
131205
->addDependency(new ClassDependency($node->class->toString(), $node->getLine()));
132206
}
207+
}
133208

134-
/**
135-
* matches parameters dependency in property definitions like
136-
* public ?NotBlank $foo;.
137-
*
138-
* @see FileVisitorTest::test_it_parse_typed_property
139-
*/
209+
private function handleTypedProperty(Node $node): void
210+
{
140211
if ($node instanceof Node\Stmt\Property) {
141212
if (null === $node->type) {
142213
return;
143214
}
144215

145216
$type = $node->type;
146217
if ($type instanceof NullableType) {
147-
/** @var NullableType * */
148-
$nullableType = $type;
149-
$type = $nullableType->type;
218+
$type = $type->type;
150219
}
151220

152-
if (!method_exists($type, 'toString')) {
153-
return;
154-
}
155-
156-
if ($this->isBuiltInType($type->toString())) {
221+
if (!method_exists($type, 'toString') || $this->isBuiltInType($type->toString())) {
157222
return;
158223
}
159224

160225
try {
161-
$this->classDescriptionBuilder->addDependency(new ClassDependency($type->toString(), $node->getLine()));
226+
$this->classDescriptionBuilder
227+
->addDependency(new ClassDependency($type->toString(), $node->getLine()));
162228
} catch (\Exception $e) {
229+
// Silently ignore
163230
}
164231
}
232+
}
165233

166-
if (null !== $node->getDocComment()) {
167-
/** @var Doc $docComment */
168-
$docComment = $node->getDocComment();
234+
private function handleDocComment(Node $node): void
235+
{
236+
$docComment = $node->getDocComment();
169237

170-
$this->classDescriptionBuilder->addDocBlock($docComment->getText());
238+
if (null === $docComment) {
239+
return;
171240
}
172241

173-
/**
174-
* matches parameters dependency in functions and method definitions like
175-
* public function __construct(Symfony\Component\HttpFoundation\Request $request).
176-
*
177-
* @see FileVisitorTest::test_should_returns_all_dependencies
178-
*/
242+
$this->classDescriptionBuilder->addDocBlock($docComment->getText());
243+
}
244+
245+
private function handleParamDependency(Node $node): void
246+
{
179247
if ($node instanceof Node\Param) {
180248
$this->addParamDependency($node);
181249
}
250+
}
182251

252+
private function handleInterfaceNode(Node $node): void
253+
{
183254
if ($node instanceof Node\Stmt\Interface_) {
184255
if (null === $node->namespacedName) {
185256
return;
@@ -193,7 +264,10 @@ public function enterNode(Node $node): void
193264
->addExtends($interface->toString(), $interface->getLine());
194265
}
195266
}
267+
}
196268

269+
private function handleTraitNode(Node $node): void
270+
{
197271
if ($node instanceof Node\Stmt\Trait_) {
198272
if (null === $node->namespacedName) {
199273
return;
@@ -202,15 +276,21 @@ public function enterNode(Node $node): void
202276
$this->classDescriptionBuilder->setClassName($node->namespacedName->toCodeString());
203277
$this->classDescriptionBuilder->setTrait(true);
204278
}
279+
}
205280

281+
private function handleReturnTypeDependency(Node $node): void
282+
{
206283
if ($node instanceof Node\Stmt\ClassMethod) {
207284
$returnType = $node->returnType;
208285
if ($returnType instanceof Node\Name\FullyQualified) {
209286
$this->classDescriptionBuilder
210287
->addDependency(new ClassDependency($returnType->toString(), $returnType->getLine()));
211288
}
212289
}
290+
}
213291

292+
private function handleAttributeNode(Node $node): void
293+
{
214294
if ($node instanceof Node\Attribute) {
215295
$nodeName = $node->name;
216296

@@ -221,41 +301,6 @@ public function enterNode(Node $node): void
221301
}
222302
}
223303

224-
public function getClassDescriptions(): array
225-
{
226-
return $this->classDescriptions;
227-
}
228-
229-
public function clearParsedClassDescriptions(): void
230-
{
231-
$this->classDescriptions = [];
232-
$this->classDescriptionBuilder->setFilePath(null);
233-
$this->classDescriptionBuilder->clear();
234-
}
235-
236-
public function leaveNode(Node $node): void
237-
{
238-
if ($node instanceof Node\Stmt\Class_ && !$node->isAnonymous()) {
239-
$this->classDescriptions[] = $this->classDescriptionBuilder->build();
240-
$this->classDescriptionBuilder->clear();
241-
}
242-
243-
if ($node instanceof Node\Stmt\Enum_) {
244-
$this->classDescriptions[] = $this->classDescriptionBuilder->build();
245-
$this->classDescriptionBuilder->clear();
246-
}
247-
248-
if ($node instanceof Node\Stmt\Interface_) {
249-
$this->classDescriptions[] = $this->classDescriptionBuilder->build();
250-
$this->classDescriptionBuilder->clear();
251-
}
252-
253-
if ($node instanceof Node\Stmt\Trait_) {
254-
$this->classDescriptions[] = $this->classDescriptionBuilder->build();
255-
$this->classDescriptionBuilder->clear();
256-
}
257-
}
258-
259304
private function isSelfOrStaticOrParent(string $dependencyClass): bool
260305
{
261306
return 'self' === $dependencyClass || 'static' === $dependencyClass || 'parent' === $dependencyClass;

0 commit comments

Comments
 (0)