canCollectCodeCoverage()) { return false; } if ($runtime->hasPCOV() || $runtime->hasPHPDBGCodeCoverage()) { return true; } if (static::usingXdebug()) { $mode = getenv('XDEBUG_MODE') ?: ini_get('xdebug.mode'); return $mode && in_array('coverage', explode(',', $mode), true); } return true; } /** * If the user is using Xdebug. */ public static function usingXdebug(): bool { return (new Runtime())->hasXdebug(); } /** * Reports the code coverage report to the * console and returns the result in float. */ public static function report(OutputInterface $output): float { if (! file_exists($reportPath = self::getPath())) { if (self::usingXdebug()) { $output->writeln( " WARN Unable to get coverage using Xdebug. Did you set Xdebug's coverage mode?", ); return 0.0; } $output->writeln( ' WARN No coverage driver detected. Did you install Xdebug or PCOV?', ); return 0.0; } /** @var CodeCoverage $codeCoverage */ $codeCoverage = require $reportPath; unlink($reportPath); $totalCoverage = $codeCoverage->getReport()->percentageOfExecutedLines(); /** @var Directory $report */ $report = $codeCoverage->getReport(); foreach ($report->getIterator() as $file) { if (! $file instanceof File) { continue; } $dirname = dirname($file->id()); $basename = basename($file->id(), '.php'); $name = $dirname === '.' ? $basename : implode(DIRECTORY_SEPARATOR, [ $dirname, $basename, ]); $percentage = $file->numberOfExecutableLines() === 0 ? '100.0' : number_format($file->percentageOfExecutedLines()->asFloat(), 1, '.', ''); $uncoveredLines = ''; $percentageOfExecutedLinesAsString = $file->percentageOfExecutedLines()->asString(); if (! in_array($percentageOfExecutedLinesAsString, ['0.00%', '100.00%', '100.0%', ''], true)) { $uncoveredLines = trim(implode(', ', self::getMissingCoverage($file))); $uncoveredLines = sprintf('%s', $uncoveredLines).' / '; } $color = $percentage === '100.0' ? 'green' : ($percentage === '0.0' ? 'red' : 'yellow'); $truncateAt = max(1, terminal()->width() - 12); renderUsing($output); render(<< {$name} $uncoveredLines {$percentage}% HTML); } $totalCoverageAsString = $totalCoverage->asFloat() === 0.0 ? '0.0' : number_format($totalCoverage->asFloat(), 1, '.', ''); renderUsing($output); render(<<
Total: {$totalCoverageAsString} %
HTML); return $totalCoverage->asFloat(); } /** * Generates an array of missing coverage on the following format:. * * ``` * ['11', '20..25', '50', '60..80']; * ``` * * @param File $file * @return array */ public static function getMissingCoverage($file): array { $shouldBeNewLine = true; $eachLine = function (array $array, array $tests, int $line) use (&$shouldBeNewLine): array { if ($tests !== []) { $shouldBeNewLine = true; return $array; } if ($shouldBeNewLine) { $array[] = (string) $line; $shouldBeNewLine = false; return $array; } $lastKey = count($array) - 1; if (array_key_exists($lastKey, $array) && str_contains((string) $array[$lastKey], '..')) { [$from] = explode('..', (string) $array[$lastKey]); $array[$lastKey] = $line > $from ? sprintf('%s..%s', $from, $line) : sprintf('%s..%s', $line, $from); return $array; } $array[$lastKey] = sprintf('%s..%s', $array[$lastKey], $line); return $array; }; $array = []; foreach (array_filter($file->lineCoverageData(), 'is_array') as $line => $tests) { $array = $eachLine($array, $tests, $line); } return $array; } }