Skip to content

Commit

Permalink
fix #1226 - skipped multiple violations in one file (#1227)
Browse files Browse the repository at this point in the history
* add tests for skipped violations that occure multiple times

* fix #1226
  • Loading branch information
gennadigennadigennadi authored Jun 21, 2023
1 parent 70d251d commit 2fb4cce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Contract/Analyser/EventHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ public function __construct(
*/
public function shouldViolationBeSkipped(string $depender, string $dependent): bool
{
if (!array_key_exists($depender, $this->skippedViolations)) {
return false;
}
$key = array_search($dependent, $this->unmatchedSkippedViolation[$depender], true);
if (false === $key) {
$skippedViolation = $this->skippedViolations[$depender] ?? [];
$matched = [] !== $skippedViolation && in_array($dependent, $skippedViolation, true);

if (!$matched) {
return false;
}

unset($this->unmatchedSkippedViolation[$depender][$key]);
if (false !== ($key = array_search($dependent, $this->unmatchedSkippedViolation[$depender], true))) {
unset($this->unmatchedSkippedViolation[$depender][$key]);
}

return true;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Contract/Analyser/EventHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public function testIsViolationSkipped(): void
];
$helper = new EventHelper($configuration, new LayerProvider([]));

self::assertTrue(
$helper->shouldViolationBeSkipped(
ClassLikeToken::fromFQCN('ClassWithOneDep')->toString(),
ClassLikeToken::fromFQCN('DependencyClass')->toString()
)
);
// also skips multiple occurrences
self::assertTrue(
$helper->shouldViolationBeSkipped(
ClassLikeToken::fromFQCN('ClassWithOneDep')->toString(),
Expand Down Expand Up @@ -73,6 +80,13 @@ public function testUnmatchedSkippedViolations(): void
];
$helper = new EventHelper($configuration, new LayerProvider([]));

self::assertTrue(
$helper->shouldViolationBeSkipped(
ClassLikeToken::fromFQCN('ClassWithOneDep')->toString(),
ClassLikeToken::fromFQCN('DependencyClass')->toString()
)
);
// also skips multiple occurrences
self::assertTrue(
$helper->shouldViolationBeSkipped(
ClassLikeToken::fromFQCN('ClassWithOneDep')->toString(),
Expand Down

0 comments on commit 2fb4cce

Please sign in to comment.