Skip to content

Commit

Permalink
Normalize the symbols registered (#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 11, 2022
1 parent 7129a81 commit 43584b6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
42 changes: 29 additions & 13 deletions src/Symbol/SymbolRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use function Safe\preg_match;
use function strtolower;
use function trim;
use const SORT_STRING;

final class SymbolRegistry
{
Expand All @@ -45,11 +44,8 @@ public static function create(
array $regexes = []
): self {
return new self(
array_map(
static fn (string $name) => strtolower(trim($name, '\\')),
$names,
),
array_unique($regexes, SORT_STRING),
self::normalizeNames($names),
array_unique($regexes),
false,
);
}
Expand All @@ -66,13 +62,8 @@ public static function createForConstants(
array $regexes = []
): self {
return new self(
array_map(
static fn (string $name) => self::lowerCaseConstantName(
trim($name, '\\'),
),
$names,
),
array_unique($regexes, SORT_STRING),
self::normalizeConstantNames($names),
array_unique($regexes),
true,
);
}
Expand Down Expand Up @@ -151,6 +142,31 @@ public function getRegexes(): array
return $this->regexes;
}

private static function normalizeNames(array $names): array
{
return array_map(
static fn (string $name) => strtolower(
self::normalizeName($name),
),
$names,
);
}

private static function normalizeConstantNames(array $names): array
{
return array_map(
static fn (string $name) => self::lowerCaseConstantName(
self::normalizeName($name),
),
$names,
);
}

private static function normalizeName(string $name): string
{
return trim($name, '\\ ');
}

/**
* Transforms the constant FQ name "Acme\Foo\X" to "acme\foo\X" since the
* namespace remains case-insensitive for constants regardless of whether
Expand Down
14 changes: 13 additions & 1 deletion tests/Symbol/SymbolRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static function provideSymbols(): iterable
$expected,
];

yield '[(polluted) name only] '.$title => [
yield '[(polluted with leading backslash) name only] '.$title => [
$names,
[],
'\\'.$symbol,
Expand Down Expand Up @@ -227,6 +227,18 @@ private static function provideNames(): iterable
'PHPUnit',
false,
];

yield 'name with extra spaces' => [
[' Pest '],
'Pest',
true,
];

yield 'name with extra backslashes' => [
['\\Pest\\'],
'Pest',
true,
];
}

private static function provideRegex(): iterable
Expand Down

0 comments on commit 43584b6

Please sign in to comment.