Optimize buildFilterArgument in Shard plugin for compact regex generation and add comprehensive tests (#1675)

This commit is contained in:
oddvalue
2026-06-12 20:06:50 +01:00
committed by GitHub
parent 0d7814ca16
commit d393799d2a
2 changed files with 124 additions and 1 deletions

View File

@ -225,7 +225,38 @@ final class Shard implements AddsOutput, HandlesArguments, Terminable
*/
private function buildFilterArgument(array $testsToRun): string
{
return addslashes(implode('|', $testsToRun));
if ($testsToRun === []) {
return '';
}
/** @var array<string, mixed> $tree */
$tree = [];
foreach ($testsToRun as $class) {
$parts = explode('\\', $class);
$current = &$tree;
foreach ($parts as $part) {
if (! isset($current[$part])) {
$current[$part] = [];
}
$current = &$current[$part];
}
}
$buildRegex = function (array $tree) use (&$buildRegex): string {
$parts = [];
foreach ($tree as $key => $sub) {
$subRegex = $buildRegex($sub);
if ($subRegex === '') {
$parts[] = preg_quote($key, '/');
} else {
$parts[] = preg_quote($key, '/').'\\\\'.(count($sub) > 1 ? '('.$subRegex.')' : $subRegex);
}
}
return implode('|', $parts);
};
return $buildRegex($tree);
}
/**