diff --git a/src/Symbol/SymbolRegistry.php b/src/Symbol/SymbolRegistry.php index 84f3743e..107ff989 100644 --- a/src/Symbol/SymbolRegistry.php +++ b/src/Symbol/SymbolRegistry.php @@ -27,7 +27,6 @@ use function Safe\preg_match; use function strtolower; use function trim; -use const SORT_STRING; final class SymbolRegistry { @@ -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, ); } @@ -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, ); } @@ -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 diff --git a/tests/Symbol/SymbolRegistryTest.php b/tests/Symbol/SymbolRegistryTest.php index 70fdc3e7..a5e74a29 100644 --- a/tests/Symbol/SymbolRegistryTest.php +++ b/tests/Symbol/SymbolRegistryTest.php @@ -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, @@ -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