Skip to content

Commit

Permalink
Fix "php-code-coverage" v11 compatibility (#2161)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Feb 6, 2024
1 parent 1b5f81b commit a90d044
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions src/Behat/CoverageUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,55 @@ public static function start(Filter $filter): void
self::$coverage->start(self::class);
}

public static function startFromPhpunitConfig(string $phpunitConfigDir): void
/**
* @return list<string>
*/
private static function listFiles(string $directory): array
{
$filter = new Filter();
$res = [];
foreach (array_diff(scandir($directory), ['.', '..']) as $v) {
$path = $directory . '/' . $v;
if (is_dir($path)) {
foreach (self::listFiles($path) as $path2) {
$res[] = $path2;
}
} else {
$res[] = $path;
}
}

return $res;
}

public static function startFromPhpunitConfig(string $phpunitConfigDir): void
{
$phpunitCoverageConfig = simplexml_load_file($phpunitConfigDir . '/phpunit.xml.dist')->source;
foreach ($phpunitCoverageConfig->include->directory ?? [] as $path) {
$filter->includeDirectory($phpunitConfigDir . '/' . $path); // @phpstan-ignore-line
}
foreach ($phpunitCoverageConfig->include->file ?? [] as $path) {
$filter->includeFile($phpunitConfigDir . '/' . $path);
}

$excludeFiles = [];
foreach ($phpunitCoverageConfig->exclude->directory ?? [] as $path) {
$filter->excludeDirectory($phpunitConfigDir . '/' . $path); // @phpstan-ignore-line
foreach (self::listFiles($phpunitConfigDir . '/' . $path) as $path2) {
$excludeFiles[] = $path2;
}
}
foreach ($phpunitCoverageConfig->exclude->file ?? [] as $path) {
$filter->excludeFile($phpunitConfigDir . '/' . $path); // @phpstan-ignore-line
$excludeFiles[] = $phpunitConfigDir . '/' . $path;
}

$files = [];
foreach ($phpunitCoverageConfig->include->directory ?? [] as $path) {
foreach (self::listFiles($phpunitConfigDir . '/' . $path) as $path2) {
$files[] = $path2;
}
}
$files = array_diff($files, $excludeFiles);

foreach ($phpunitCoverageConfig->include->file ?? [] as $path) {
$files[] = $phpunitConfigDir . '/' . $path;
}

// https://github.com/sebastianbergmann/phpunit/blob/11.0.2/src/TextUI/Configuration/CodeCoverageFilterRegistry.php#L57
$filter = new Filter();
$filter->includeFiles($files);
static::start($filter);

// fix https://github.com/sebastianbergmann/php-code-coverage/issues/942
Expand Down

0 comments on commit a90d044

Please sign in to comment.