From 1d91538dc6d049ce8cd4ed7326b6063696a74841 Mon Sep 17 00:00:00 2001 From: Herberto Graca Date: Mon, 7 Aug 2023 21:41:29 +0200 Subject: [PATCH] Fix pattern match --- src/Analyzer/PatternString.php | 18 +---------- .../ResideInOneOfTheseNamespaces.php | 2 +- tests/Unit/Analyzer/PatternStringTest.php | 30 ++++++++++++++++--- 3 files changed, 28 insertions(+), 22 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/src/Expression/ForClasses/ResideInOneOfTheseNamespaces.php b/src/Expression/ForClasses/ResideInOneOfTheseNamespaces.php index 4b5978ba..67fe5d1b 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; } } diff --git a/tests/Unit/Analyzer/PatternStringTest.php b/tests/Unit/Analyzer/PatternStringTest.php index e26221d2..f566a3ec 100644 --- a/tests/Unit/Analyzer/PatternStringTest.php +++ b/tests/Unit/Analyzer/PatternStringTest.php @@ -15,10 +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('*This*')); - $this->assertFalse($pattern->matches('This*')); + $this->assertEquals($expectedResult, (new PatternString($string))->matches($pattern)); + } + + public function providePatterns(): array + { + return [ + ['SoThisIsAnExample', '*This*', true], + ['SoThisIsAnExample', 'This*', false], + ['SoThisIsAnExample', '*This', false], + ['SoThisIsAnExample', 'SoThisIsAnExample', true], + ['SoThisIsAnExample', 'So????????Example', true], + ['SoThisIsAnExample', '*SoThisIsAnExample', true], + ['SoThisIsAnExample', 'SoThisIsAnExample*', true], + ['SoThisIsAnExample', 'So*Example', true], + ['SoThisIsAnExample', '*ThisIsAnExample', true], + ['SoThisIsAnExample', 'SoThisIsAn*', true], + ['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], + ]; } }