Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. File.php

function File::renderSourceWithPathCoverage

1 call to File::renderSourceWithPathCoverage()
File::render in vendor/phpunit/php-code-coverage/src/Report/Html/Renderer/File.php

File

vendor/phpunit/php-code-coverage/src/Report/Html/Renderer/File.php, line 654

Class

File
@internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage

Namespace

SebastianBergmann\CodeCoverage\Report\Html

Code

private function renderSourceWithPathCoverage(FileNode $node) : string {
    $linesTemplate = new Template($this->templatePath . 'lines.html.dist', '{{', '}}');
    $singleLineTemplate = new Template($this->templatePath . 'line.html.dist', '{{', '}}');
    $functionCoverageData = $node->functionCoverageData();
    $testData = $node->testData();
    $codeLines = $this->loadFile($node->pathAsString());
    $lineData = [];
    
    /** @var int $line */
    foreach (array_keys($codeLines) as $line) {
        $lineData[$line + 1] = [
            'includedInPaths' => [],
            'includedInHitPaths' => [],
            'tests' => [],
        ];
    }
    foreach ($functionCoverageData as $method) {
        foreach ($method['paths'] as $pathId => $path) {
            foreach ($path['path'] as $branchTaken) {
                foreach (range($method['branches'][$branchTaken]['line_start'], $method['branches'][$branchTaken]['line_end']) as $line) {
                    if (!isset($lineData[$line])) {
                        continue;
                    }
                    $lineData[$line]['includedInPaths'][] = $pathId;
                    if ($path['hit']) {
                        $lineData[$line]['includedInHitPaths'][] = $pathId;
                        $lineData[$line]['tests'] = array_unique(array_merge($lineData[$line]['tests'], $path['hit']));
                    }
                }
            }
        }
    }
    $lines = '';
    $i = 1;
    
    /** @var string $line */
    foreach ($codeLines as $line) {
        $trClass = '';
        $popover = '';
        $includedInPathsCount = count(array_unique($lineData[$i]['includedInPaths']));
        $includedInHitPathsCount = count(array_unique($lineData[$i]['includedInHitPaths']));
        if ($includedInPathsCount > 0) {
            $lineCss = 'success';
            if ($includedInHitPathsCount === 0) {
                $lineCss = 'danger';
            }
            elseif ($includedInHitPathsCount !== $includedInPathsCount) {
                $lineCss = 'warning';
            }
            $popoverContent = '<ul>';
            if (count($lineData[$i]['tests']) === 1) {
                $popoverTitle = '1 test covers line ' . $i;
            }
            else {
                $popoverTitle = count($lineData[$i]['tests']) . ' tests cover line ' . $i;
            }
            $popoverTitle .= '. These are covering ' . $includedInHitPathsCount . ' out of the ' . $includedInPathsCount . ' code paths.';
            foreach ($lineData[$i]['tests'] as $test) {
                $popoverContent .= $this->createPopoverContentForTest($test, $testData[$test]);
            }
            $popoverContent .= '</ul>';
            $trClass = $lineCss . ' popin';
            $popover = sprintf(' data-title="%s" data-content="%s" data-placement="top" data-html="true"', $popoverTitle, htmlspecialchars($popoverContent, $this->htmlSpecialCharsFlags));
        }
        $lines .= $this->renderLine($singleLineTemplate, $i, $line, $trClass, $popover);
        $i++;
    }
    $linesTemplate->setVar([
        'lines' => $lines,
    ]);
    return $linesTemplate->render();
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal