From a90d04455b94815b1353d81ec88aa9897542c7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 6 Feb 2024 17:22:50 +0100 Subject: [PATCH] Fix "php-code-coverage" v11 compatibility (#2161) --- src/Behat/CoverageUtil.php | 51 ++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/src/Behat/CoverageUtil.php b/src/Behat/CoverageUtil.php index 096f122486..1d10a1c17e 100644 --- a/src/Behat/CoverageUtil.php +++ b/src/Behat/CoverageUtil.php @@ -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 + */ + 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