Skip to content

Commit 3483a1f

Browse files
Ayeshmvorisek
authored andcommitted
[run-tests.php] Combine multiple str_replace calls to a single strtr call
Makes the replacement easier to see, neatly aligned, and only takes one function call. This is safe because none of the combined replacement values contain tokens that would be recursively replaced. This also improves the readability on how the regular expressions in `EXPECTF` matcher is constructed. Co-authored-by: Michael Voříšek <[email protected]>
1 parent c83a10d commit 3483a1f

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

run-tests.php

+17-15
Original file line numberDiff line numberDiff line change
@@ -2567,21 +2567,23 @@ function run_test(string $php, $file, array $env): string
25672567
$wanted_re = $temp;
25682568

25692569
// Stick to basics
2570-
$wanted_re = str_replace('%e', '\\' . DIRECTORY_SEPARATOR, $wanted_re);
2571-
$wanted_re = str_replace('%s', '[^\r\n]+', $wanted_re);
2572-
$wanted_re = str_replace('%S', '[^\r\n]*', $wanted_re);
2573-
$wanted_re = str_replace('%a', '.+', $wanted_re);
2574-
$wanted_re = str_replace('%A', '.*', $wanted_re);
2575-
$wanted_re = str_replace('%w', '\s*', $wanted_re);
2576-
$wanted_re = str_replace('%i', '[+-]?\d+', $wanted_re);
2577-
$wanted_re = str_replace('%d', '\d+', $wanted_re);
2578-
$wanted_re = str_replace('%x', '[0-9a-fA-F]+', $wanted_re);
2579-
$wanted_re = str_replace('%f', '[+-]?(?:\d+|(?=\.\d))(?:\.\d+)?(?:[Ee][+-]?\d+)?', $wanted_re);
2580-
$wanted_re = str_replace('%c', '.', $wanted_re);
2581-
$wanted_re = str_replace('%0', '\x00', $wanted_re);
2582-
}
2583-
2584-
if (preg_match("/^$wanted_re\$/s", $output)) {
2570+
$wanted_re = strtr($wanted_re, [
2571+
'%e' => preg_quote(DIRECTORY_SEPARATOR, '/'),
2572+
'%s' => '[^\r\n]+',
2573+
'%S' => '[^\r\n]*',
2574+
'%a' => '.+',
2575+
'%A' => '.*',
2576+
'%w' => '\s*',
2577+
'%i' => '[+-]?\d+',
2578+
'%d' => '\d+',
2579+
'%x' => '[0-9a-fA-F]+',
2580+
'%f' => '[+-]?(?:\d+|(?=\.\d))(?:\.\d+)?(?:[Ee][+-]?\d+)?',
2581+
'%c' => '.',
2582+
'%0' => '\x00',
2583+
]);
2584+
}
2585+
2586+
if (preg_match('/^' . $wanted_re . '$/s', $output)) {
25852587
$passed = true;
25862588
}
25872589
} else {

0 commit comments

Comments
 (0)