Skip to content

Commit

Permalink
Group and indent OR and AND output
Browse files Browse the repository at this point in the history
  • Loading branch information
hgraca committed Oct 1, 2023
1 parent 5adeb36 commit 5cc1d6c
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/Expression/Boolean/Andx.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Arkitect\Rules\Violation;
use Arkitect\Rules\ViolationMessage;
use Arkitect\Rules\Violations;
use Arkitect\Shared\String\IndentationHelper;

final class Andx implements Expression
{
Expand All @@ -28,7 +29,7 @@ public function describe(ClassDescription $theClass, string $because = ''): Desc
$expressionsDescriptions[] = $expression->describe($theClass, $because)->toString();
}
$expressionsDescriptionsString = "(\n"
.implode("\nAND\n", array_unique(array_map('trim', $expressionsDescriptions)))
.IndentationHelper::indent(implode("\nAND\n", array_unique(array_map('trim', $expressionsDescriptions))))
."\n)";

return new Description($expressionsDescriptionsString, $because);
Expand Down
2 changes: 1 addition & 1 deletion src/Expression/Boolean/Not.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(Expression $expression)

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

public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
Expand Down
5 changes: 4 additions & 1 deletion src/Expression/Boolean/Orx.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Arkitect\Rules\Violation;
use Arkitect\Rules\ViolationMessage;
use Arkitect\Rules\Violations;
use Arkitect\Shared\String\IndentationHelper;

final class Orx implements Expression
{
Expand All @@ -26,7 +27,9 @@ public function describe(ClassDescription $theClass, string $because = ''): Desc
foreach ($this->expressions as $expression) {
$expressionsDescriptions[] = $expression->describe($theClass, $because)->toString();
}
$expressionsDescriptionsString = implode("\nOR\n", array_unique($expressionsDescriptions))."\n";
$expressionsDescriptionsString = "(\n"
.IndentationHelper::indent(implode("\nOR\n", array_unique($expressionsDescriptions)))
."\n)";

return new Description($expressionsDescriptionsString, $because);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Shared/String/IndentationHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Arkitect\Shared\String;

final class IndentationHelper
{
public static function indent(string $text, int $spaces = 2): string
{
return preg_replace('/^/m', str_repeat(' ', $spaces), $text);
}
}
20 changes: 15 additions & 5 deletions tests/Unit/Expressions/Boolean/AndxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,28 @@ public function test_it_should_not_pass_the_rule(): void
self::assertNotEquals(0, $violations->count());

$this->assertEquals(
"(\nshould implement SomeInterface\nbecause reasons\nAND\nshould extend SomeClass\nbecause reasons\n)\nbecause reasons",
"(\n"
." should implement SomeInterface\n"
." because reasons\n"
." AND\n"
." should extend SomeClass\n"
." because reasons\n"
.")\n"
.'because reasons',
$violationError
);
$this->assertEquals(
"The class 'HappyIsland' violated the expression\n"
."should extend SomeClass\n"
."from the rule\n"
."(\n"
."should implement SomeInterface\nbecause reasons\n"
."AND\n"
."should extend SomeClass\nbecause reasons\n"
.")\nbecause reasons",
." should implement SomeInterface\n"
." because reasons\n"
." AND\n"
." should extend SomeClass\n"
." because reasons\n"
.")\n"
.'because reasons',
$violations->get(0)->getError()
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Expressions/Boolean/NotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function test_it_should_return_violation_error(): void
self::assertNotEquals(0, $violations->count());

$this->assertEquals(
"NOT (HappyIsland should be an interface)\nbecause we want to add this rule for our software",
"NOT HappyIsland should be an interface\nbecause we want to add this rule for our software",
$violationError
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public function test_it_should_see_violations_only_outside_exclusions(): void
);
self::assertStringContainsString(
<<<TXT
NOT (resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentA\)
NOT resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentA\
OR
NOT ((
resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentC\
AND
not resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentC\ComponentCA\
))
NOT (
resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentC\
AND
not resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentC\ComponentCA\
)
TXT
,
$violationsText
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/RuleBuilders/Architecture/ArchitectureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function test_it_should_see_violations_only_outside_exclusions(): void
$violationsText
);
self::assertStringContainsString(
'NOT (resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentA\)',
'NOT resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentA\\',
$violationsText
);
}
Expand Down

0 comments on commit 5cc1d6c

Please sign in to comment.