Skip to content

Commit a4ef990

Browse files
wip
1 parent 9d64c5b commit a4ef990

File tree

1 file changed

+103
-74
lines changed

1 file changed

+103
-74
lines changed

src/Analyzer/FileVisitor.php

Lines changed: 103 additions & 74 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;
@@ -26,7 +25,71 @@ public function setFilePath(?string $filePath): void
2625
$this->classDescriptionBuilder->setFilePath($filePath);
2726
}
2827

29-
public function parseClassNode(Node $node): void
28+
public function enterNode(Node $node): void
29+
{
30+
$this->handleClassNode($node);
31+
32+
$this->handleEnumNode($node);
33+
34+
$this->handleStaticClassConstantNode($node);
35+
36+
$this->handleStaticClassCallsNode($node);
37+
38+
$this->handleInstanceOf($node);
39+
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();
84+
}
85+
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
3093
{
3194
if (!($node instanceof Node\Stmt\Class_)) {
3295
return;
@@ -53,7 +116,7 @@ public function parseClassNode(Node $node): void
53116
$this->classDescriptionBuilder->setAbstract($node->isAbstract());
54117
}
55118

56-
public function parseEnumNode(Node $node): void
119+
private function handleEnumNode(Node $node): void
57120
{
58121
if ($node instanceof Node\Stmt\Enum_ && null !== $node->namespacedName) {
59122
$this->classDescriptionBuilder->setClassName($node->namespacedName->toCodeString());
@@ -66,7 +129,7 @@ public function parseEnumNode(Node $node): void
66129
}
67130
}
68131

69-
public function parseStaticClassConstantNode(Node $node): void
132+
private function handleStaticClassConstantNode(Node $node): void
70133
{
71134
/**
72135
* adding static classes as dependencies
@@ -87,7 +150,7 @@ public function parseStaticClassConstantNode(Node $node): void
87150
}
88151
}
89152

90-
public function parseStaticClassCallsNode(Node $node): void
153+
private function handleStaticClassCallsNode(Node $node): void
91154
{
92155
/**
93156
* adding static function classes as dependencies
@@ -108,16 +171,8 @@ public function parseStaticClassCallsNode(Node $node): void
108171
}
109172
}
110173

111-
public function enterNode(Node $node): void
174+
private function handleInstanceOf(Node $node): void
112175
{
113-
$this->parseClassNode($node);
114-
115-
$this->parseEnumNode($node);
116-
117-
$this->parseStaticClassConstantNode($node);
118-
119-
$this->parseStaticClassCallsNode($node);
120-
121176
if (
122177
$node instanceof Node\Expr\Instanceof_
123178
&& method_exists($node->class, 'toString')
@@ -128,7 +183,10 @@ public function enterNode(Node $node): void
128183
$this->classDescriptionBuilder
129184
->addDependency(new ClassDependency($node->class->toString(), $node->getLine()));
130185
}
186+
}
131187

188+
private function handleNewExpression(Node $node): void
189+
{
132190
if (
133191
$node instanceof Node\Expr\New_
134192
&& !($node->class instanceof Node\Expr\Variable)
@@ -146,56 +204,53 @@ public function enterNode(Node $node): void
146204
$this->classDescriptionBuilder
147205
->addDependency(new ClassDependency($node->class->toString(), $node->getLine()));
148206
}
207+
}
149208

150-
/**
151-
* matches parameters dependency in property definitions like
152-
* public ?NotBlank $foo;.
153-
*
154-
* @see FileVisitorTest::test_it_parse_typed_property
155-
*/
209+
private function handleTypedProperty(Node $node): void
210+
{
156211
if ($node instanceof Node\Stmt\Property) {
157212
if (null === $node->type) {
158213
return;
159214
}
160215

161216
$type = $node->type;
162217
if ($type instanceof NullableType) {
163-
/** @var NullableType * */
164-
$nullableType = $type;
165-
$type = $nullableType->type;
166-
}
167-
168-
if (!method_exists($type, 'toString')) {
169-
return;
218+
$type = $type->type;
170219
}
171220

172-
if ($this->isBuiltInType($type->toString())) {
221+
if (!method_exists($type, 'toString') || $this->isBuiltInType($type->toString())) {
173222
return;
174223
}
175224

176225
try {
177-
$this->classDescriptionBuilder->addDependency(new ClassDependency($type->toString(), $node->getLine()));
226+
$this->classDescriptionBuilder
227+
->addDependency(new ClassDependency($type->toString(), $node->getLine()));
178228
} catch (\Exception $e) {
229+
// Silently ignore
179230
}
180231
}
232+
}
181233

182-
if (null !== $node->getDocComment()) {
183-
/** @var Doc $docComment */
184-
$docComment = $node->getDocComment();
234+
private function handleDocComment(Node $node): void
235+
{
236+
$docComment = $node->getDocComment();
185237

186-
$this->classDescriptionBuilder->addDocBlock($docComment->getText());
238+
if (null === $docComment) {
239+
return;
187240
}
188241

189-
/**
190-
* matches parameters dependency in functions and method definitions like
191-
* public function __construct(Symfony\Component\HttpFoundation\Request $request).
192-
*
193-
* @see FileVisitorTest::test_should_returns_all_dependencies
194-
*/
242+
$this->classDescriptionBuilder->addDocBlock($docComment->getText());
243+
}
244+
245+
private function handleParamDependency(Node $node): void
246+
{
195247
if ($node instanceof Node\Param) {
196248
$this->addParamDependency($node);
197249
}
250+
}
198251

252+
private function handleInterfaceNode(Node $node): void
253+
{
199254
if ($node instanceof Node\Stmt\Interface_) {
200255
if (null === $node->namespacedName) {
201256
return;
@@ -209,7 +264,10 @@ public function enterNode(Node $node): void
209264
->addExtends($interface->toString(), $interface->getLine());
210265
}
211266
}
267+
}
212268

269+
private function handleTraitNode(Node $node): void
270+
{
213271
if ($node instanceof Node\Stmt\Trait_) {
214272
if (null === $node->namespacedName) {
215273
return;
@@ -218,15 +276,21 @@ public function enterNode(Node $node): void
218276
$this->classDescriptionBuilder->setClassName($node->namespacedName->toCodeString());
219277
$this->classDescriptionBuilder->setTrait(true);
220278
}
279+
}
221280

281+
private function handleReturnTypeDependency(Node $node): void
282+
{
222283
if ($node instanceof Node\Stmt\ClassMethod) {
223284
$returnType = $node->returnType;
224285
if ($returnType instanceof Node\Name\FullyQualified) {
225286
$this->classDescriptionBuilder
226287
->addDependency(new ClassDependency($returnType->toString(), $returnType->getLine()));
227288
}
228289
}
290+
}
229291

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

@@ -237,41 +301,6 @@ public function enterNode(Node $node): void
237301
}
238302
}
239303

240-
public function getClassDescriptions(): array
241-
{
242-
return $this->classDescriptions;
243-
}
244-
245-
public function clearParsedClassDescriptions(): void
246-
{
247-
$this->classDescriptions = [];
248-
$this->classDescriptionBuilder->setFilePath(null);
249-
$this->classDescriptionBuilder->clear();
250-
}
251-
252-
public function leaveNode(Node $node): void
253-
{
254-
if ($node instanceof Node\Stmt\Class_ && !$node->isAnonymous()) {
255-
$this->classDescriptions[] = $this->classDescriptionBuilder->build();
256-
$this->classDescriptionBuilder->clear();
257-
}
258-
259-
if ($node instanceof Node\Stmt\Enum_) {
260-
$this->classDescriptions[] = $this->classDescriptionBuilder->build();
261-
$this->classDescriptionBuilder->clear();
262-
}
263-
264-
if ($node instanceof Node\Stmt\Interface_) {
265-
$this->classDescriptions[] = $this->classDescriptionBuilder->build();
266-
$this->classDescriptionBuilder->clear();
267-
}
268-
269-
if ($node instanceof Node\Stmt\Trait_) {
270-
$this->classDescriptions[] = $this->classDescriptionBuilder->build();
271-
$this->classDescriptionBuilder->clear();
272-
}
273-
}
274-
275304
private function isSelfOrStaticOrParent(string $dependencyClass): bool
276305
{
277306
return 'self' === $dependencyClass || 'static' === $dependencyClass || 'parent' === $dependencyClass;

0 commit comments

Comments
 (0)