19
19
20
20
class CanParseClassTest extends TestCase
21
21
{
22
+ private function parseCode (string $ code , string $ filePath = 'relativePathName ' , ?string $ version = null ): array
23
+ {
24
+ $ fp = FileParserFactory::forPhpVersion ($ version ?? TargetPhpVersion::PHP_7_4 );
25
+ $ fp ->parse ($ code , $ filePath );
26
+ return $ fp ->getClassDescriptions ();
27
+ }
28
+
29
+ private function evaluateRule ($ rule , ClassDescription $ classDescription , string $ reason = 'test reason ' ): Violations
30
+ {
31
+ $ violations = new Violations ();
32
+ $ rule ->evaluate ($ classDescription , $ violations , $ reason );
33
+ return $ violations ;
34
+ }
22
35
public function test_violation_should_have_ref_to_filepath (): void
23
36
{
24
37
$ code = <<< 'EOF'
@@ -38,13 +51,10 @@ public function __construct(Request $request)
38
51
}
39
52
EOF;
40
53
41
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
42
- $ fp ->parse ($ code , 'path/to/class.php ' );
43
-
44
- $ violations = new Violations ();
45
-
54
+ $ classDescriptions = $ this ->parseCode ($ code , 'path/to/class.php ' );
55
+
46
56
$ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['Foo ' ]);
47
- $ dependsOnTheseNamespaces -> evaluate ( $ fp -> getClassDescriptions ()[ 0 ] , $ violations , 'because ' );
57
+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ classDescriptions [ 0 ] , 'because ' );
48
58
49
59
self ::assertCount (2 , $ violations );
50
60
self ::assertEquals ('path/to/class.php ' , $ violations ->get (0 )->getFilePath ());
@@ -69,9 +79,7 @@ public function bar($a, $b)
69
79
}
70
80
EOF;
71
81
72
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
73
- $ fp ->parse ($ code , 'relativePathName ' );
74
- $ cd = $ fp ->getClassDescriptions ();
82
+ $ cd = $ this ->parseCode ($ code );
75
83
76
84
self ::assertCount (1 , $ cd );
77
85
self ::assertCount (1 , $ cd [0 ]->getDependencies ());
@@ -97,9 +105,7 @@ class Cat implements AnInterface
97
105
}
98
106
EOF;
99
107
100
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
101
- $ fp ->parse ($ code , 'relativePathName ' );
102
- $ cd = $ fp ->getClassDescriptions ();
108
+ $ cd = $ this ->parseCode ($ code );
103
109
104
110
self ::assertCount (2 , $ cd );
105
111
self ::assertInstanceOf (ClassDescription::class, $ cd [0 ]);
@@ -141,9 +147,7 @@ class Cat implements AnInterface
141
147
}
142
148
EOF;
143
149
144
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
145
- $ fp ->parse ($ code , 'relativePathName ' );
146
- $ cd = $ fp ->getClassDescriptions ();
150
+ $ cd = $ this ->parseCode ($ code );
147
151
148
152
self ::assertCount (2 , $ cd );
149
153
self ::assertInstanceOf (ClassDescription::class, $ cd [0 ]);
@@ -183,9 +187,7 @@ class Cat implements AnInterface
183
187
}
184
188
EOF;
185
189
186
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
187
- $ fp ->parse ($ code , 'relativePathName ' );
188
- $ cd = $ fp ->getClassDescriptions ();
190
+ $ cd = $ this ->parseCode ($ code );
189
191
190
192
self ::assertCount (2 , $ cd );
191
193
self ::assertInstanceOf (ClassDescription::class, $ cd [0 ]);
@@ -217,10 +219,8 @@ class Cat extends Animal
217
219
}
218
220
EOF;
219
221
220
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
221
- $ fp ->parse ($ code , 'relativePathName ' );
222
-
223
- $ cd = $ fp ->getClassDescriptions ()[1 ];
222
+ $ cd = $ this ->parseCode ($ code );
223
+ $ cd = $ cd [1 ];
224
224
225
225
self ::assertEquals ('Root\Animals\Animal ' , $ cd ->getExtends ()[0 ]->toString ());
226
226
}
@@ -245,10 +245,8 @@ public function methodWithAnonymous(): void
245
245
}
246
246
EOF;
247
247
248
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
249
- $ fp ->parse ($ code , 'relativePathName ' );
250
-
251
- $ cd = $ fp ->getClassDescriptions ()[1 ];
248
+ $ cd = $ this ->parseCode ($ code );
249
+ $ cd = $ cd [1 ];
252
250
253
251
self ::assertEquals ('Root\Animals\Animal ' , $ cd ->getExtends ()[0 ]->toString ());
254
252
}
@@ -272,14 +270,10 @@ public function __construct(Request $request)
272
270
}
273
271
EOF;
274
272
275
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
276
- $ fp ->parse ($ code , 'relativePathName ' );
277
- $ cd = $ fp ->getClassDescriptions ();
278
-
279
- $ violations = new Violations ();
273
+ $ cd = $ this ->parseCode ($ code );
280
274
281
275
$ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['Foo ' , 'Symfony ' , 'Doctrine ' ]);
282
- $ dependsOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
276
+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ cd [0 ], 'we want to add this rule for our software ' );
283
277
284
278
self ::assertCount (0 , $ violations );
285
279
}
@@ -308,9 +302,7 @@ public function __construct(Request $request, ?Nullable $nullable)
308
302
}
309
303
EOF;
310
304
311
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
312
- $ fp ->parse ($ code , 'relativePathName ' );
313
- $ cd = $ fp ->getClassDescriptions ();
305
+ $ cd = $ this ->parseCode ($ code );
314
306
315
307
$ expectedDependencies = [
316
308
new ClassDependency ('Foo\Baz\Baz ' , 10 ),
@@ -340,15 +332,10 @@ public function __construct()
340
332
}
341
333
EOF;
342
334
343
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
344
- $ fp ->parse ($ code , 'relativePathName ' );
345
-
346
- $ cd = $ fp ->getClassDescriptions ();
347
-
348
- $ violations = new Violations ();
335
+ $ cd = $ this ->parseCode ($ code );
349
336
350
337
$ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['Foo ' , 'Symfony ' , 'Doctrine ' ]);
351
- $ dependsOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
338
+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ cd [0 ], 'we want to add this rule for our software ' );
352
339
353
340
self ::assertCount (0 , $ violations );
354
341
}
@@ -379,15 +366,10 @@ public function doSomething(self $self, static $static)
379
366
}
380
367
EOF;
381
368
382
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
383
- $ fp ->parse ($ code , 'relativePathName ' );
384
-
385
- $ cd = $ fp ->getClassDescriptions ();
386
-
387
- $ violations = new Violations ();
369
+ $ cd = $ this ->parseCode ($ code );
388
370
389
371
$ notHaveDependencyOutsideNamespace = new NotHaveDependencyOutsideNamespace ('Root\Animals ' );
390
- $ notHaveDependencyOutsideNamespace -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
372
+ $ violations = $ this -> evaluateRule ( $ notHaveDependencyOutsideNamespace , $ cd [0 ], 'we want to add this rule for our software ' );
391
373
392
374
self ::assertCount (0 , $ violations );
393
375
}
@@ -411,15 +393,10 @@ public function foo()
411
393
}
412
394
EOF;
413
395
414
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
415
- $ fp ->parse ($ code , 'relativePathName ' );
416
-
417
- $ cd = $ fp ->getClassDescriptions ();
418
-
419
- $ violations = new Violations ();
396
+ $ cd = $ this ->parseCode ($ code );
420
397
421
398
$ dependsOnlyOnTheseNamespaces = new DependsOnlyOnTheseNamespaces ();
422
- $ dependsOnlyOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
399
+ $ violations = $ this -> evaluateRule ( $ dependsOnlyOnTheseNamespaces , $ cd [0 ], 'we want to add this rule for our software ' );
423
400
424
401
self ::assertCount (1 , $ violations );
425
402
}
@@ -452,15 +429,10 @@ public function getStatic(): self
452
429
}
453
430
EOF;
454
431
455
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
456
- $ fp ->parse ($ code , 'relativePathName ' );
457
-
458
- $ cd = $ fp ->getClassDescriptions ();
459
-
460
- $ violations = new Violations ();
432
+ $ cd = $ this ->parseCode ($ code );
461
433
462
434
$ notHaveDependencyOutsideNamespace = new NotHaveDependencyOutsideNamespace ('Root\Cars ' );
463
- $ notHaveDependencyOutsideNamespace -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
435
+ $ violations = $ this -> evaluateRule ( $ notHaveDependencyOutsideNamespace , $ cd [0 ], 'we want to add this rule for our software ' );
464
436
465
437
self ::assertCount (1 , $ violations );
466
438
}
@@ -484,15 +456,11 @@ class Test implements Order
484
456
485
457
EOF;
486
458
487
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_1 );
488
- $ fp ->parse ($ code , 'relativePathName ' );
489
-
490
- $ cd = $ fp ->getClassDescriptions ()[2 ]; // class Test
491
-
492
- $ violations = new Violations ();
459
+ $ cd = $ this ->parseCode ($ code , 'relativePathName ' , TargetPhpVersion::PHP_8_1 );
460
+ $ cd = $ cd [2 ]; // class Test
493
461
494
462
$ implement = new Implement ('Foo\Order ' );
495
- $ implement -> evaluate ( $ cd , $ violations , 'we want to add this rule for our software ' );
463
+ $ violations = $ this -> evaluateRule ( $ implement , $ cd , 'we want to add this rule for our software ' );
496
464
497
465
self ::assertCount (0 , $ violations );
498
466
}
@@ -512,15 +480,10 @@ public function getBookList(): QueryBuilder;
512
480
}
513
481
EOF;
514
482
515
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_1 );
516
- $ fp ->parse ($ code , 'relativePathName ' );
517
-
518
- $ cd = $ fp ->getClassDescriptions ();
519
-
520
- $ violations = new Violations ();
483
+ $ cd = $ this ->parseCode ($ code , 'relativePathName ' , TargetPhpVersion::PHP_8_1 );
521
484
522
485
$ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['MyProject\AppBundle\Application ' ]);
523
- $ dependsOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
486
+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ cd [0 ], 'we want to add this rule for our software ' );
524
487
525
488
self ::assertCount (1 , $ violations );
526
489
}
@@ -549,10 +512,7 @@ public function foobar();
549
512
}
550
513
EOF;
551
514
552
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_1 );
553
- $ fp ->parse ($ code , 'relativePathName ' );
554
-
555
- $ cd = $ fp ->getClassDescriptions ();
515
+ $ cd = $ this ->parseCode ($ code , 'relativePathName ' , TargetPhpVersion::PHP_8_1 );
556
516
557
517
self ::assertCount (3 , $ cd );
558
518
self ::assertEquals ('MyProject\AppBundle\Application\FooAble ' , $ cd [2 ]->getExtends ()[0 ]->toString ());
@@ -583,14 +543,10 @@ public function getRequest(): Request //the violations is reported here
583
543
}
584
544
EOF;
585
545
586
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
587
- $ fp ->parse ($ code , 'relativePathName ' );
588
- $ cd = $ fp ->getClassDescriptions ();
589
-
590
- $ violations = new Violations ();
546
+ $ cd = $ this ->parseCode ($ code );
591
547
592
548
$ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['Foo ' , 'Symfony ' , 'Doctrine ' ]);
593
- $ dependsOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
549
+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ cd [0 ], 'we want to add this rule for our software ' );
594
550
595
551
self ::assertCount (0 , $ violations );
596
552
}
@@ -608,13 +564,9 @@ public function __construct() {
608
564
}
609
565
EOF;
610
566
611
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_4 );
612
- $ fp ->parse ($ code , 'relativePathName ' );
613
-
614
- $ cd = $ fp ->getClassDescriptions ();
615
- $ violations = new Violations ();
567
+ $ cd = $ this ->parseCode ($ code , 'relativePathName ' , TargetPhpVersion::PHP_8_4 );
616
568
$ isFinal = new IsFinal ();
617
- $ isFinal -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
569
+ $ violations = $ this -> evaluateRule ( $ isFinal , $ cd [0 ], 'we want to add this rule for our software ' );
618
570
619
571
self ::assertCount (0 , $ violations );
620
572
}
@@ -634,13 +586,9 @@ abstract public function foo() {}
634
586
}
635
587
EOF;
636
588
637
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_4 );
638
- $ fp ->parse ($ code , 'relativePathName ' );
639
-
640
- $ cd = $ fp ->getClassDescriptions ();
641
- $ violations = new Violations ();
589
+ $ cd = $ this ->parseCode ($ code , 'relativePathName ' , TargetPhpVersion::PHP_8_4 );
642
590
$ isAbstract = new IsAbstract ();
643
- $ isAbstract -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
591
+ $ violations = $ this -> evaluateRule ( $ isAbstract , $ cd [0 ], 'we want to add this rule for our software ' );
644
592
645
593
self ::assertCount (0 , $ violations );
646
594
}
@@ -658,13 +606,9 @@ public function __construct() {
658
606
}
659
607
EOF;
660
608
661
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_4 );
662
- $ fp ->parse ($ code , 'relativePathName ' );
663
-
664
- $ cd = $ fp ->getClassDescriptions ();
665
- $ violations = new Violations ();
609
+ $ cd = $ this ->parseCode ($ code , 'relativePathName ' , TargetPhpVersion::PHP_8_4 );
666
610
$ isReadOnly = new IsReadonly ();
667
- $ isReadOnly -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
611
+ $ violations = $ this -> evaluateRule ( $ isReadOnly , $ cd [0 ], 'we want to add this rule for our software ' );
668
612
669
613
self ::assertCount (0 , $ violations );
670
614
}
0 commit comments