From 0bc3c0fe3170593a38f1fb952d0a2a19ae235513 Mon Sep 17 00:00:00 2001 From: Gennadi McKelvey Date: Tue, 20 Jun 2023 17:38:51 +0200 Subject: [PATCH 1/2] add tests for skipped violations that occure multiple times --- tests/Contract/Analyser/EventHelperTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Contract/Analyser/EventHelperTest.php b/tests/Contract/Analyser/EventHelperTest.php index 713db5921..0c229378d 100644 --- a/tests/Contract/Analyser/EventHelperTest.php +++ b/tests/Contract/Analyser/EventHelperTest.php @@ -26,6 +26,14 @@ public function testIsViolationSkipped(): void ]; $helper = new EventHelper($configuration, new LayerProvider([])); + self::assertTrue( + $helper->shouldViolationBeSkipped( + ClassLikeToken::fromFQCN('ClassWithOneDep')->toString(), + ClassLikeToken::fromFQCN('DependencyClass')->toString() + ) + ); + + // also skipps multiple occurrences self::assertTrue( $helper->shouldViolationBeSkipped( ClassLikeToken::fromFQCN('ClassWithOneDep')->toString(), @@ -73,6 +81,13 @@ public function testUnmatchedSkippedViolations(): void ]; $helper = new EventHelper($configuration, new LayerProvider([])); + self::assertTrue( + $helper->shouldViolationBeSkipped( + ClassLikeToken::fromFQCN('ClassWithOneDep')->toString(), + ClassLikeToken::fromFQCN('DependencyClass')->toString() + ) + ); + // also skipps multiple occurrences self::assertTrue( $helper->shouldViolationBeSkipped( ClassLikeToken::fromFQCN('ClassWithOneDep')->toString(), From 8a1c0489554dcd0315bc975c3f9c42b40e670630 Mon Sep 17 00:00:00 2001 From: Gennadi McKelvey Date: Tue, 20 Jun 2023 10:59:24 +0200 Subject: [PATCH 2/2] fix #1226 --- src/Contract/Analyser/EventHelper.php | 13 +++++++------ tests/Contract/Analyser/EventHelperTest.php | 5 ++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Contract/Analyser/EventHelper.php b/src/Contract/Analyser/EventHelper.php index c6d649157..a4c937044 100644 --- a/src/Contract/Analyser/EventHelper.php +++ b/src/Contract/Analyser/EventHelper.php @@ -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; } diff --git a/tests/Contract/Analyser/EventHelperTest.php b/tests/Contract/Analyser/EventHelperTest.php index 0c229378d..5e605e70d 100644 --- a/tests/Contract/Analyser/EventHelperTest.php +++ b/tests/Contract/Analyser/EventHelperTest.php @@ -32,8 +32,7 @@ public function testIsViolationSkipped(): void ClassLikeToken::fromFQCN('DependencyClass')->toString() ) ); - - // also skipps multiple occurrences + // also skips multiple occurrences self::assertTrue( $helper->shouldViolationBeSkipped( ClassLikeToken::fromFQCN('ClassWithOneDep')->toString(), @@ -87,7 +86,7 @@ public function testUnmatchedSkippedViolations(): void ClassLikeToken::fromFQCN('DependencyClass')->toString() ) ); - // also skipps multiple occurrences + // also skips multiple occurrences self::assertTrue( $helper->shouldViolationBeSkipped( ClassLikeToken::fromFQCN('ClassWithOneDep')->toString(),