@@ -29,30 +29,43 @@ public function enterNode(Node $node): void
29
29
{
30
30
$ this ->handleClassNode ($ node );
31
31
32
+ // handles anonymous class definition like new class() {}
32
33
$ this ->handleAnonClassNode ($ node );
33
34
35
+ // handles enum definition
34
36
$ this ->handleEnumNode ($ node );
35
37
38
+ // handles interface definition like interface MyInterface {}
39
+ $ this ->handleInterfaceNode ($ node );
40
+
41
+ // handles trait definition like trait MyTrait {}
42
+ $ this ->handleTraitNode ($ node );
43
+
44
+ // handles code like $constantValue = StaticClass::constant;
36
45
$ this ->handleStaticClassConstantNode ($ node );
37
46
47
+ // handles code like $static = StaticClass::foo();
38
48
$ this ->handleStaticClassCallsNode ($ node );
39
49
50
+ // handles code lik $a instanceof MyClass
40
51
$ this ->handleInstanceOf ($ node );
41
52
53
+ // handles code like $a = new MyClass();
42
54
$ this ->handleNewExpression ($ node );
43
55
56
+ // handles code like public MyClass $myClass;
44
57
$ this ->handleTypedProperty ($ node );
45
58
59
+ // handles docblock like /** @var MyClass $myClass */
46
60
$ this ->handleDocComment ($ node );
47
61
62
+ // handles code like public function myMethod(MyClass $myClass) {}
48
63
$ this ->handleParamDependency ($ node );
49
64
50
- $ this ->handleInterfaceNode ($ node );
51
-
52
- $ this ->handleTraitNode ($ node );
53
-
65
+ // handles code like public function myMethod(): MyClass {}
54
66
$ this ->handleReturnTypeDependency ($ node );
55
67
68
+ // handles attribute definition like #[MyAttribute]
56
69
$ this ->handleAttributeNode ($ node );
57
70
}
58
71
@@ -145,117 +158,97 @@ private function handleAnonClassNode(Node $node): void
145
158
146
159
private function handleEnumNode (Node $ node ): void
147
160
{
148
- if ($ node instanceof Node \Stmt \Enum_ && null !== $ node -> namespacedName ) {
149
- $ this -> classDescriptionBuilder -> setClassName ( $ node -> namespacedName -> toCodeString ()) ;
150
- $ this -> classDescriptionBuilder -> setEnum ( true );
161
+ if (!( $ node instanceof Node \Stmt \Enum_) ) {
162
+ return ;
163
+ }
151
164
152
- foreach ($ node ->implements as $ interface ) {
153
- $ this ->classDescriptionBuilder
154
- ->addInterface ($ interface ->toString (), $ interface ->getLine ());
155
- }
165
+ if (null == $ node ->namespacedName ) {
166
+ return ;
167
+ }
168
+
169
+ $ this ->classDescriptionBuilder ->setClassName ($ node ->namespacedName ->toCodeString ());
170
+ $ this ->classDescriptionBuilder ->setEnum (true );
171
+
172
+ foreach ($ node ->implements as $ interface ) {
173
+ $ this ->classDescriptionBuilder
174
+ ->addInterface ($ interface ->toString (), $ interface ->getLine ());
156
175
}
157
176
}
158
177
159
178
private function handleStaticClassConstantNode (Node $ node ): void
160
179
{
161
- /**
162
- * adding static classes as dependencies
163
- * $constantValue = StaticClass::constant;.
164
- *
165
- * @see FileVisitorTest::test_it_should_return_errors_for_const_outside_namespace
166
- */
167
- if (
168
- $ node instanceof Node \Expr \ClassConstFetch
169
- && method_exists ($ node ->class , 'toString ' )
170
- ) {
171
- if ($ this ->isSelfOrStaticOrParent ($ node ->class ->toString ())) {
172
- return ;
173
- }
180
+ if (!($ node instanceof Node \Expr \ClassConstFetch)) {
181
+ return ;
182
+ }
174
183
175
- $ this -> classDescriptionBuilder
176
- -> addDependency ( new ClassDependency ( $ node -> class -> toString (), $ node -> getLine ())) ;
184
+ if (!( $ node -> class instanceof Node \ Name \FullyQualified)) {
185
+ return ;
177
186
}
187
+
188
+ $ this ->classDescriptionBuilder
189
+ ->addDependency (new ClassDependency ($ node ->class ->toString (), $ node ->getLine ()));
178
190
}
179
191
180
192
private function handleStaticClassCallsNode (Node $ node ): void
181
193
{
182
- /**
183
- * adding static function classes as dependencies
184
- * $static = StaticClass::foo();.
185
- *
186
- * @see FileVisitorTest::test_should_returns_all_dependencies
187
- */
188
- if (
189
- $ node instanceof Node \Expr \StaticCall
190
- && method_exists ($ node ->class , 'toString ' )
191
- ) {
192
- if ($ this ->isSelfOrStaticOrParent ($ node ->class ->toString ())) {
193
- return ;
194
- }
194
+ if (!($ node instanceof Node \Expr \StaticCall)) {
195
+ return ;
196
+ }
195
197
196
- $ this -> classDescriptionBuilder
197
- -> addDependency ( new ClassDependency ( $ node -> class -> toString (), $ node -> getLine ())) ;
198
+ if (!( $ node -> class instanceof Node \ Name \FullyQualified)) {
199
+ return ;
198
200
}
201
+
202
+ $ this ->classDescriptionBuilder
203
+ ->addDependency (new ClassDependency ($ node ->class ->toString (), $ node ->getLine ()));
199
204
}
200
205
201
206
private function handleInstanceOf (Node $ node ): void
202
207
{
203
- if (
204
- $ node instanceof Node \Expr \Instanceof_
205
- && method_exists ($ node ->class , 'toString ' )
206
- ) {
207
- if ($ this ->isSelfOrStaticOrParent ($ node ->class ->toString ())) {
208
- return ;
209
- }
210
- $ this ->classDescriptionBuilder
211
- ->addDependency (new ClassDependency ($ node ->class ->toString (), $ node ->getLine ()));
208
+ if (!($ node instanceof Node \Expr \Instanceof_)) {
209
+ return ;
210
+ }
211
+
212
+ if (!($ node ->class instanceof Node \Name \FullyQualified)) {
213
+ return ;
212
214
}
215
+
216
+ $ this ->classDescriptionBuilder
217
+ ->addDependency (new ClassDependency ($ node ->class ->toString (), $ node ->getLine ()));
213
218
}
214
219
215
220
private function handleNewExpression (Node $ node ): void
216
221
{
217
- if (
218
- $ node instanceof Node \Expr \New_
219
- && !($ node ->class instanceof Node \Expr \Variable)
220
- ) {
221
- if ((method_exists ($ node ->class , 'isAnonymous ' ) && true === $ node ->class ->isAnonymous ())
222
- || !method_exists ($ node ->class , 'toString ' )
223
- ) {
224
- return ;
225
- }
226
-
227
- if ($ this ->isSelfOrStaticOrParent ($ node ->class ->toString ())) {
228
- return ;
229
- }
222
+ if (!($ node instanceof Node \Expr \New_)) {
223
+ return ;
224
+ }
230
225
231
- $ this -> classDescriptionBuilder
232
- -> addDependency ( new ClassDependency ( $ node -> class -> toString (), $ node -> getLine ())) ;
226
+ if (!( $ node -> class instanceof Node \ Name \FullyQualified)) {
227
+ return ;
233
228
}
229
+
230
+ $ this ->classDescriptionBuilder
231
+ ->addDependency (new ClassDependency ($ node ->class ->toString (), $ node ->getLine ()));
234
232
}
235
233
236
234
private function handleTypedProperty (Node $ node ): void
237
235
{
238
- if ($ node instanceof Node \Stmt \Property) {
239
- if (null === $ node ->type ) {
240
- return ;
241
- }
242
-
243
- $ type = $ node ->type ;
244
- if ($ type instanceof NullableType) {
245
- $ type = $ type ->type ;
246
- }
247
-
248
- if (!method_exists ($ type , 'toString ' ) || $ this ->isBuiltInType ($ type ->toString ())) {
249
- return ;
250
- }
251
-
252
- try {
253
- $ this ->classDescriptionBuilder
254
- ->addDependency (new ClassDependency ($ type ->toString (), $ node ->getLine ()));
255
- } catch (\Exception $ e ) {
256
- // Silently ignore
257
- }
236
+ if (!($ node instanceof Node \Stmt \Property)) {
237
+ return ;
238
+ }
239
+
240
+ if (null === $ node ->type ) {
241
+ return ;
242
+ }
243
+
244
+ $ type = $ node ->type instanceof NullableType ? $ node ->type ->type : $ node ->type ;
245
+
246
+ if (!($ type instanceof Node \Name \FullyQualified)) {
247
+ return ;
258
248
}
249
+
250
+ $ this ->classDescriptionBuilder
251
+ ->addDependency (new ClassDependency ($ type ->toString (), $ node ->getLine ()));
259
252
}
260
253
261
254
private function handleDocComment (Node $ node ): void
@@ -278,92 +271,82 @@ private function handleParamDependency(Node $node): void
278
271
279
272
private function handleInterfaceNode (Node $ node ): void
280
273
{
281
- if ($ node instanceof Node \Stmt \Interface_) {
282
- if (null === $ node ->namespacedName ) {
283
- return ;
284
- }
274
+ if (!($ node instanceof Node \Stmt \Interface_)) {
275
+ return ;
276
+ }
285
277
286
- $ this ->classDescriptionBuilder ->setClassName ($ node ->namespacedName ->toCodeString ());
287
- $ this ->classDescriptionBuilder ->setInterface (true );
278
+ if (null === $ node ->namespacedName ) {
279
+ return ;
280
+ }
288
281
289
- foreach ($ node ->extends as $ interface ) {
290
- $ this ->classDescriptionBuilder
291
- ->addExtends ($ interface ->toString (), $ interface ->getLine ());
292
- }
282
+ $ this ->classDescriptionBuilder ->setClassName ($ node ->namespacedName ->toCodeString ());
283
+ $ this ->classDescriptionBuilder ->setInterface (true );
284
+
285
+ foreach ($ node ->extends as $ interface ) {
286
+ $ this ->classDescriptionBuilder
287
+ ->addExtends ($ interface ->toString (), $ interface ->getLine ());
293
288
}
294
289
}
295
290
296
291
private function handleTraitNode (Node $ node ): void
297
292
{
298
- if ($ node instanceof Node \Stmt \Trait_) {
299
- if (null === $ node ->namespacedName ) {
300
- return ;
301
- }
293
+ if (!($ node instanceof Node \Stmt \Trait_)) {
294
+ return ;
295
+ }
302
296
303
- $ this -> classDescriptionBuilder -> setClassName ( $ node ->namespacedName -> toCodeString ());
304
- $ this -> classDescriptionBuilder -> setTrait ( true ) ;
297
+ if ( null === $ node ->namespacedName ) {
298
+ return ;
305
299
}
300
+
301
+ $ this ->classDescriptionBuilder ->setClassName ($ node ->namespacedName ->toCodeString ());
302
+ $ this ->classDescriptionBuilder ->setTrait (true );
306
303
}
307
304
308
305
private function handleReturnTypeDependency (Node $ node ): void
309
306
{
310
- if ($ node instanceof Node \Stmt \ClassMethod) {
311
- $ returnType = $ node ->returnType ;
312
- if ($ returnType instanceof Node \Name \FullyQualified) {
313
- $ this ->classDescriptionBuilder
314
- ->addDependency (new ClassDependency ($ returnType ->toString (), $ returnType ->getLine ()));
315
- }
307
+ if (!($ node instanceof Node \Stmt \ClassMethod)) {
308
+ return ;
316
309
}
317
- }
318
310
319
- private function handleAttributeNode (Node $ node ): void
320
- {
321
- if ($ node instanceof Node \Attribute) {
322
- $ nodeName = $ node ->name ;
311
+ $ returnType = $ node ->returnType ;
323
312
324
- if ($ nodeName instanceof Node \Name \FullyQualified) {
325
- $ this ->classDescriptionBuilder
326
- ->addAttribute ($ node ->name ->toString (), $ node ->getLine ());
327
- }
313
+ if (!($ returnType instanceof Node \Name \FullyQualified)) {
314
+ return ;
328
315
}
329
- }
330
316
331
- private function isSelfOrStaticOrParent (string $ dependencyClass ): bool
332
- {
333
- return 'self ' === $ dependencyClass || 'static ' === $ dependencyClass || 'parent ' === $ dependencyClass ;
317
+ $ this ->classDescriptionBuilder
318
+ ->addDependency (new ClassDependency ($ returnType ->toString (), $ returnType ->getLine ()));
334
319
}
335
320
336
- private function addParamDependency (Node \ Param $ node ): void
321
+ private function handleAttributeNode (Node $ node ): void
337
322
{
338
- if (null === $ node-> type || $ node -> type instanceof Node \Identifier ) {
323
+ if (!( $ node instanceof Node \Attribute) ) {
339
324
return ;
340
325
}
341
326
342
- $ type = $ node ->type ;
343
- if ($ type instanceof NullableType) {
344
- /** @var NullableType * */
345
- $ nullableType = $ type ;
346
- $ type = $ nullableType ->type ;
347
- }
327
+ $ nodeName = $ node ->name ;
348
328
349
- if (method_exists ( $ type , ' isSpecialClassName ' ) && true === $ type -> isSpecialClassName ( )) {
329
+ if (!( $ nodeName instanceof Node \ Name \FullyQualified )) {
350
330
return ;
351
331
}
352
332
353
- if (!method_exists ($ type , 'toString ' )) {
333
+ $ this ->classDescriptionBuilder
334
+ ->addAttribute ($ node ->name ->toString (), $ node ->getLine ());
335
+ }
336
+
337
+ private function addParamDependency (Node \Param $ node ): void
338
+ {
339
+ if (null === $ node ->type || $ node ->type instanceof Node \Identifier) {
354
340
return ;
355
341
}
356
342
357
- if ($ this ->isBuiltInType ($ type ->toString ())) {
343
+ $ type = $ node ->type instanceof NullableType ? $ node ->type ->type : $ node ->type ;
344
+
345
+ if (!($ type instanceof Node \Name \FullyQualified)) {
358
346
return ;
359
347
}
360
348
361
349
$ this ->classDescriptionBuilder
362
350
->addDependency (new ClassDependency ($ type ->toString (), $ node ->getLine ()));
363
351
}
364
-
365
- private function isBuiltInType (string $ typeName ): bool
366
- {
367
- return \in_array ($ typeName , ['bool ' , 'int ' , 'float ' , 'string ' , 'array ' , 'resource ' , 'object ' , 'null ' ]);
368
- }
369
352
}
0 commit comments