From fb43431c2e0c00e22cd9a995d01a9a608bea1ef4 Mon Sep 17 00:00:00 2001 From: Herberto Graca Date: Wed, 26 Jul 2023 23:04:59 +0200 Subject: [PATCH 1/2] Replace `array` for `string ...` This provides better typing --- src/Analyzer/ClassDescription.php | 2 +- src/Rules/ArchRule.php | 2 +- tests/Unit/Analyzer/ClassDescriptionTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Analyzer/ClassDescription.php b/src/Analyzer/ClassDescription.php index 0a2719f2..7d49407c 100644 --- a/src/Analyzer/ClassDescription.php +++ b/src/Analyzer/ClassDescription.php @@ -93,7 +93,7 @@ public function namespaceMatches(string $pattern): bool return $this->FQCN->matches($pattern); } - public function namespaceMatchesOneOfTheseNamespaces(array $classesToBeExcluded): bool + public function namespaceMatchesOneOfTheseNamespaces(string ...$classesToBeExcluded): bool { foreach ($classesToBeExcluded as $classToBeExcluded) { if ($this->namespaceMatches($classToBeExcluded)) { diff --git a/src/Rules/ArchRule.php b/src/Rules/ArchRule.php index 2909d417..cb29d8f3 100644 --- a/src/Rules/ArchRule.php +++ b/src/Rules/ArchRule.php @@ -38,7 +38,7 @@ public function __construct( public function check(ClassDescription $classDescription, Violations $violations): void { - if ($classDescription->namespaceMatchesOneOfTheseNamespaces($this->classesToBeExcluded)) { + if ($classDescription->namespaceMatchesOneOfTheseNamespaces(...$this->classesToBeExcluded)) { return; } diff --git a/tests/Unit/Analyzer/ClassDescriptionTest.php b/tests/Unit/Analyzer/ClassDescriptionTest.php index 4726d027..0642c3ac 100644 --- a/tests/Unit/Analyzer/ClassDescriptionTest.php +++ b/tests/Unit/Analyzer/ClassDescriptionTest.php @@ -36,7 +36,7 @@ public function test_should_return_true_if_there_class_is_in_namespace_array(): { $cd = $this->builder->build(); - $this->assertTrue($cd->namespaceMatchesOneOfTheseNamespaces(['Fruit'])); + $this->assertTrue($cd->namespaceMatchesOneOfTheseNamespaces('Fruit', 'Banana')); } public function test_should_return_true_if_is_annotated_with(): void From aadf6234048df0e2ea826a8c7abf199b0f6cecad Mon Sep 17 00:00:00 2001 From: Herberto Graca Date: Sat, 23 Sep 2023 16:21:09 +0200 Subject: [PATCH 2/2] fixup! --- src/Analyzer/ClassDescription.php | 7 ++++++- src/Rules/ArchRule.php | 2 +- tests/Unit/Analyzer/ClassDescriptionTest.php | 9 ++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Analyzer/ClassDescription.php b/src/Analyzer/ClassDescription.php index 7d49407c..01edd26a 100644 --- a/src/Analyzer/ClassDescription.php +++ b/src/Analyzer/ClassDescription.php @@ -93,7 +93,12 @@ public function namespaceMatches(string $pattern): bool return $this->FQCN->matches($pattern); } - public function namespaceMatchesOneOfTheseNamespaces(string ...$classesToBeExcluded): bool + public function namespaceMatchesOneOfTheseNamespaces(array $classesToBeExcluded): bool + { + return $this->namespaceMatchesOneOfTheseNamespacesSplat(...$classesToBeExcluded); + } + + public function namespaceMatchesOneOfTheseNamespacesSplat(string ...$classesToBeExcluded): bool { foreach ($classesToBeExcluded as $classToBeExcluded) { if ($this->namespaceMatches($classToBeExcluded)) { diff --git a/src/Rules/ArchRule.php b/src/Rules/ArchRule.php index cb29d8f3..198fa16b 100644 --- a/src/Rules/ArchRule.php +++ b/src/Rules/ArchRule.php @@ -38,7 +38,7 @@ public function __construct( public function check(ClassDescription $classDescription, Violations $violations): void { - if ($classDescription->namespaceMatchesOneOfTheseNamespaces(...$this->classesToBeExcluded)) { + if ($classDescription->namespaceMatchesOneOfTheseNamespacesSplat(...$this->classesToBeExcluded)) { return; } diff --git a/tests/Unit/Analyzer/ClassDescriptionTest.php b/tests/Unit/Analyzer/ClassDescriptionTest.php index 0642c3ac..7dc06266 100644 --- a/tests/Unit/Analyzer/ClassDescriptionTest.php +++ b/tests/Unit/Analyzer/ClassDescriptionTest.php @@ -36,7 +36,14 @@ public function test_should_return_true_if_there_class_is_in_namespace_array(): { $cd = $this->builder->build(); - $this->assertTrue($cd->namespaceMatchesOneOfTheseNamespaces('Fruit', 'Banana')); + $this->assertTrue($cd->namespaceMatchesOneOfTheseNamespaces(['Fruit'])); + } + + public function test_should_return_true_if_there_class_is_in_namespace_list(): void + { + $cd = $this->builder->build(); + + $this->assertTrue($cd->namespaceMatchesOneOfTheseNamespacesSplat('Fruit', 'Banana')); } public function test_should_return_true_if_is_annotated_with(): void