-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #383 from simivar/feature/is-not-interface
Add IsInterface and IsNotInterface
- Loading branch information
Showing
5 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Expression\ForClasses; | ||
|
||
use Arkitect\Analyzer\ClassDescription; | ||
use Arkitect\Expression\Description; | ||
use Arkitect\Expression\Expression; | ||
use Arkitect\Rules\Violation; | ||
use Arkitect\Rules\ViolationMessage; | ||
use Arkitect\Rules\Violations; | ||
|
||
class IsInterface implements Expression | ||
{ | ||
public function describe(ClassDescription $theClass, string $because): Description | ||
{ | ||
return new Description("{$theClass->getName()} should be an interface", $because); | ||
} | ||
|
||
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void | ||
{ | ||
if ($theClass->isInterface()) { | ||
return; | ||
} | ||
|
||
$violation = Violation::create( | ||
$theClass->getFQCN(), | ||
ViolationMessage::selfExplanatory($this->describe($theClass, $because)) | ||
); | ||
|
||
$violations->add($violation); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Expression\ForClasses; | ||
|
||
use Arkitect\Analyzer\ClassDescription; | ||
use Arkitect\Expression\Description; | ||
use Arkitect\Expression\Expression; | ||
use Arkitect\Rules\Violation; | ||
use Arkitect\Rules\ViolationMessage; | ||
use Arkitect\Rules\Violations; | ||
|
||
class IsNotInterface implements Expression | ||
{ | ||
public function describe(ClassDescription $theClass, string $because): Description | ||
{ | ||
return new Description("{$theClass->getName()} should not be an interface", $because); | ||
} | ||
|
||
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void | ||
{ | ||
if (!$theClass->isInterface()) { | ||
return; | ||
} | ||
|
||
$violation = Violation::create( | ||
$theClass->getFQCN(), | ||
ViolationMessage::selfExplanatory($this->describe($theClass, $because)) | ||
); | ||
|
||
$violations->add($violation); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Tests\Unit\Expressions\ForClasses; | ||
|
||
use Arkitect\Analyzer\ClassDescription; | ||
use Arkitect\Analyzer\FullyQualifiedClassName; | ||
use Arkitect\Expression\ForClasses\IsInterface; | ||
use Arkitect\Rules\Violations; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class IsInterfaceTest extends TestCase | ||
{ | ||
public function test_it_should_return_violation_error(): void | ||
{ | ||
$isFinal = new IsInterface(); | ||
$classDescription = new ClassDescription( | ||
FullyQualifiedClassName::fromString('HappyIsland'), | ||
[], | ||
[], | ||
null, | ||
false, | ||
false, | ||
false, | ||
false, | ||
false | ||
); | ||
$because = 'we want to add this rule for our software'; | ||
$violationError = $isFinal->describe($classDescription, $because)->toString(); | ||
|
||
$violations = new Violations(); | ||
$isFinal->evaluate($classDescription, $violations, $because); | ||
self::assertNotEquals(0, $violations->count()); | ||
|
||
$this->assertEquals('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_interface(): void | ||
{ | ||
$isFinal = new IsInterface(); | ||
$classDescription = new ClassDescription( | ||
FullyQualifiedClassName::fromString('HappyIsland'), | ||
[], | ||
[], | ||
null, | ||
false, | ||
false, | ||
true, | ||
false, | ||
false | ||
); | ||
$because = 'we want to add this rule for our software'; | ||
$violations = new Violations(); | ||
$isFinal->evaluate($classDescription, $violations, $because); | ||
self::assertEquals(0, $violations->count()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Tests\Unit\Expressions\ForClasses; | ||
|
||
use Arkitect\Analyzer\ClassDescription; | ||
use Arkitect\Analyzer\FullyQualifiedClassName; | ||
use Arkitect\Expression\ForClasses\IsNotInterface; | ||
use Arkitect\Rules\Violations; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class IsNotInterfaceTest extends TestCase | ||
{ | ||
public function test_it_should_return_violation_error(): void | ||
{ | ||
$isFinal = new IsNotInterface(); | ||
$classDescription = new ClassDescription( | ||
FullyQualifiedClassName::fromString('HappyIsland'), | ||
[], | ||
[], | ||
null, | ||
false, | ||
false, | ||
true, | ||
false, | ||
false | ||
); | ||
$because = 'we want to add this rule for our software'; | ||
$violationError = $isFinal->describe($classDescription, $because)->toString(); | ||
|
||
$violations = new Violations(); | ||
$isFinal->evaluate($classDescription, $violations, $because); | ||
self::assertNotEquals(0, $violations->count()); | ||
|
||
$this->assertEquals('HappyIsland should not 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 | ||
{ | ||
$isFinal = new IsNotInterface(); | ||
$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(); | ||
$isFinal->evaluate($classDescription, $violations, $because); | ||
self::assertEquals(0, $violations->count()); | ||
} | ||
} |