Skip to content

Commit 9aaafd8

Browse files
authored
Fix pattern match (#404)
1 parent 7690d57 commit 9aaafd8

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

src/Analyzer/PatternString.php

+1-17
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function matches(string $pattern): bool
2727
return $isInThisNamespace || $isThisClass;
2828
}
2929

30-
return $this->startsWithPattern($pattern);
30+
return fnmatch($pattern, $this->value, \FNM_NOESCAPE);
3131
}
3232

3333
public function toString(): string
@@ -43,20 +43,4 @@ private function containsWildcard(string $pattern): bool
4343
|| str_contains($pattern, '.')
4444
|| str_contains($pattern, '[');
4545
}
46-
47-
private function startsWithPattern(string $pattern): bool
48-
{
49-
return 1 === preg_match('#^'.$this->convertShellToRegExPattern($pattern).'#', $this->value);
50-
}
51-
52-
private function convertShellToRegExPattern(string $pattern): string
53-
{
54-
return strtr($pattern, [
55-
'*' => '.*',
56-
'?' => '.',
57-
'.' => '\.',
58-
'[!' => '[^',
59-
'\\' => '\\\\',
60-
]);
61-
}
6246
}

src/Expression/ForClasses/ResideInOneOfTheseNamespaces.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function evaluate(ClassDescription $theClass, Violations $violations, str
3232
{
3333
$resideInNamespace = false;
3434
foreach ($this->namespaces as $namespace) {
35-
if ($theClass->namespaceMatches($namespace)) {
35+
if ($theClass->namespaceMatches($namespace.'*')) {
3636
$resideInNamespace = true;
3737
}
3838
}

tests/Unit/Analyzer/PatternStringTest.php

+26-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,32 @@ public function test_it_works_for_simple_strings(): void
1515
$this->assertFalse($pattern->matches('Something else'));
1616
}
1717

18-
public function test_wildcard_is_for_alphanumeric(): void
18+
/**
19+
* @dataProvider providePatterns
20+
*/
21+
public function test_wildcard_is_for_alphanumeric(string $string, string $pattern, bool $expectedResult): void
1922
{
20-
$pattern = new PatternString('SoThisIsAnExample');
21-
$this->assertTrue($pattern->matches('*This*'));
22-
$this->assertFalse($pattern->matches('This*'));
23+
$this->assertEquals($expectedResult, (new PatternString($string))->matches($pattern));
24+
}
25+
26+
public function providePatterns(): array
27+
{
28+
return [
29+
['SoThisIsAnExample', '*This*', true],
30+
['SoThisIsAnExample', 'This*', false],
31+
['SoThisIsAnExample', '*This', false],
32+
['SoThisIsAnExample', 'SoThisIsAnExample', true],
33+
['SoThisIsAnExample', 'So????????Example', true],
34+
['SoThisIsAnExample', '*SoThisIsAnExample', true],
35+
['SoThisIsAnExample', 'SoThisIsAnExample*', true],
36+
['SoThisIsAnExample', 'So*Example', true],
37+
['SoThisIsAnExample', '*ThisIsAnExample', true],
38+
['SoThisIsAnExample', 'SoThisIsAn*', true],
39+
['Food\Vegetables\Roots\Carrot', 'Food\*\Roots', false],
40+
['Food\Vegetables\Roots\Orange\Carrot', 'Food\*\Roots', false],
41+
['Food\Vegetables\Carrot', '*\Vegetables', false],
42+
['Food\Vegetables\Roots\Carrot', '*\Vegetables', false],
43+
['Food\Vegetables\Roots\Orange\Carrot', '*\Vegetables', false],
44+
];
2345
}
2446
}

0 commit comments

Comments
 (0)