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 17, 2024
1 parent 204026e commit a7fe6f3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ $rules[] = Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\Controller'))
->should(new Extend('App\Controller\AbstractController'))
->because('we want to be sure that all controllers extend AbstractController');

You can add multiple parameters, the violation will happen when none of them match
```

### Has an attribute (requires PHP >= 8.0)
Expand Down
19 changes: 12 additions & 7 deletions src/Expression/ForClasses/Extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,30 @@

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;
/** @var string $className */
foreach ($this->classNames as $className) {
if (null !== $extends && $extends->matches($className)) {
return;
}
}

$violation = Violation::create(
Expand Down
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 a7fe6f3

Please sign in to comment.