Skip to content

Commit

Permalink
Fix regex cannot have the i flag (#759)
Browse files Browse the repository at this point in the history
If the regex had the `i` flag it would have been appended still resulting in an invalid regex.
  • Loading branch information
theofidry authored Nov 12, 2022
1 parent 9d7eddb commit 68a562c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/Configuration/SymbolsConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
use function is_bool;
use function is_string;
use function sprintf;
use function str_contains;
use function strrpos;
use function substr;

final class SymbolsConfigurationFactory
{
Expand Down Expand Up @@ -203,12 +206,31 @@ private function getRegex(string $regex, string $key, int|string $index): string
);
}

// TODO: double check that we are not adding it twice or that adding it twice does not break anything
$regex .= 'i';
$flags = self::getRegexFlags($regex);

if (!str_contains($flags, 'i')) {
// Ensure namespace comparisons are always case-insensitive
$regex .= 'i';
}

return $regex;
}

/**
* @param non-empty-string $regex
*/
private static function getRegexFlags(string $regex): string
{
$separator = $regex[0];
$lastSeparatorPosition = strrpos($regex, $separator);

if (false === $lastSeparatorPosition) {
return '';
}

return substr($regex, $lastSeparatorPosition);
}

/**
* @psalm-assert string[] $value
*/
Expand Down Expand Up @@ -240,6 +262,9 @@ private static function assertIsArrayOfStrings(mixed $value, string $key): void
}
}

/**
* @phpstan-assert non-empty-string $regex
*/
private function assertValidRegex(string $regex, string $key, string $index): void
{
$errorMessage = $this->regexChecker->validateRegex($regex);
Expand Down
42 changes: 42 additions & 0 deletions tests/Configuration/SymbolsConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,48 @@ public static function configProvider(): iterable
),
];

yield 'exclude namespace regex with flags' => [
[
ConfigurationKeys::EXCLUDE_NAMESPACES_KEYWORD => [
'~^PHPUnit\\Runner(\\.*)?$~u',
],
],
SymbolsConfiguration::create(
excludedNamespaces: NamespaceRegistry::create(
[],
['~^PHPUnit\\Runner(\\.*)?$~ui'],
),
),
];

yield 'exclude namespace regex with case insensitive flag' => [
[
ConfigurationKeys::EXCLUDE_NAMESPACES_KEYWORD => [
'~^PHPUnit\\Runner(\\.*)?$~i',
],
],
SymbolsConfiguration::create(
excludedNamespaces: NamespaceRegistry::create(
[],
['~^PHPUnit\\Runner(\\.*)?$~i'],
),
),
];

yield 'exclude namespace regex with several flags flag' => [
[
ConfigurationKeys::EXCLUDE_NAMESPACES_KEYWORD => [
'~^PHPUnit\\Runner(\\.*)?$~uiA',
],
],
SymbolsConfiguration::create(
excludedNamespaces: NamespaceRegistry::create(
[],
['~^PHPUnit\\Runner(\\.*)?$~uiA'],
),
),
];

yield 'nominal' => [
[
ConfigurationKeys::EXPOSE_GLOBAL_CONSTANTS_KEYWORD => false,
Expand Down

0 comments on commit 68a562c

Please sign in to comment.