Skip to content

Commit 979b032

Browse files
committed
[run-tests.php] Improve non-optimal nested if/elseif/else blocks with happy path optimizations
Simplifies and improves the readability of multiple `if`/`elseif`/`else` blocks by removing them when an earlier branch exists the execution flow by either returning or terminating the script.
1 parent e4c8949 commit 979b032

File tree

1 file changed

+53
-59
lines changed

1 file changed

+53
-59
lines changed

run-tests.php

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ function main(): void
228228
// fail to reattach to the OpCache because it will be using the
229229
// wrong path.
230230
die("TEMP environment is NOT set");
231-
} else {
232-
if (count($environment) == 1) {
233-
// Not having other environment variables, only having TEMP, is
234-
// probably ok, but strange and may make a difference in the
235-
// test pass rate, so warn the user.
236-
echo "WARNING: Only 1 environment variable will be available to tests(TEMP environment variable)" . PHP_EOL;
237-
}
231+
}
232+
233+
if (count($environment) == 1) {
234+
// Not having other environment variables, only having TEMP, is
235+
// probably ok, but strange and may make a difference in the
236+
// test pass rate, so warn the user.
237+
echo "WARNING: Only 1 environment variable will be available to tests(TEMP environment variable)" . PHP_EOL;
238238
}
239239
}
240240

@@ -436,10 +436,8 @@ function main(): void
436436
$matches = [];
437437
if (preg_match('/^#.*\[(.*)\]\:\s+(.*)$/', $test, $matches)) {
438438
$redir_tests[] = [$matches[1], $matches[2]];
439-
} else {
440-
if (strlen($test)) {
441-
$test_files[] = trim($test);
442-
}
439+
} elseif (strlen($test)) {
440+
$test_files[] = trim($test);
443441
}
444442
}
445443
}
@@ -624,27 +622,21 @@ function main(): void
624622
if (!$testfile && strpos($argv[$i], '*') !== false && function_exists('glob')) {
625623
if (substr($argv[$i], -5) == '.phpt') {
626624
$pattern_match = glob($argv[$i]);
625+
} elseif (preg_match("/\*$/", $argv[$i])) {
626+
$pattern_match = glob($argv[$i] . '.phpt');
627627
} else {
628-
if (preg_match("/\*$/", $argv[$i])) {
629-
$pattern_match = glob($argv[$i] . '.phpt');
630-
} else {
631-
die('Cannot find test file "' . $argv[$i] . '".' . PHP_EOL);
632-
}
628+
die('Cannot find test file "' . $argv[$i] . '".' . PHP_EOL);
633629
}
634630

635631
if (is_array($pattern_match)) {
636632
$test_files = array_merge($test_files, $pattern_match);
637633
}
634+
} elseif (is_dir($testfile)) {
635+
find_files($testfile);
636+
} elseif (substr($testfile, -5) == '.phpt') {
637+
$test_files[] = $testfile;
638638
} else {
639-
if (is_dir($testfile)) {
640-
find_files($testfile);
641-
} else {
642-
if (substr($testfile, -5) == '.phpt') {
643-
$test_files[] = $testfile;
644-
} else {
645-
die('Cannot find test file "' . $argv[$i] . '".' . PHP_EOL);
646-
}
647-
}
639+
die('Cannot find test file "' . $argv[$i] . '".' . PHP_EOL);
648640
}
649641
}
650642
}
@@ -1085,9 +1077,9 @@ function test_name($name): string
10851077
{
10861078
if (is_array($name)) {
10871079
return $name[0] . ':' . $name[1];
1088-
} else {
1089-
return $name;
10901080
}
1081+
1082+
return $name;
10911083
}
10921084
/**
10931085
* @param array|string $a
@@ -1105,9 +1097,9 @@ function test_sort($a, $b): int
11051097

11061098
if ($ta == $tb) {
11071099
return strcmp($a, $b);
1108-
} else {
1109-
return $tb - $ta;
11101100
}
1101+
1102+
return $tb - $ta;
11111103
}
11121104

11131105
//
@@ -1118,10 +1110,8 @@ function save_text(string $filename, string $text, ?string $filename_copy = null
11181110
{
11191111
global $DETAILED;
11201112

1121-
if ($filename_copy && $filename_copy != $filename) {
1122-
if (file_put_contents($filename_copy, $text) === false) {
1123-
error("Cannot open file '" . $filename_copy . "' (save_text)");
1124-
}
1113+
if ($filename_copy && $filename_copy != $filename && file_put_contents($filename_copy, $text) === false) {
1114+
error("Cannot open file '" . $filename_copy . "' (save_text)");
11251115
}
11261116

11271117
if (file_put_contents($filename, $text) === false) {
@@ -1214,12 +1204,16 @@ function system_with_timeout(
12141204

12151205
if ($n === false) {
12161206
break;
1217-
} elseif ($n === 0) {
1207+
}
1208+
1209+
if ($n === 0) {
12181210
/* timed out */
12191211
$data .= "\n ** ERROR: process timed out **\n";
12201212
proc_terminate($proc, 9);
12211213
return $data;
1222-
} elseif ($n > 0) {
1214+
}
1215+
1216+
if ($n > 0) {
12231217
if ($captureStdOut) {
12241218
$line = fread($pipes[1], 8192);
12251219
} elseif ($captureStdErr) {
@@ -2201,17 +2195,17 @@ function run_test(string $php, $file, array $env): string
22012195

22022196
$junit->markTestAs('PASS', $shortname, $tested);
22032197
return 'REDIR';
2204-
} else {
2205-
$bork_info = "Redirect info must contain exactly one TEST string to be used as redirect directory.";
2206-
show_result("BORK", $bork_info, '', '', $temp_filenames);
2207-
$PHP_FAILED_TESTS['BORKED'][] = [
2208-
'name' => $file,
2209-
'test_name' => '',
2210-
'output' => '',
2211-
'diff' => '',
2212-
'info' => "$bork_info [$file]",
2213-
];
22142198
}
2199+
2200+
$bork_info = "Redirect info must contain exactly one TEST string to be used as redirect directory.";
2201+
show_result("BORK", $bork_info, '', '', $temp_filenames);
2202+
$PHP_FAILED_TESTS['BORKED'][] = [
2203+
'name' => $file,
2204+
'test_name' => '',
2205+
'output' => '',
2206+
'diff' => '',
2207+
'info' => "$bork_info [$file]",
2208+
];
22152209
}
22162210

22172211
if (is_array($org_file) || $test->hasSection('REDIRECTTEST')) {
@@ -2776,9 +2770,9 @@ function comp_line(string $l1, string $l2, bool $is_reg)
27762770
{
27772771
if ($is_reg) {
27782772
return preg_match('/^' . $l1 . '$/s', $l2);
2779-
} else {
2780-
return !strcmp($l1, $l2);
27812773
}
2774+
2775+
return !strcmp($l1, $l2);
27822776
}
27832777

27842778
function count_array_diff(
@@ -2854,20 +2848,20 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
28542848
$idx1++;
28552849
$idx2++;
28562850
continue;
2857-
} else {
2858-
$c1 = @count_array_diff($ar1, $ar2, $is_reg, $w, $idx1 + 1, $idx2, $cnt1, $cnt2, 10);
2859-
$c2 = @count_array_diff($ar1, $ar2, $is_reg, $w, $idx1, $idx2 + 1, $cnt1, $cnt2, 10);
2851+
}
28602852

2861-
if ($c1 > $c2) {
2862-
$old1[$idx1] = sprintf("{$line_number_spec}- ", $idx1 + 1) . $w[$idx1++];
2863-
} elseif ($c2 > 0) {
2864-
$old2[$idx2] = sprintf("{$line_number_spec}+ ", $idx2 + 1) . $ar2[$idx2++];
2865-
} else {
2866-
$old1[$idx1] = sprintf("{$line_number_spec}- ", $idx1 + 1) . $w[$idx1++];
2867-
$old2[$idx2] = sprintf("{$line_number_spec}+ ", $idx2 + 1) . $ar2[$idx2++];
2868-
}
2869-
$last_printed_context_line = $idx1;
2853+
$c1 = @count_array_diff($ar1, $ar2, $is_reg, $w, $idx1 + 1, $idx2, $cnt1, $cnt2, 10);
2854+
$c2 = @count_array_diff($ar1, $ar2, $is_reg, $w, $idx1, $idx2 + 1, $cnt1, $cnt2, 10);
2855+
2856+
if ($c1 > $c2) {
2857+
$old1[$idx1] = sprintf("{$line_number_spec}- ", $idx1 + 1) . $w[$idx1++];
2858+
} elseif ($c2 > 0) {
2859+
$old2[$idx2] = sprintf("{$line_number_spec}+ ", $idx2 + 1) . $ar2[$idx2++];
2860+
} else {
2861+
$old1[$idx1] = sprintf("{$line_number_spec}- ", $idx1 + 1) . $w[$idx1++];
2862+
$old2[$idx2] = sprintf("{$line_number_spec}+ ", $idx2 + 1) . $ar2[$idx2++];
28702863
}
2864+
$last_printed_context_line = $idx1;
28712865
}
28722866
$mapping[$idx2] = $idx1;
28732867

0 commit comments

Comments
 (0)