From 7d7e9c1acd96191896080869883ef4ab4b6de366 Mon Sep 17 00:00:00 2001 From: Herberto Graca Date: Mon, 7 Aug 2023 21:41:29 +0200 Subject: [PATCH 1/5] Fix pattern match --- src/Analyzer/PatternString.php | 18 +----------------- tests/Unit/Analyzer/PatternStringTest.php | 4 ++++ 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/src/Analyzer/PatternString.php b/src/Analyzer/PatternString.php index 60ab84c7..cd049e0a 100644 --- a/src/Analyzer/PatternString.php +++ b/src/Analyzer/PatternString.php @@ -27,7 +27,7 @@ public function matches(string $pattern): bool return $isInThisNamespace || $isThisClass; } - return $this->startsWithPattern($pattern); + return fnmatch($pattern, $this->value, \FNM_NOESCAPE); } public function toString(): string @@ -43,20 +43,4 @@ private function containsWildcard(string $pattern): bool || str_contains($pattern, '.') || str_contains($pattern, '['); } - - private function startsWithPattern(string $pattern): bool - { - return 1 === preg_match('#^'.$this->convertShellToRegExPattern($pattern).'#', $this->value); - } - - private function convertShellToRegExPattern(string $pattern): string - { - return strtr($pattern, [ - '*' => '.*', - '?' => '.', - '.' => '\.', - '[!' => '[^', - '\\' => '\\\\', - ]); - } } diff --git a/tests/Unit/Analyzer/PatternStringTest.php b/tests/Unit/Analyzer/PatternStringTest.php index e26221d2..bc214901 100644 --- a/tests/Unit/Analyzer/PatternStringTest.php +++ b/tests/Unit/Analyzer/PatternStringTest.php @@ -19,6 +19,10 @@ public function test_wildcard_is_for_alphanumeric(): void { $pattern = new PatternString('SoThisIsAnExample'); $this->assertTrue($pattern->matches('*This*')); + $this->assertTrue($pattern->matches('*SoThisIsAnExample')); + $this->assertTrue($pattern->matches('*ThisIsAnExample')); + $this->assertFalse($pattern->matches('*This')); + $this->assertFalse($pattern->matches('*This')); $this->assertFalse($pattern->matches('This*')); } } From 0535e4639ab9e1f2e75c9a37c54f2fde5c9ed7c4 Mon Sep 17 00:00:00 2001 From: Herberto Graca Date: Sat, 26 Aug 2023 23:21:35 +0200 Subject: [PATCH 2/5] fixup! --- tests/Unit/Analyzer/PatternStringTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Unit/Analyzer/PatternStringTest.php b/tests/Unit/Analyzer/PatternStringTest.php index bc214901..ac4b7a1d 100644 --- a/tests/Unit/Analyzer/PatternStringTest.php +++ b/tests/Unit/Analyzer/PatternStringTest.php @@ -22,7 +22,6 @@ public function test_wildcard_is_for_alphanumeric(): void $this->assertTrue($pattern->matches('*SoThisIsAnExample')); $this->assertTrue($pattern->matches('*ThisIsAnExample')); $this->assertFalse($pattern->matches('*This')); - $this->assertFalse($pattern->matches('*This')); $this->assertFalse($pattern->matches('This*')); } } From b5273ebc8cc83c060c2dd3f91a7460e9749ed322 Mon Sep 17 00:00:00 2001 From: Herberto Graca Date: Sat, 26 Aug 2023 23:27:32 +0200 Subject: [PATCH 3/5] fixup! --- tests/Unit/Analyzer/PatternStringTest.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Analyzer/PatternStringTest.php b/tests/Unit/Analyzer/PatternStringTest.php index ac4b7a1d..6ff582f4 100644 --- a/tests/Unit/Analyzer/PatternStringTest.php +++ b/tests/Unit/Analyzer/PatternStringTest.php @@ -18,9 +18,14 @@ public function test_it_works_for_simple_strings(): void public function test_wildcard_is_for_alphanumeric(): void { $pattern = new PatternString('SoThisIsAnExample'); - $this->assertTrue($pattern->matches('*This*')); + $this->assertTrue($pattern->matches('SoThisIsAnExample')); + $this->assertTrue($pattern->matches('So????????Example')); $this->assertTrue($pattern->matches('*SoThisIsAnExample')); + $this->assertTrue($pattern->matches('SoThisIsAnExample*')); + $this->assertTrue($pattern->matches('So*Example')); $this->assertTrue($pattern->matches('*ThisIsAnExample')); + $this->assertTrue($pattern->matches('SoThisIsAn*')); + $this->assertTrue($pattern->matches('*This*')); $this->assertFalse($pattern->matches('*This')); $this->assertFalse($pattern->matches('This*')); } From a6d170cbe12d5bca6cae780749574b257c2d349b Mon Sep 17 00:00:00 2001 From: Herberto Graca Date: Tue, 5 Sep 2023 13:30:49 +0200 Subject: [PATCH 4/5] fixup! --- tests/Unit/Analyzer/PatternStringTest.php | 38 ++++++++++++++++------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/tests/Unit/Analyzer/PatternStringTest.php b/tests/Unit/Analyzer/PatternStringTest.php index 6ff582f4..09ef0dfa 100644 --- a/tests/Unit/Analyzer/PatternStringTest.php +++ b/tests/Unit/Analyzer/PatternStringTest.php @@ -15,18 +15,32 @@ public function test_it_works_for_simple_strings(): void $this->assertFalse($pattern->matches('Something else')); } - public function test_wildcard_is_for_alphanumeric(): void + /** + * @dataProvider providePatterns + */ + public function test_wildcard_is_for_alphanumeric(string $string, string $pattern, bool $expectedResult): void { - $pattern = new PatternString('SoThisIsAnExample'); - $this->assertTrue($pattern->matches('SoThisIsAnExample')); - $this->assertTrue($pattern->matches('So????????Example')); - $this->assertTrue($pattern->matches('*SoThisIsAnExample')); - $this->assertTrue($pattern->matches('SoThisIsAnExample*')); - $this->assertTrue($pattern->matches('So*Example')); - $this->assertTrue($pattern->matches('*ThisIsAnExample')); - $this->assertTrue($pattern->matches('SoThisIsAn*')); - $this->assertTrue($pattern->matches('*This*')); - $this->assertFalse($pattern->matches('*This')); - $this->assertFalse($pattern->matches('This*')); + $this->assertEquals($expectedResult, (new PatternString($string))->matches($pattern)); + } + + public function providePatterns(): array + { + return [ + ['SoThisIsAnExample', 'SoThisIsAnExample', true], + ['SoThisIsAnExample', 'So????????Example', true], + ['SoThisIsAnExample', '*SoThisIsAnExample', true], + ['SoThisIsAnExample', 'SoThisIsAnExample*', true], + ['SoThisIsAnExample', 'So*Example', true], + ['SoThisIsAnExample', '*ThisIsAnExample', true], + ['SoThisIsAnExample', 'SoThisIsAn*', true], + ['SoThisIsAnExample', '*This*', true], + ['SoThisIsAnExample', '*This', false], + ['SoThisIsAnExample', 'This*', false], + ['Food\Vegetables\Roots\Carrot', 'Food\*\Roots', false], + ['Food\Vegetables\Roots\Orange\Carrot', 'Food\*\Roots', false], + ['Food\Vegetables\Carrot', '*\Vegetables', false], + ['Food\Vegetables\Roots\Carrot', '*\Vegetables', false], + ['Food\Vegetables\Roots\Orange\Carrot', '*\Vegetables', false], + ]; } } From 7f6ae0df5c5b3d0db1057fa9abd00a4c07a93858 Mon Sep 17 00:00:00 2001 From: Herberto Graca Date: Tue, 5 Sep 2023 13:34:59 +0200 Subject: [PATCH 5/5] fixup! --- src/Expression/ForClasses/ResideInOneOfTheseNamespaces.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Expression/ForClasses/ResideInOneOfTheseNamespaces.php b/src/Expression/ForClasses/ResideInOneOfTheseNamespaces.php index 5048486d..f2bccc83 100644 --- a/src/Expression/ForClasses/ResideInOneOfTheseNamespaces.php +++ b/src/Expression/ForClasses/ResideInOneOfTheseNamespaces.php @@ -32,7 +32,7 @@ public function evaluate(ClassDescription $theClass, Violations $violations, str { $resideInNamespace = false; foreach ($this->namespaces as $namespace) { - if ($theClass->namespaceMatches($namespace)) { + if ($theClass->namespaceMatches($namespace.'*')) { $resideInNamespace = true; } }