Skip to content

Commit

Permalink
Add ability to check for multiple extends
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Willem Kaper committed May 14, 2024
1 parent 204026e commit 5e0f5d9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
34 changes: 22 additions & 12 deletions src/Expression/ForClasses/Extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
19 changes: 17 additions & 2 deletions tests/Unit/Expressions/ForClasses/ExtendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}

0 comments on commit 5e0f5d9

Please sign in to comment.