Skip to content

Commit 921341f

Browse files
fain182claude
andcommitted
refactor: reduce duplication in FileParser test cases
Extract common test patterns into helper methods to eliminate repetitive boilerplate code: - parseCode(): combines FileParserFactory creation, parsing, and getting descriptions - evaluateRule(): combines Violations creation and rule evaluation This reduces 56 lines of duplicated code while maintaining test coverage and readability. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent bfeca55 commit 921341f

File tree

1 file changed

+49
-105
lines changed

1 file changed

+49
-105
lines changed

tests/Unit/Analyzer/FileParser/CanParseClassTest.php

Lines changed: 49 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@
1919

2020
class CanParseClassTest extends TestCase
2121
{
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+
}
2235
public function test_violation_should_have_ref_to_filepath(): void
2336
{
2437
$code = <<< 'EOF'
@@ -38,13 +51,10 @@ public function __construct(Request $request)
3851
}
3952
EOF;
4053

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+
4656
$dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces(['Foo']);
47-
$dependsOnTheseNamespaces->evaluate($fp->getClassDescriptions()[0], $violations, 'because');
57+
$violations = $this->evaluateRule($dependsOnTheseNamespaces, $classDescriptions[0], 'because');
4858

4959
self::assertCount(2, $violations);
5060
self::assertEquals('path/to/class.php', $violations->get(0)->getFilePath());
@@ -69,9 +79,7 @@ public function bar($a, $b)
6979
}
7080
EOF;
7181

72-
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_7_4);
73-
$fp->parse($code, 'relativePathName');
74-
$cd = $fp->getClassDescriptions();
82+
$cd = $this->parseCode($code);
7583

7684
self::assertCount(1, $cd);
7785
self::assertCount(1, $cd[0]->getDependencies());
@@ -97,9 +105,7 @@ class Cat implements AnInterface
97105
}
98106
EOF;
99107

100-
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_7_4);
101-
$fp->parse($code, 'relativePathName');
102-
$cd = $fp->getClassDescriptions();
108+
$cd = $this->parseCode($code);
103109

104110
self::assertCount(2, $cd);
105111
self::assertInstanceOf(ClassDescription::class, $cd[0]);
@@ -141,9 +147,7 @@ class Cat implements AnInterface
141147
}
142148
EOF;
143149

144-
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_7_4);
145-
$fp->parse($code, 'relativePathName');
146-
$cd = $fp->getClassDescriptions();
150+
$cd = $this->parseCode($code);
147151

148152
self::assertCount(2, $cd);
149153
self::assertInstanceOf(ClassDescription::class, $cd[0]);
@@ -183,9 +187,7 @@ class Cat implements AnInterface
183187
}
184188
EOF;
185189

186-
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_7_4);
187-
$fp->parse($code, 'relativePathName');
188-
$cd = $fp->getClassDescriptions();
190+
$cd = $this->parseCode($code);
189191

190192
self::assertCount(2, $cd);
191193
self::assertInstanceOf(ClassDescription::class, $cd[0]);
@@ -217,10 +219,8 @@ class Cat extends Animal
217219
}
218220
EOF;
219221

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];
224224

225225
self::assertEquals('Root\Animals\Animal', $cd->getExtends()[0]->toString());
226226
}
@@ -245,10 +245,8 @@ public function methodWithAnonymous(): void
245245
}
246246
EOF;
247247

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];
252250

253251
self::assertEquals('Root\Animals\Animal', $cd->getExtends()[0]->toString());
254252
}
@@ -272,14 +270,10 @@ public function __construct(Request $request)
272270
}
273271
EOF;
274272

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);
280274

281275
$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');
283277

284278
self::assertCount(0, $violations);
285279
}
@@ -308,9 +302,7 @@ public function __construct(Request $request, ?Nullable $nullable)
308302
}
309303
EOF;
310304

311-
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_7_4);
312-
$fp->parse($code, 'relativePathName');
313-
$cd = $fp->getClassDescriptions();
305+
$cd = $this->parseCode($code);
314306

315307
$expectedDependencies = [
316308
new ClassDependency('Foo\Baz\Baz', 10),
@@ -340,15 +332,10 @@ public function __construct()
340332
}
341333
EOF;
342334

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);
349336

350337
$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');
352339

353340
self::assertCount(0, $violations);
354341
}
@@ -379,15 +366,10 @@ public function doSomething(self $self, static $static)
379366
}
380367
EOF;
381368

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);
388370

389371
$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');
391373

392374
self::assertCount(0, $violations);
393375
}
@@ -411,15 +393,10 @@ public function foo()
411393
}
412394
EOF;
413395

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);
420397

421398
$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');
423400

424401
self::assertCount(1, $violations);
425402
}
@@ -452,15 +429,10 @@ public function getStatic(): self
452429
}
453430
EOF;
454431

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);
461433

462434
$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');
464436

465437
self::assertCount(1, $violations);
466438
}
@@ -484,15 +456,11 @@ class Test implements Order
484456

485457
EOF;
486458

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
493461

494462
$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');
496464

497465
self::assertCount(0, $violations);
498466
}
@@ -512,15 +480,10 @@ public function getBookList(): QueryBuilder;
512480
}
513481
EOF;
514482

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);
521484

522485
$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');
524487

525488
self::assertCount(1, $violations);
526489
}
@@ -549,10 +512,7 @@ public function foobar();
549512
}
550513
EOF;
551514

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);
556516

557517
self::assertCount(3, $cd);
558518
self::assertEquals('MyProject\AppBundle\Application\FooAble', $cd[2]->getExtends()[0]->toString());
@@ -583,14 +543,10 @@ public function getRequest(): Request //the violations is reported here
583543
}
584544
EOF;
585545

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);
591547

592548
$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');
594550

595551
self::assertCount(0, $violations);
596552
}
@@ -608,13 +564,9 @@ public function __construct() {
608564
}
609565
EOF;
610566

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);
616568
$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');
618570

619571
self::assertCount(0, $violations);
620572
}
@@ -634,13 +586,9 @@ abstract public function foo() {}
634586
}
635587
EOF;
636588

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);
642590
$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');
644592

645593
self::assertCount(0, $violations);
646594
}
@@ -658,13 +606,9 @@ public function __construct() {
658606
}
659607
EOF;
660608

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);
666610
$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');
668612

669613
self::assertCount(0, $violations);
670614
}

0 commit comments

Comments
 (0)