1<?php
2
3function generate_corpus_from_phpt(string $corpusDir, array $testDirs, int $maxLen = 8 * 1024) {
4    @mkdir($corpusDir);
5    foreach ($testDirs as $testDir) {
6        $it = new RecursiveIteratorIterator(
7            new RecursiveDirectoryIterator($testDir),
8            RecursiveIteratorIterator::LEAVES_ONLY
9        );
10
11        foreach ($it as $file) {
12            if (!preg_match('/\.phpt$/', $file)) continue;
13            $fullCode = file_get_contents($file);
14            if (!preg_match('/--FILE--\R(.*?)\R--([_A-Z]+)--/s', $fullCode, $matches)) continue;
15            $code = $matches[1];
16            if (strlen($code) > $maxLen) continue;
17
18            $outFile = str_replace($testDir, '', $file);
19            $outFile = str_replace('/', '_', $outFile);
20            if (!preg_match('/SKIP_SLOW_TESTS|SKIP_PERF_SENSITIVE|USE_ZEND_ALLOC/', $fullCode)) {
21                file_put_contents($corpusDir . '/' . $outFile, $code);
22            }
23        }
24    }
25}
26