|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * This file is part of phpunit/php-code-coverage. |
| 4 | + * |
| 5 | + * (c) Sebastian Bergmann <sebastian@phpunit.de> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | +namespace SebastianBergmann\CodeCoverage\Report; |
| 11 | + |
| 12 | +use function dirname; |
| 13 | +use function file_put_contents; |
| 14 | +use lcov\{FunctionCoverage, LineCoverage, LineData, Record, Report}; |
| 15 | +use SebastianBergmann\CodeCoverage\Directory; |
| 16 | +use SebastianBergmann\CodeCoverage\Driver\WriteOperationFailedException; |
| 17 | +use SebastianBergmann\CodeCoverage\Node\File; |
| 18 | + |
| 19 | +final class Lcov |
| 20 | +{ |
| 21 | + public function process(CodeCoverage $coverage, ?string $target = null, ?string $name = null): string |
| 22 | + { |
| 23 | + $report = $coverage->getReport(); |
| 24 | + |
| 25 | + $records = []; |
| 26 | + foreach ($report as $item) { |
| 27 | + if (!$item instanceof File) { |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + $coverageData = $item->lineCoverageData(); |
| 32 | + |
| 33 | + $linesData = []; |
| 34 | + |
| 35 | + $record = new Record($item->pathAsString(), [ |
| 36 | + 'functions' => new FunctionCoverage(1, 1), |
| 37 | + 'lines' => $lineCoverage |
| 38 | + ]); |
| 39 | + } |
| 40 | + |
| 41 | + $report = new Report("Example", $records); |
| 42 | + |
| 43 | + $lineCoverage = new LineCoverage(2, 2, [ |
| 44 | + new LineData(6, 2), |
| 45 | + new LineData(7, 2) |
| 46 | + ]); |
| 47 | + |
| 48 | + $record = new Record("/home/cedx/lcov.hx/fixture.php", [ |
| 49 | + 'functions' => new FunctionCoverage(1, 1), |
| 50 | + 'lines' => $lineCoverage |
| 51 | + ]); |
| 52 | + |
| 53 | + $report = new Report("Example", [$record]); |
| 54 | + |
| 55 | + if ($target !== null) { |
| 56 | + Directory::create(dirname($target)); |
| 57 | + |
| 58 | + if (@file_put_contents($target, $buffer) === false) { |
| 59 | + throw new WriteOperationFailedException($target); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return $buffer; |
| 64 | + } |
| 65 | +} |
0 commit comments