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 regex cannot have the i flag #759

Merged
merged 2 commits into from
Nov 12, 2022
Merged
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
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