|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypePHP\Internal\Cli; |
| 6 | + |
| 7 | +use TypePHP\Internal\Reporting\ViolationRecord; |
| 8 | + |
| 9 | +/** |
| 10 | + * @internal Merges multiple JSON report files into a unified report with aggregated occurrence counts. |
| 11 | + */ |
| 12 | +final class ReportMergeCommand implements CommandInterface |
| 13 | +{ |
| 14 | + public function execute(array $args, $outputStream = STDOUT, $errorStream = STDERR): int |
| 15 | + { |
| 16 | + $c = [CliFormatter::class, 'color']; |
| 17 | + |
| 18 | + $inputFiles = []; |
| 19 | + $outputFile = null; |
| 20 | + |
| 21 | + for ($i = 0; $i < \count($args); $i++) { |
| 22 | + $arg = $args[$i]; |
| 23 | + |
| 24 | + if ($arg === 'report:merge') { |
| 25 | + continue; |
| 26 | + } |
| 27 | + |
| 28 | + if (str_starts_with($arg, '--output=')) { |
| 29 | + $outputFile = substr($arg, 9); |
| 30 | + |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + if ($arg === '-o' && isset($args[$i + 1])) { |
| 35 | + $outputFile = $args[$i + 1]; |
| 36 | + $i++; |
| 37 | + |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + if (! str_starts_with($arg, '-')) { |
| 42 | + $inputFiles[] = $arg; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if ($inputFiles === []) { |
| 47 | + fwrite($errorStream, "\n " . $c(' TYPEPHP ', 'badge_red') . ' ' . $c('Error', 'bold') . "\n\n"); |
| 48 | + fwrite($errorStream, ' ' . $c('✗', 'red') . " No input report files specified.\n\n"); |
| 49 | + fwrite($errorStream, ' ' . $c('Usage:', 'yellow') . "\n"); |
| 50 | + fwrite($errorStream, " vendor/bin/typephp report:merge <file1.json> <file2.json> [--output=<merged.json>]\n\n"); |
| 51 | + |
| 52 | + return 1; |
| 53 | + } |
| 54 | + |
| 55 | + /** @var array<string, ViolationRecord> $mergedViolations */ |
| 56 | + $mergedViolations = []; |
| 57 | + $validFileCount = 0; |
| 58 | + |
| 59 | + foreach ($inputFiles as $file) { |
| 60 | + if (! file_exists($file)) { |
| 61 | + fwrite($errorStream, ' ' . $c('!', 'yellow') . ' Warning: Report file ' . $c('"' . $file . '"', 'bold') . " not found, skipping.\n"); |
| 62 | + |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + $rawContent = @file_get_contents($file); |
| 67 | + if ($rawContent === false || trim($rawContent) === '') { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + /** @var array{violations?: list<array<string, mixed>>}|null $data */ |
| 72 | + $data = json_decode($rawContent, true); |
| 73 | + if (! \is_array($data) || ! isset($data['violations']) || ! \is_array($data['violations'])) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + $validFileCount++; |
| 78 | + |
| 79 | + foreach ($data['violations'] as $v) { |
| 80 | + if (\is_array($v)) { |
| 81 | + $record = ViolationRecord::fromArray($v); |
| 82 | + if ($record !== null) { |
| 83 | + $hash = $record->getHash(); |
| 84 | + if (isset($mergedViolations[$hash])) { |
| 85 | + $mergedViolations[$hash] = $mergedViolations[$hash]->withIncrementedCount($record->count); |
| 86 | + } else { |
| 87 | + $mergedViolations[$hash] = $record; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + $violationsList = array_values($mergedViolations); |
| 95 | + $filesAffected = []; |
| 96 | + foreach ($violationsList as $v) { |
| 97 | + $filesAffected[$v->file] = true; |
| 98 | + } |
| 99 | + |
| 100 | + $document = [ |
| 101 | + 'version' => '1.0', |
| 102 | + 'generated_at' => gmdate('c'), |
| 103 | + 'summary' => [ |
| 104 | + 'total_violations' => \count($violationsList), |
| 105 | + 'files_affected' => \count($filesAffected), |
| 106 | + ], |
| 107 | + 'violations' => $violationsList, |
| 108 | + ]; |
| 109 | + |
| 110 | + $json = json_encode($document, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| 111 | + |
| 112 | + if ($outputFile !== null && $json !== false) { |
| 113 | + $outputDir = \dirname($outputFile); |
| 114 | + if (! is_dir($outputDir)) { |
| 115 | + @mkdir($outputDir, 0777, true); |
| 116 | + } |
| 117 | + |
| 118 | + file_put_contents($outputFile, $json); |
| 119 | + |
| 120 | + fwrite($outputStream, "\n " . $c(' TYPEPHP ', 'badge') . ' ' . $c('Report Merge', 'bold') . "\n\n"); |
| 121 | + fwrite($outputStream, ' ' . $c('✓', 'green') . ' Merged ' . $c((string) $validFileCount, 'bold') . ' report file(s) into ' . $c('"' . $outputFile . '"', 'bold') . ' (' . $c((string) \count($violationsList), 'bold') . " unique violation(s)).\n\n"); |
| 122 | + |
| 123 | + return 0; |
| 124 | + } |
| 125 | + |
| 126 | + if ($json !== false) { |
| 127 | + fwrite($outputStream, $json . "\n"); |
| 128 | + } |
| 129 | + |
| 130 | + return 0; |
| 131 | + } |
| 132 | +} |
0 commit comments