1<?php
2
3if ($argc >= 2) {
4    $corpusDir = $argv[1];
5} else {
6    $corpusDir = __DIR__ . '/corpus/execute';
7}
8if ($argc >= 3) {
9    $testDirs = array_slice($argv, 2);
10} else {
11    $baseDir = __DIR__ . '/../..';
12    $testDirs = ["$baseDir/Zend/tests", "$baseDir/ext/reflection"];
13}
14
15$maxLen = 8 * 1024;
16@mkdir($corpusDir);
17
18foreach ($testDirs as $testDir) {
19    $it = new RecursiveIteratorIterator(
20        new RecursiveDirectoryIterator($testDir),
21        RecursiveIteratorIterator::LEAVES_ONLY
22    );
23
24    foreach ($it as $file) {
25        if (!preg_match('/\.phpt$/', $file)) continue;
26        $fullCode = file_get_contents($file);
27        if (!preg_match('/--FILE--\R(.*?)\R--([_A-Z]+)--/s', $fullCode, $matches)) continue;
28        $code = $matches[1];
29        if (strlen($code) > $maxLen) continue;
30
31        $outFile = str_replace($testDir, '', $file);
32        $outFile = str_replace('/', '_', $outFile);
33        if (!preg_match('/SKIP_SLOW_TESTS|SKIP_PERF_SENSITIVE|USE_ZEND_ALLOC/', $fullCode)) {
34            file_put_contents($corpusDir . '/' . $outFile, $code);
35        }
36    }
37}
38