Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pattern match #404

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions src/Analyzer/PatternString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, [
'*' => '.*',
'?' => '.',
'.' => '\.',
'[!' => '[^',
'\\' => '\\\\',
]);
}
}
2 changes: 1 addition & 1 deletion src/Expression/ForClasses/ResideInOneOfTheseNamespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
30 changes: 26 additions & 4 deletions tests/Unit/Analyzer/PatternStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '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],
];
}
}
Loading