Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions src/Analyzer/ClassDescriptionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class ClassDescriptionBuilder
private $classDependencies = [];

/** @var ?FullyQualifiedClassName */
private $FQCN = null;
private $FQCN;

/** @var list<FullyQualifiedClassName> */
private $interfaces = [];

/** @var ?FullyQualifiedClassName */
private $extend = null;
private $extend;

/** @var bool */
private $final = false;
Expand Down
20 changes: 10 additions & 10 deletions src/Analyzer/FileVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public function enterNode(Node $node): void
*
* @see FileVisitorTest::test_it_should_return_errors_for_const_outside_namespace
*/
if ($node instanceof Node\Expr\ClassConstFetch &&
method_exists($node->class, 'toString')
if ($node instanceof Node\Expr\ClassConstFetch
&& method_exists($node->class, 'toString')
) {
if ($this->isSelfOrStaticOrParent($node->class->toString())) {
return;
Expand All @@ -89,8 +89,8 @@ public function enterNode(Node $node): void
*
* @see FileVisitorTest::test_should_returns_all_dependencies
*/
if ($node instanceof Node\Expr\StaticCall &&
method_exists($node->class, 'toString')
if ($node instanceof Node\Expr\StaticCall
&& method_exists($node->class, 'toString')
) {
if ($this->isSelfOrStaticOrParent($node->class->toString())) {
return;
Expand All @@ -100,8 +100,8 @@ public function enterNode(Node $node): void
->addDependency(new ClassDependency($node->class->toString(), $node->getLine()));
}

if ($node instanceof Node\Expr\Instanceof_ &&
method_exists($node->class, 'toString')
if ($node instanceof Node\Expr\Instanceof_
&& method_exists($node->class, 'toString')
) {
if ($this->isSelfOrStaticOrParent($node->class->toString())) {
return;
Expand All @@ -110,11 +110,11 @@ public function enterNode(Node $node): void
->addDependency(new ClassDependency($node->class->toString(), $node->getLine()));
}

if ($node instanceof Node\Expr\New_ &&
!($node->class instanceof Node\Expr\Variable)
if ($node instanceof Node\Expr\New_
&& !($node->class instanceof Node\Expr\Variable)
) {
if ((method_exists($node->class, 'isAnonymous') && $node->class->isAnonymous()) ||
!method_exists($node->class, 'toString')) {
if ((method_exists($node->class, 'isAnonymous') && $node->class->isAnonymous())
|| !method_exists($node->class, 'toString')) {
return;
}

Expand Down
4 changes: 1 addition & 3 deletions src/Analyzer/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function enterNode(Node $node)

if ($this->parseCustomAnnotations && !($node->type instanceof FullyQualified)) {
foreach ($phpDocNode->getTags() as $tagValue) {
if ('@' === $tagValue->name[0] && false === strpos($tagValue->name, '@var')) {
if ('@' === $tagValue->name[0] && !str_contains($tagValue->name, '@var')) {
$customTag = str_replace('@', '', $tagValue->name);
$type = $this->resolveName(new Node\Name($customTag), Use_::TYPE_NORMAL);
$node->type = $type;
Expand Down Expand Up @@ -365,8 +365,6 @@ private function resolveSignature($node): void
* @psalm-suppress MissingParamType
* @psalm-suppress PossiblyNullArgument
* @psalm-suppress MissingReturnType
*
* @param mixed $node
*/
private function resolveType($node)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Analyzer/PatternString.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public function toString(): string
private function containsWildcard(string $pattern): bool
{
return
str_contains($pattern, '*') ||
str_contains($pattern, '?') ||
str_contains($pattern, '.') ||
str_contains($pattern, '[');
str_contains($pattern, '*')
|| str_contains($pattern, '?')
|| str_contains($pattern, '.')
|| str_contains($pattern, '[');
}

private function startsWithPattern(string $pattern): bool
Expand Down
46 changes: 46 additions & 0 deletions src/Expression/Boolean/Orx.php
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)
Copy link
Contributor

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)?

{
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) {
Copy link

Choose a reason for hiding this comment

The 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 🙏

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay 🖖

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added with tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually not sure this is a good idea.
If we want to build automations on top of this, we will want to instantiate this with a list of expressions that are somehow inferred.
This list might have something or might be empty, in which case I think it should be irrelevant and therefore should not fail, essentially being ignored.

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))
));
}
}
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/DependsOnlyOnTheseNamespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function evaluate(ClassDescription $theClass, Violations $violations, str
/** @var ClassDependency $dependency */
foreach ($dependencies as $dependency) {
if (
'' === $dependency->getFQCN()->namespace() ||
$theClass->namespaceMatches($dependency->getFQCN()->namespace())
'' === $dependency->getFQCN()->namespace()
|| $theClass->namespaceMatches($dependency->getFQCN()->namespace())
) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Violation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Violation implements \JsonSerializable
/** @var string */
private $error;

public function __construct(string $fqcn, string $error, ?int $line = null)
public function __construct(string $fqcn, string $error, int $line = null)
{
$this->fqcn = $fqcn;
$this->error = $error;
Expand Down
2 changes: 1 addition & 1 deletion tests/E2E/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function test_you_can_ignore_the_default_baseline(): void
protected function runCheck(
$configFilePath = null,
bool $stopOnFailure = null,
?string $useBaseline = null,
string $useBaseline = null,
$generateBaseline = false,
bool $skipBaseline = false
): ApplicationTester {
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/Expressions/Boolean/OrxTest.php
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public function shouldMatchNamespacesProvider(): array

/**
* @dataProvider shouldMatchNamespacesProvider
*
* @param mixed $expectedNamespace
* @param mixed $actualFQCN
* @param mixed $explanation
*/
public function test_it_should_match_namespace_and_descendants($expectedNamespace, $actualFQCN, $explanation): void
{
Expand Down