From 5e0f5d928fdcc6b066a49a593e0ffbc09b264d96 Mon Sep 17 00:00:00 2001 From: Jan Willem Kaper Date: Tue, 14 May 2024 15:18:08 +0200 Subject: [PATCH] Add ability to check for multiple extends --- src/Expression/ForClasses/Extend.php | 34 ++++++++++++------- .../Expressions/ForClasses/ExtendTest.php | 19 +++++++++-- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/Expression/ForClasses/Extend.php b/src/Expression/ForClasses/Extend.php index b27947c5..3ecb56a1 100644 --- a/src/Expression/ForClasses/Extend.php +++ b/src/Expression/ForClasses/Extend.php @@ -13,32 +13,42 @@ class Extend implements Expression { - /** @var string */ - private $className; + /** @var string[] */ + private $classNames; - public function __construct(string $className) + public function __construct(string ...$classNames) { - $this->className = $className; + $this->classNames = $classNames; } public function describe(ClassDescription $theClass, string $because): Description { - return new Description("should extend {$this->className}", $because); + $desc = implode(', ', $this->classNames); + + return new Description("should extend one of these classes: {$desc}", $because); } public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void { $extends = $theClass->getExtends(); - if (null !== $extends && $extends->matches($this->className)) { - return; + $matches = false; + + /** @var string $className */ + foreach ($this->classNames as $className) { + if (null !== $extends && $extends->matches($className)) { + $matches = true; + break; + } } - $violation = Violation::create( - $theClass->getFQCN(), - ViolationMessage::selfExplanatory($this->describe($theClass, $because)) - ); + if (!$matches) { + $violation = Violation::create( + $theClass->getFQCN(), + ViolationMessage::selfExplanatory($this->describe($theClass, $because)) + ); - $violations->add($violation); + $violations->add($violation); + } } } diff --git a/tests/Unit/Expressions/ForClasses/ExtendTest.php b/tests/Unit/Expressions/ForClasses/ExtendTest.php index 749f305e..e73015d5 100644 --- a/tests/Unit/Expressions/ForClasses/ExtendTest.php +++ b/tests/Unit/Expressions/ForClasses/ExtendTest.php @@ -70,7 +70,7 @@ public function test_it_should_return_violation_error_when_class_not_extend(): v $extend->evaluate($classDescription, $violations, 'we want to add this rule for our software'); self::assertEquals(1, $violations->count()); - self::assertEquals('should extend My\BaseClass because we want to add this rule for our software', $violations->get(0)->getError()); + self::assertEquals('should extend one of these classes: My\BaseClass because we want to add this rule for our software', $violations->get(0)->getError()); } public function test_it_should_return_violation_error_if_extend_is_null(): void @@ -97,6 +97,21 @@ public function test_it_should_return_violation_error_if_extend_is_null(): void $extend->evaluate($classDescription, $violations, $because); self::assertEquals(1, $violations->count()); - self::assertEquals('should extend My\BaseClass because we want to add this rule for our software', $violationError); + self::assertEquals('should extend one of these classes: My\BaseClass because we want to add this rule for our software', $violationError); + } + + public function test_it_should_accept_multiple_extends(): void + { + $extend = new Extend('My\FirstExtend', 'My\SecondExtend'); + + $classDescription = (new ClassDescriptionBuilder()) + ->setClassName('My\Class') + ->setExtends('My\SecondExtend', 10) + ->build(); + + $violations = new Violations(); + $extend->evaluate($classDescription, $violations, 'because'); + + self::assertEquals(0, $violations->count()); } }