$original) { foreach ($data[$temporary] ?? [] as $line => $hits) { $data[$original][$line] = max($hits, $data[$original][$line] ?? -2); } unset($data[$temporary]); } $root = realpath(dirname(__DIR__, 2)); $hashes = []; $htmlSpans = []; foreach ($data as $file => $lines) { if (!str_starts_with($file, $root . '/') || str_contains($file, '/vendor/')) { unset($data[$file]); continue; } $hashes[$file] = hash_file('sha256', $file); $htmlSpans[$file] = []; // Zend records a literal HTML output opcode at the end of its block. foreach (token_get_all(file_get_contents($file)) as $token) { if (is_array($token) && $token[0] === T_INLINE_HTML) { $htmlSpans[$file][] = [$token[2], $token[2] + substr_count($token[1], "\n")]; } } } if ($contract !== null) { $contract['hashes'] = []; foreach ($contract['producers'] as $producer) { $contract['hashes'][$producer] = hash_file('sha256', $root . '/' . $producer); } } $prefix = $contract !== null ? $contract['name'] . '-' : ''; $path = $requestCoverageDirectory . '/' . $prefix . getmypid() . '-' . bin2hex(random_bytes(8)) . '.json'; if (file_put_contents($path, json_encode(['root' => $root, 'coverage' => $data, 'hashes' => $hashes, 'html_spans' => $htmlSpans, 'contract' => $contract], JSON_THROW_ON_ERROR)) === false) { throw new RuntimeException('Could not preserve request coverage.'); } }); }