Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit 7939e6d

Browse files
committed
Indent violations output
1 parent 5cc1d6c commit 7939e6d

File tree

8 files changed

+95
-87
lines changed

8 files changed

+95
-87
lines changed

src/Rules/Violations.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Arkitect\Exceptions\FailOnFirstViolationException;
88
use Arkitect\Exceptions\IndexNotFoundException;
9+
use Arkitect\Shared\String\IndentationHelper;
910

1011
class Violations implements \IteratorAggregate, \Countable, \JsonSerializable
1112
{
@@ -86,20 +87,22 @@ public function toString(): string
8687
*/
8788
foreach ($violationsCollection as $key => $violationsByFqcn) {
8889
$violationForThisFqcn = \count($violationsByFqcn);
89-
$errors .= "\n$key has {$violationForThisFqcn} violations";
90+
$errors .= "\n$key has {$violationForThisFqcn} violations\n";
9091

92+
$violationDescription = '';
9193
foreach ($violationsByFqcn as $violation) {
92-
$errors .= "\n";
93-
$errors .= $violation->getError();
94+
$violationDescription .= "\n";
95+
$violationDescription .= $violation->getError();
9496

9597
if (null !== $violation->getLine()) {
96-
$errors .= ' (on line '.$violation->getLine().')';
98+
$violationDescription .= ' (on line '.$violation->getLine().')';
9799
}
98-
$errors .= "\n";
100+
$violationDescription .= "\n";
99101
}
102+
$errors .= IndentationHelper::indent(trim($violationDescription))."\n";
100103
}
101104

102-
return $errors;
105+
return IndentationHelper::clearEmptyLines($errors);
103106
}
104107

105108
public function toArray(): array

src/Shared/String/IndentationHelper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ public static function indent(string $text, int $spaces = 2): string
1010
{
1111
return preg_replace('/^/m', str_repeat(' ', $spaces), $text);
1212
}
13+
14+
public static function clearEmptyLines(string $text): string
15+
{
16+
return preg_replace('/^\s+$/m', '', $text);
17+
}
1318
}

tests/E2E/Cli/CheckCommandTest.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,34 @@ public function test_app_returns_error_with_multiple_violations(): void
3535
$expectedErrors = 'ERRORS!
3636
3737
App\Controller\Foo has 2 violations
38-
should have a name that matches *Controller
39-
because we want uniform naming
38+
should have a name that matches *Controller
39+
because we want uniform naming
4040
41-
should implement ContainerAwareInterface
42-
because all controllers should be container aware
41+
should implement ContainerAwareInterface
42+
because all controllers should be container aware
4343
4444
App\Controller\ProductsController has 1 violations
45-
should implement ContainerAwareInterface
46-
because all controllers should be container aware
45+
should implement ContainerAwareInterface
46+
because all controllers should be container aware
4747
4848
App\Controller\UserController has 1 violations
49-
should implement ContainerAwareInterface
50-
because all controllers should be container aware
49+
should implement ContainerAwareInterface
50+
because all controllers should be container aware
5151
5252
App\Controller\YieldController has 1 violations
53-
should implement ContainerAwareInterface
54-
because all controllers should be container aware
53+
should implement ContainerAwareInterface
54+
because all controllers should be container aware
5555
5656
App\Domain\Model has 2 violations
57-
depends on App\Services\UserService
58-
from the rule
59-
should not depend on classes outside namespace App\Domain
60-
because we want protect our domain (on line 14)
57+
depends on App\Services\UserService
58+
from the rule
59+
should not depend on classes outside namespace App\Domain
60+
because we want protect our domain (on line 14)
6161
62-
depends on App\Services\CartService
63-
from the rule
64-
should not depend on classes outside namespace App\Domain
65-
because we want protect our domain (on line 15)';
62+
depends on App\Services\CartService
63+
from the rule
64+
should not depend on classes outside namespace App\Domain
65+
because we want protect our domain (on line 15)';
6666

6767
$this->assertCheckHasErrors($cmdTester, $expectedErrors);
6868
}
@@ -73,8 +73,8 @@ public function test_app_returns_single_error_because_there_is_stop_on_failure_p
7373

7474
$expectedErrors = '
7575
App\Controller\Foo has 1 violations
76-
should implement ContainerAwareInterface
77-
because all controllers should be container aware';
76+
should implement ContainerAwareInterface
77+
because all controllers should be container aware';
7878

7979
$this->assertCheckHasErrors($cmdTester, $expectedErrors);
8080
$this->assertCheckHasNoErrorsLike($cmdTester, "App\Controller\ProductsController has 1 violations");
@@ -109,7 +109,7 @@ public function test_bug_yield(): void
109109
$expectedErrors = 'ERRORS!
110110
111111
App\Controller\Foo has 1 violations
112-
should have a name that matches *Controller';
112+
should have a name that matches *Controller';
113113

114114
$this->assertCheckHasErrors($cmdTester, $expectedErrors);
115115
}

tests/E2E/PHPUnit/CheckClassHaveAttributeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public function test_controllers_should_have_name_ending_in_controller(): void
3939

4040
$expectedExceptionMessage = '
4141
App\Controller\Foo has 1 violations
42-
should have a name that matches *Controller
43-
because its a symfony thing';
42+
should have a name that matches *Controller
43+
because its a symfony thing';
4444

4545
$this->expectException(ExpectationFailedException::class);
4646
$this->expectExceptionMessage($expectedExceptionMessage);

tests/E2E/PHPUnit/CheckClassImplementInterfaceTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@ public function test_assertion_should_fail_on_broken_rule(): void
2626

2727
$expectedExceptionMessage = '
2828
App\Controller\Foo has 1 violations
29-
should implement ContainerAwareInterface
30-
because i said so
29+
should implement ContainerAwareInterface
30+
because i said so
3131
3232
App\Controller\ProductsController has 1 violations
33-
should implement ContainerAwareInterface
34-
because i said so
33+
should implement ContainerAwareInterface
34+
because i said so
3535
3636
App\Controller\UserController has 1 violations
37-
should implement ContainerAwareInterface
38-
because i said so
37+
should implement ContainerAwareInterface
38+
because i said so
3939
4040
App\Controller\YieldController has 1 violations
41-
should implement ContainerAwareInterface
42-
because i said so
41+
should implement ContainerAwareInterface
42+
because i said so
4343
4444
App\Services\UserService has 1 violations
45-
should implement ContainerAwareInterface
46-
because i said so';
45+
should implement ContainerAwareInterface
46+
because i said so';
4747

4848
$this->expectException(ExpectationFailedException::class);
4949
$this->expectExceptionMessage($expectedExceptionMessage);

tests/E2E/Smoke/RunArkitectBinTest.php

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,34 @@ public function test_returns_error_with_multiple_violations(): void
2323
$expectedErrors = 'ERRORS!
2424
2525
App\Controller\Foo has 2 violations
26-
should have a name that matches *Controller
27-
because we want uniform naming
26+
should have a name that matches *Controller
27+
because we want uniform naming
2828
29-
should implement ContainerAwareInterface
30-
because all controllers should be container aware
29+
should implement ContainerAwareInterface
30+
because all controllers should be container aware
3131
3232
App\Controller\ProductsController has 1 violations
33-
should implement ContainerAwareInterface
34-
because all controllers should be container aware
33+
should implement ContainerAwareInterface
34+
because all controllers should be container aware
3535
3636
App\Controller\UserController has 1 violations
37-
should implement ContainerAwareInterface
38-
because all controllers should be container aware
37+
should implement ContainerAwareInterface
38+
because all controllers should be container aware
3939
4040
App\Controller\YieldController has 1 violations
41-
should implement ContainerAwareInterface
42-
because all controllers should be container aware
41+
should implement ContainerAwareInterface
42+
because all controllers should be container aware
4343
4444
App\Domain\Model has 2 violations
45-
depends on App\Services\UserService
46-
from the rule
47-
should not depend on classes outside namespace App\Domain
48-
because we want protect our domain (on line 14)
49-
50-
depends on App\Services\CartService
51-
from the rule
52-
should not depend on classes outside namespace App\Domain
53-
because we want protect our domain (on line 15)
45+
depends on App\Services\UserService
46+
from the rule
47+
should not depend on classes outside namespace App\Domain
48+
because we want protect our domain (on line 14)
49+
50+
depends on App\Services\CartService
51+
from the rule
52+
should not depend on classes outside namespace App\Domain
53+
because we want protect our domain (on line 15)
5454
';
5555

5656
$this->assertEquals(self::ERROR_CODE, $process->getExitCode());
@@ -64,34 +64,34 @@ public function test_returns_error_with_multiple_violations_without_passing_conf
6464
$expectedErrors = 'ERRORS!
6565
6666
App\Controller\Foo has 2 violations
67-
should have a name that matches *Controller
68-
because we want uniform naming
67+
should have a name that matches *Controller
68+
because we want uniform naming
6969
70-
should implement ContainerAwareInterface
71-
because all controllers should be container aware
70+
should implement ContainerAwareInterface
71+
because all controllers should be container aware
7272
7373
App\Controller\ProductsController has 1 violations
74-
should implement ContainerAwareInterface
75-
because all controllers should be container aware
74+
should implement ContainerAwareInterface
75+
because all controllers should be container aware
7676
7777
App\Controller\UserController has 1 violations
78-
should implement ContainerAwareInterface
79-
because all controllers should be container aware
78+
should implement ContainerAwareInterface
79+
because all controllers should be container aware
8080
8181
App\Controller\YieldController has 1 violations
82-
should implement ContainerAwareInterface
83-
because all controllers should be container aware
82+
should implement ContainerAwareInterface
83+
because all controllers should be container aware
8484
8585
App\Domain\Model has 2 violations
86-
depends on App\Services\UserService
87-
from the rule
88-
should not depend on classes outside namespace App\Domain
89-
because we want protect our domain (on line 14)
90-
91-
depends on App\Services\CartService
92-
from the rule
93-
should not depend on classes outside namespace App\Domain
94-
because we want protect our domain (on line 15)
86+
depends on App\Services\UserService
87+
from the rule
88+
should not depend on classes outside namespace App\Domain
89+
because we want protect our domain (on line 14)
90+
91+
depends on App\Services\CartService
92+
from the rule
93+
should not depend on classes outside namespace App\Domain
94+
because we want protect our domain (on line 15)
9595
';
9696

9797
$this->assertStringContainsString($expectedErrors, $process->getOutput());
@@ -120,7 +120,7 @@ public function test_bug_yield(): void
120120
$expectedErrors = 'ERRORS!
121121
122122
App\Controller\Foo has 1 violations
123-
should have a name that matches *Controller';
123+
should have a name that matches *Controller';
124124

125125
$this->assertEquals(self::ERROR_CODE, $process->getExitCode());
126126
$this->assertStringContainsString($expectedErrors, $process->getOutput());

tests/Unit/Expressions/ForClasses/NotDependsOnTheseExpressionsTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ public function test_it_should_see_violations_only_outside_exclusions(): void
8585
);
8686
self::assertStringContainsString(
8787
<<<TXT
88-
NOT resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentA\
89-
OR
90-
NOT (
91-
resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentC\
92-
AND
93-
not resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentC\ComponentCA\
94-
)
88+
NOT resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentA\
89+
OR
90+
NOT (
91+
resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentC\
92+
AND
93+
not resides in one of these namespaces: Arkitect\Tests\Fixtures\ComponentC\ComponentCA\
94+
)
9595
TXT
9696
,
9797
$violationsText

tests/Unit/Rules/ViolationsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public function test_to_string(): void
5959
$this->violationStore->add($violation);
6060
$expected = '
6161
App\Controller\ProductController has 1 violations
62-
should implement ContainerInterface
62+
should implement ContainerInterface
6363
6464
App\Controller\Foo has 1 violations
65-
should have name end with Controller
65+
should have name end with Controller
6666
';
6767

6868
$this->assertEquals($expected, $this->violationStore->toString());

0 commit comments

Comments
 (0)