|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php declare(strict_types=1); |
| 3 | + |
| 4 | +use Salient\Cli\CliApplication; |
| 5 | +use Salient\Utility\Exception\FilesystemErrorException; |
| 6 | +use SebastianBergmann\CodeCoverage\Node\Directory; |
| 7 | +use SebastianBergmann\CodeCoverage\CodeCoverage; |
| 8 | + |
| 9 | +require dirname(__DIR__) . '/vendor/autoload.php'; |
| 10 | + |
| 11 | +$app = new CliApplication(dirname(__DIR__)); |
| 12 | + |
| 13 | +/** @var string[] */ |
| 14 | +$args = $_SERVER['argv']; |
| 15 | +$file = $args[1] ?? dirname(__DIR__) . '/build/coverage.php'; |
| 16 | +if (!is_file($file)) { |
| 17 | + throw new FilesystemErrorException(sprintf('File not found: %s', $file)); |
| 18 | +} |
| 19 | + |
| 20 | +/** @var CodeCoverage */ |
| 21 | +$coverage = require ($file); |
| 22 | + |
| 23 | +// [ Directory => [ name, API state, `Contract` state, code state ], ... ] |
| 24 | +$components = [ |
| 25 | + 'Contract' => ['Contracts', false, false, false], // 2025-02-22: top-level interfaces finalised |
| 26 | + 'Utility' => ['Utils', true, false, '2025-01-28: code review finalised'], |
| 27 | + 'Polyfill' => ['Polyfills', false, true, '2025-03-04: code review finalised'], |
| 28 | + 'Collection' => ['Collections', true, true, '2025-02-23: code review finalised'], |
| 29 | + 'Core' => ['Core', true, true, '2025-02-14: code review finalised, replacement of `Legacy` classes still pending'], |
| 30 | + 'Iterator' => ['Iterators', true, true, '2025-02-26: code review finalised'], |
| 31 | + 'Cache' => ['Cache', true, true, '2025-02-23: code review finalised'], |
| 32 | + 'Console' => ['Console', null, null, null], |
| 33 | + 'Container' => ['Container', null, null, null], |
| 34 | + 'Http' => ['HTTP', null, null, null], |
| 35 | + 'Db' => ['Db', null, null, null], |
| 36 | + 'Cli' => ['CLI', null, null, null], |
| 37 | + 'Sync' => ['Sync', null, null, null], |
| 38 | + 'Curler' => ['Curler', true, true, '2025-02-24: code review finalised'], |
| 39 | + 'PHPDoc' => ['PHPDoc', null, false, null], |
| 40 | + 'PHPStan' => ['PHPStan', false, false, '2025-03-04: code review finalised'], |
| 41 | + 'Testing' => ['Testing', true, false, '2025-03-04: code review finalised'], |
| 42 | + 'Sli' => ['Sli', null, false, null], |
| 43 | +]; |
| 44 | + |
| 45 | +/** |
| 46 | + * @return array{coverage:float,executed:int,executable:int,code:int,comments:int,total:int} |
| 47 | + */ |
| 48 | +function getLines(Directory $dir): array |
| 49 | +{ |
| 50 | + $lines = $dir->linesOfCode(); |
| 51 | + $lines = [ |
| 52 | + 'coverage' => 1.0, |
| 53 | + 'executed' => $dir->numberOfExecutedLines(), |
| 54 | + 'executable' => $dir->numberOfExecutableLines(), |
| 55 | + 'code' => $lines['nonCommentLinesOfCode'], |
| 56 | + 'comments' => $lines['commentLinesOfCode'], |
| 57 | + 'total' => $lines['linesOfCode'], |
| 58 | + ]; |
| 59 | + if ($lines['executable'] > 0) { |
| 60 | + $lines['coverage'] = $lines['executed'] / $lines['executable']; |
| 61 | + } |
| 62 | + return $lines; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * @param non-empty-array<non-empty-array<string|int>> $table |
| 67 | + */ |
| 68 | +function printTable(array $table): void |
| 69 | +{ |
| 70 | + foreach ($table as $row) { |
| 71 | + foreach ($row as $i => $column) { |
| 72 | + $widths[$i] = max($widths[$i] ?? 0, mb_strlen((string) $column)); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + $row = []; |
| 77 | + foreach ($widths as $width) { |
| 78 | + $row[] = str_repeat('-', $width); |
| 79 | + } |
| 80 | + array_splice($table, 1, 0, [$row]); |
| 81 | + |
| 82 | + foreach ($table as $row) { |
| 83 | + printf('|'); |
| 84 | + foreach ($row as $i => $column) { |
| 85 | + printf( |
| 86 | + ' %s%s |', |
| 87 | + $column, |
| 88 | + str_repeat(' ', $widths[$i] - mb_strlen((string) $column)), |
| 89 | + ); |
| 90 | + } |
| 91 | + printf("\n"); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +$report = $coverage->getReport(); |
| 96 | +$totalLines = getLines($report); |
| 97 | +$dirLines = []; |
| 98 | +/** @var Directory $dir */ |
| 99 | +foreach ($report->directories() as $dir) { |
| 100 | + $dirLines[$dir->name()] = getLines($dir); |
| 101 | +} |
| 102 | + |
| 103 | +$data = [['Component', 'Size', 'Coverage', 'Lines', 'Code', 'Executable', 'API?', 'Contracts?', 'Code?']]; |
| 104 | +$totalSize = 0; |
| 105 | +$progress = [0, 0, 0]; |
| 106 | +$expectedProgress = [0, 0, 0]; |
| 107 | +foreach ($components as $dir => $component) { |
| 108 | + $lines = $dirLines[$dir]; |
| 109 | + $name = array_shift($component); |
| 110 | + $size = $lines['code'] / $totalLines['code']; |
| 111 | + $totalSize += $size; |
| 112 | + $row = [ |
| 113 | + $name, |
| 114 | + sprintf('%.2f%%', $size * 100), |
| 115 | + sprintf('%.2f%%', $lines['coverage'] * 100), |
| 116 | + $lines['total'], |
| 117 | + $lines['code'], |
| 118 | + $lines['executable'], |
| 119 | + ]; |
| 120 | + $size = $lines['total'] / $totalLines['total']; |
| 121 | + foreach ($component as $i => $state) { |
| 122 | + $row[] = $state === null |
| 123 | + ? '✘' |
| 124 | + : ($state === false |
| 125 | + ? '-' |
| 126 | + : '✔'); |
| 127 | + if ($state !== false) { |
| 128 | + $expectedProgress[$i] += $size; |
| 129 | + if ($state !== null) { |
| 130 | + $progress[$i] += $size; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + $data[] = $row; |
| 135 | +} |
| 136 | + |
| 137 | +$row = [ |
| 138 | + 'TOTAL', |
| 139 | + sprintf('%.2f%%', $totalSize * 100), |
| 140 | + sprintf('%.2f%%', $totalLines['coverage'] * 100), |
| 141 | + $totalLines['total'], |
| 142 | + $totalLines['code'], |
| 143 | + $totalLines['executable'], |
| 144 | +]; |
| 145 | +foreach ($progress as $i => $actualProgress) { |
| 146 | + $row[] = sprintf('%.2f%%', $actualProgress * 100 / $expectedProgress[$i]); |
| 147 | +} |
| 148 | +$data[] = $row; |
| 149 | + |
| 150 | +printTable($data); |
0 commit comments