@@ -38,13 +38,10 @@ public function __construct(Request $request)
3838 }
3939 EOF;
4040
41- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
42- $ fp ->parse ($ code , 'path/to/class.php ' );
43-
44- $ violations = new Violations ();
41+ $ classDescriptions = $ this ->parseCode ($ code , 'path/to/class.php ' );
4542
4643 $ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['Foo ' ]);
47- $ dependsOnTheseNamespaces -> evaluate ( $ fp -> getClassDescriptions ()[ 0 ] , $ violations , 'because ' );
44+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ classDescriptions [ 0 ] , 'because ' );
4845
4946 self ::assertCount (2 , $ violations );
5047 self ::assertEquals ('path/to/class.php ' , $ violations ->get (0 )->getFilePath ());
@@ -69,9 +66,7 @@ public function bar($a, $b)
6966 }
7067 EOF;
7168
72- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
73- $ fp ->parse ($ code , 'relativePathName ' );
74- $ cd = $ fp ->getClassDescriptions ();
69+ $ cd = $ this ->parseCode ($ code );
7570
7671 self ::assertCount (1 , $ cd );
7772 self ::assertCount (1 , $ cd [0 ]->getDependencies ());
@@ -97,9 +92,7 @@ class Cat implements AnInterface
9792 }
9893 EOF;
9994
100- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
101- $ fp ->parse ($ code , 'relativePathName ' );
102- $ cd = $ fp ->getClassDescriptions ();
95+ $ cd = $ this ->parseCode ($ code );
10396
10497 self ::assertCount (2 , $ cd );
10598 self ::assertInstanceOf (ClassDescription::class, $ cd [0 ]);
@@ -141,9 +134,7 @@ class Cat implements AnInterface
141134 }
142135 EOF;
143136
144- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
145- $ fp ->parse ($ code , 'relativePathName ' );
146- $ cd = $ fp ->getClassDescriptions ();
137+ $ cd = $ this ->parseCode ($ code );
147138
148139 self ::assertCount (2 , $ cd );
149140 self ::assertInstanceOf (ClassDescription::class, $ cd [0 ]);
@@ -183,9 +174,7 @@ class Cat implements AnInterface
183174 }
184175 EOF;
185176
186- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
187- $ fp ->parse ($ code , 'relativePathName ' );
188- $ cd = $ fp ->getClassDescriptions ();
177+ $ cd = $ this ->parseCode ($ code );
189178
190179 self ::assertCount (2 , $ cd );
191180 self ::assertInstanceOf (ClassDescription::class, $ cd [0 ]);
@@ -217,10 +206,8 @@ class Cat extends Animal
217206 }
218207 EOF;
219208
220- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
221- $ fp ->parse ($ code , 'relativePathName ' );
222-
223- $ cd = $ fp ->getClassDescriptions ()[1 ];
209+ $ cd = $ this ->parseCode ($ code );
210+ $ cd = $ cd [1 ];
224211
225212 self ::assertEquals ('Root\Animals\Animal ' , $ cd ->getExtends ()[0 ]->toString ());
226213 }
@@ -245,10 +232,8 @@ public function methodWithAnonymous(): void
245232 }
246233 EOF;
247234
248- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
249- $ fp ->parse ($ code , 'relativePathName ' );
250-
251- $ cd = $ fp ->getClassDescriptions ()[1 ];
235+ $ cd = $ this ->parseCode ($ code );
236+ $ cd = $ cd [1 ];
252237
253238 self ::assertEquals ('Root\Animals\Animal ' , $ cd ->getExtends ()[0 ]->toString ());
254239 }
@@ -272,14 +257,10 @@ public function __construct(Request $request)
272257 }
273258 EOF;
274259
275- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
276- $ fp ->parse ($ code , 'relativePathName ' );
277- $ cd = $ fp ->getClassDescriptions ();
278-
279- $ violations = new Violations ();
260+ $ cd = $ this ->parseCode ($ code );
280261
281262 $ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['Foo ' , 'Symfony ' , 'Doctrine ' ]);
282- $ dependsOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
263+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ cd [0 ], 'we want to add this rule for our software ' );
283264
284265 self ::assertCount (0 , $ violations );
285266 }
@@ -308,9 +289,7 @@ public function __construct(Request $request, ?Nullable $nullable)
308289 }
309290 EOF;
310291
311- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
312- $ fp ->parse ($ code , 'relativePathName ' );
313- $ cd = $ fp ->getClassDescriptions ();
292+ $ cd = $ this ->parseCode ($ code );
314293
315294 $ expectedDependencies = [
316295 new ClassDependency ('Foo\Baz\Baz ' , 10 ),
@@ -340,15 +319,10 @@ public function __construct()
340319 }
341320 EOF;
342321
343- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
344- $ fp ->parse ($ code , 'relativePathName ' );
345-
346- $ cd = $ fp ->getClassDescriptions ();
347-
348- $ violations = new Violations ();
322+ $ cd = $ this ->parseCode ($ code );
349323
350324 $ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['Foo ' , 'Symfony ' , 'Doctrine ' ]);
351- $ dependsOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
325+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ cd [0 ], 'we want to add this rule for our software ' );
352326
353327 self ::assertCount (0 , $ violations );
354328 }
@@ -379,15 +353,10 @@ public function doSomething(self $self, static $static)
379353 }
380354 EOF;
381355
382- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
383- $ fp ->parse ($ code , 'relativePathName ' );
384-
385- $ cd = $ fp ->getClassDescriptions ();
386-
387- $ violations = new Violations ();
356+ $ cd = $ this ->parseCode ($ code );
388357
389358 $ notHaveDependencyOutsideNamespace = new NotHaveDependencyOutsideNamespace ('Root\Animals ' );
390- $ notHaveDependencyOutsideNamespace -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
359+ $ violations = $ this -> evaluateRule ( $ notHaveDependencyOutsideNamespace , $ cd [0 ], 'we want to add this rule for our software ' );
391360
392361 self ::assertCount (0 , $ violations );
393362 }
@@ -411,15 +380,10 @@ public function foo()
411380 }
412381 EOF;
413382
414- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
415- $ fp ->parse ($ code , 'relativePathName ' );
416-
417- $ cd = $ fp ->getClassDescriptions ();
418-
419- $ violations = new Violations ();
383+ $ cd = $ this ->parseCode ($ code );
420384
421385 $ dependsOnlyOnTheseNamespaces = new DependsOnlyOnTheseNamespaces ();
422- $ dependsOnlyOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
386+ $ violations = $ this -> evaluateRule ( $ dependsOnlyOnTheseNamespaces , $ cd [0 ], 'we want to add this rule for our software ' );
423387
424388 self ::assertCount (1 , $ violations );
425389 }
@@ -452,15 +416,10 @@ public function getStatic(): self
452416 }
453417 EOF;
454418
455- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
456- $ fp ->parse ($ code , 'relativePathName ' );
457-
458- $ cd = $ fp ->getClassDescriptions ();
459-
460- $ violations = new Violations ();
419+ $ cd = $ this ->parseCode ($ code );
461420
462421 $ notHaveDependencyOutsideNamespace = new NotHaveDependencyOutsideNamespace ('Root\Cars ' );
463- $ notHaveDependencyOutsideNamespace -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
422+ $ violations = $ this -> evaluateRule ( $ notHaveDependencyOutsideNamespace , $ cd [0 ], 'we want to add this rule for our software ' );
464423
465424 self ::assertCount (1 , $ violations );
466425 }
@@ -484,15 +443,11 @@ class Test implements Order
484443
485444 EOF;
486445
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 ();
446+ $ cd = $ this ->parseCode ($ code , 'relativePathName ' , TargetPhpVersion::PHP_8_1 );
447+ $ cd = $ cd [2 ]; // class Test
493448
494449 $ implement = new Implement ('Foo\Order ' );
495- $ implement -> evaluate ( $ cd , $ violations , 'we want to add this rule for our software ' );
450+ $ violations = $ this -> evaluateRule ( $ implement , $ cd , 'we want to add this rule for our software ' );
496451
497452 self ::assertCount (0 , $ violations );
498453 }
@@ -512,15 +467,10 @@ public function getBookList(): QueryBuilder;
512467 }
513468 EOF;
514469
515- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_1 );
516- $ fp ->parse ($ code , 'relativePathName ' );
517-
518- $ cd = $ fp ->getClassDescriptions ();
519-
520- $ violations = new Violations ();
470+ $ cd = $ this ->parseCode ($ code , 'relativePathName ' , TargetPhpVersion::PHP_8_1 );
521471
522472 $ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['MyProject\AppBundle\Application ' ]);
523- $ dependsOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
473+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ cd [0 ], 'we want to add this rule for our software ' );
524474
525475 self ::assertCount (1 , $ violations );
526476 }
@@ -549,10 +499,7 @@ public function foobar();
549499 }
550500 EOF;
551501
552- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_1 );
553- $ fp ->parse ($ code , 'relativePathName ' );
554-
555- $ cd = $ fp ->getClassDescriptions ();
502+ $ cd = $ this ->parseCode ($ code , 'relativePathName ' , TargetPhpVersion::PHP_8_1 );
556503
557504 self ::assertCount (3 , $ cd );
558505 self ::assertEquals ('MyProject\AppBundle\Application\FooAble ' , $ cd [2 ]->getExtends ()[0 ]->toString ());
@@ -583,14 +530,10 @@ public function getRequest(): Request //the violations is reported here
583530 }
584531 EOF;
585532
586- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
587- $ fp ->parse ($ code , 'relativePathName ' );
588- $ cd = $ fp ->getClassDescriptions ();
589-
590- $ violations = new Violations ();
533+ $ cd = $ this ->parseCode ($ code );
591534
592535 $ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['Foo ' , 'Symfony ' , 'Doctrine ' ]);
593- $ dependsOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
536+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ cd [0 ], 'we want to add this rule for our software ' );
594537
595538 self ::assertCount (0 , $ violations );
596539 }
@@ -608,13 +551,9 @@ public function __construct() {
608551 }
609552 EOF;
610553
611- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_4 );
612- $ fp ->parse ($ code , 'relativePathName ' );
613-
614- $ cd = $ fp ->getClassDescriptions ();
615- $ violations = new Violations ();
554+ $ cd = $ this ->parseCode ($ code , 'relativePathName ' , TargetPhpVersion::PHP_8_4 );
616555 $ isFinal = new IsFinal ();
617- $ isFinal -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
556+ $ violations = $ this -> evaluateRule ( $ isFinal , $ cd [0 ], 'we want to add this rule for our software ' );
618557
619558 self ::assertCount (0 , $ violations );
620559 }
@@ -634,13 +573,9 @@ abstract public function foo() {}
634573 }
635574 EOF;
636575
637- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_4 );
638- $ fp ->parse ($ code , 'relativePathName ' );
639-
640- $ cd = $ fp ->getClassDescriptions ();
641- $ violations = new Violations ();
576+ $ cd = $ this ->parseCode ($ code , 'relativePathName ' , TargetPhpVersion::PHP_8_4 );
642577 $ isAbstract = new IsAbstract ();
643- $ isAbstract -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
578+ $ violations = $ this -> evaluateRule ( $ isAbstract , $ cd [0 ], 'we want to add this rule for our software ' );
644579
645580 self ::assertCount (0 , $ violations );
646581 }
@@ -658,14 +593,26 @@ public function __construct() {
658593 }
659594 EOF;
660595
661- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_4 );
662- $ fp ->parse ($ code , 'relativePathName ' );
663-
664- $ cd = $ fp ->getClassDescriptions ();
665- $ violations = new Violations ();
596+ $ cd = $ this ->parseCode ($ code , 'relativePathName ' , TargetPhpVersion::PHP_8_4 );
666597 $ isReadOnly = new IsReadonly ();
667- $ isReadOnly -> evaluate ( $ cd [0 ], $ violations , 'we want to add this rule for our software ' );
598+ $ violations = $ this -> evaluateRule ( $ isReadOnly , $ cd [0 ], 'we want to add this rule for our software ' );
668599
669600 self ::assertCount (0 , $ violations );
670601 }
602+
603+ private function parseCode (string $ code , string $ filePath = 'relativePathName ' , ?string $ version = null ): array
604+ {
605+ $ fp = FileParserFactory::forPhpVersion ($ version ?? TargetPhpVersion::PHP_7_4 );
606+ $ fp ->parse ($ code , $ filePath );
607+
608+ return $ fp ->getClassDescriptions ();
609+ }
610+
611+ private function evaluateRule ($ rule , ClassDescription $ classDescription , string $ reason = 'test reason ' ): Violations
612+ {
613+ $ violations = new Violations ();
614+ $ rule ->evaluate ($ classDescription , $ violations , $ reason );
615+
616+ return $ violations ;
617+ }
671618}
0 commit comments