-
-
Notifications
You must be signed in to change notification settings - Fork 46
Implement logical or expression #387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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 Orx implements Expression | ||
{ | ||
/** @var Expression[] */ | ||
private $expressions; | ||
|
||
public function __construct(array $expressions) | ||
{ | ||
if (\count($expressions) <= 1) { | ||
throw new \InvalidArgumentException('at least two expression are required'); | ||
} | ||
$this->expressions = $expressions; | ||
} | ||
|
||
public function describe(ClassDescription $theClass, string $because): Description | ||
{ | ||
return new Description('at least one expression must be true', $because); | ||
} | ||
|
||
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void | ||
{ | ||
foreach ($this->expressions as $expression) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Could we throw a logicalException or violation if none or only one expression is used? And if so, add a test method 🙏 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay 🖖 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added with tests There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm actually not sure this is a good idea. WDYT? |
||
$newViolations = new Violations(); | ||
$expression->evaluate($theClass, $newViolations, $because); | ||
if (0 === $newViolations->count()) { | ||
return; | ||
} | ||
} | ||
|
||
$violations->add(Violation::create( | ||
$theClass->getFQCN(), | ||
ViolationMessage::selfExplanatory($this->describe($theClass, $because)) | ||
)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Tests\Unit\Expressions\Boolean; | ||
|
||
use Arkitect\Analyzer\ClassDescriptionBuilder; | ||
use Arkitect\Expression\Boolean\Orx; | ||
use Arkitect\Expression\ForClasses\Extend; | ||
use Arkitect\Rules\Violations; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class OrxTest extends TestCase | ||
{ | ||
public function test_it_should_throw_exception_if_no_expressions_provided(): void | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
|
||
new Orx([]); | ||
} | ||
|
||
public function test_it_should_throw_exception_if_only_one_expression_provided(): void | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
|
||
new Orx([new Extend('My\BaseClass')]); | ||
} | ||
|
||
public function test_it_should_return_no_violation_on_success(): void | ||
{ | ||
$or = new Orx([new Extend('My\BaseClass'), new Extend('Your\OtherClass')]); | ||
|
||
$classDescription = (new ClassDescriptionBuilder()) | ||
->setClassName('My\Class') | ||
->setExtends('My\BaseClass', 10) | ||
->build(); | ||
|
||
$violations = new Violations(); | ||
$or->evaluate($classDescription, $violations, 'because'); | ||
|
||
self::assertEquals(0, $violations->count()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we do
public function __construct(Expression ...$expressions)
?