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

Breadcrumb

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

function Builder::reducePaths

Reduces the paths by cutting the longest common start path.

For instance,

<code> Array ( [/home/sb/Money/Money.php] => Array ( ... )

[/home/sb/Money/MoneyBag.php] => Array ( ... ) ) </code>

is reduced to

<code> Array ( [Money.php] => Array ( ... )

[MoneyBag.php] => Array ( ... ) ) </code>

1 call to Builder::reducePaths()
Builder::build in vendor/phpunit/php-code-coverage/src/Node/Builder.php

File

vendor/phpunit/php-code-coverage/src/Node/Builder.php, line 202

Class

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

Namespace

SebastianBergmann\CodeCoverage\Node

Code

private function reducePaths(ProcessedCodeCoverageData $coverage) : string {
    if (empty($coverage->coveredFiles())) {
        return '.';
    }
    $commonPath = '';
    $paths = $coverage->coveredFiles();
    if (count($paths) === 1) {
        $commonPath = dirname($paths[0]) . DIRECTORY_SEPARATOR;
        $coverage->renameFile($paths[0], basename($paths[0]));
        return $commonPath;
    }
    $max = count($paths);
    for ($i = 0; $i < $max; $i++) {
        // strip phar:// prefixes
        if (str_starts_with($paths[$i], 'phar://')) {
            $paths[$i] = substr($paths[$i], 7);
            $paths[$i] = str_replace('/', DIRECTORY_SEPARATOR, $paths[$i]);
        }
        $paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
        if (empty($paths[$i][0])) {
            $paths[$i][0] = DIRECTORY_SEPARATOR;
        }
    }
    $done = false;
    $max = count($paths);
    while (!$done) {
        for ($i = 0; $i < $max - 1; $i++) {
            if (!isset($paths[$i][0]) || !isset($paths[$i + 1][0]) || $paths[$i][0] !== $paths[$i + 1][0]) {
                $done = true;
                break;
            }
        }
        if (!$done) {
            $commonPath .= $paths[0][0];
            if ($paths[0][0] !== DIRECTORY_SEPARATOR) {
                $commonPath .= DIRECTORY_SEPARATOR;
            }
            for ($i = 0; $i < $max; $i++) {
                array_shift($paths[$i]);
            }
        }
    }
    $original = $coverage->coveredFiles();
    $max = count($original);
    for ($i = 0; $i < $max; $i++) {
        $coverage->renameFile($original[$i], implode(DIRECTORY_SEPARATOR, $paths[$i]));
    }
    return substr($commonPath, 0, -1);
}
RSS feed
Powered by Drupal