Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Expression/Boolean/Not.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Arkitect\Expression\Boolean;

use Arkitect\Analyzer\ClassDescription;
use Arkitect\Expression\Description;
use Arkitect\Expression\Expression;
use Arkitect\Rules\Violation;
use Arkitect\Rules\ViolationMessage;
use Arkitect\Rules\Violations;

final class Not implements Expression
{
/** @var Expression */
private $expression;

public function __construct(Expression $expression)
{
$this->expression = $expression;
}

public function describe(ClassDescription $theClass, string $because): Description
{
return new Description('must NOT ('.$this->expression->describe($theClass, '')->toString().')', $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
{
$newViolations = new Violations();
$this->expression->evaluate($theClass, $newViolations, $because);
if (0 !== $newViolations->count()) {
return;
}

$violations->add(Violation::create(
$theClass->getFQCN(),
ViolationMessage::selfExplanatory($this->describe($theClass, $because))
));
}
}
59 changes: 59 additions & 0 deletions tests/Unit/Expressions/Boolean/NotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Arkitect\Tests\Unit\Expressions\Boolean;

use Arkitect\Analyzer\ClassDescription;
use Arkitect\Analyzer\FullyQualifiedClassName;
use Arkitect\Expression\Boolean\Not;
use Arkitect\Expression\ForClasses\IsInterface;
use Arkitect\Rules\Violations;
use PHPUnit\Framework\TestCase;

class NotTest extends TestCase
{
public function test_it_should_return_violation_error(): void
{
$isNotInterface = new Not(new IsInterface());
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[],
null,
false,
false,
true,
false,
false
);
$because = 'we want to add this rule for our software';
$violationError = $isNotInterface->describe($classDescription, $because)->toString();

$violations = new Violations();
$isNotInterface->evaluate($classDescription, $violations, $because);
self::assertNotEquals(0, $violations->count());

$this->assertEquals('must NOT (HappyIsland should be an interface) because we want to add this rule for our software', $violationError);
}

public function test_it_should_return_true_if_is_not_interface(): void
{
$isNotInterface = new Not(new IsInterface());
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[],
null,
false,
false,
false,
false,
false
);
$because = 'we want to add this rule for our software';
$violations = new Violations();
$isNotInterface->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());
}
}