From 597bfd05bef2f23bf6ac2de50f983ad6849106a6 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sat, 13 Aug 2022 10:05:53 +0200 Subject: [PATCH] Closes #5022 --- .psalm/baseline.xml | 8 ----- ChangeLog-9.5.md | 1 + src/Util/ExcludeList.php | 69 ++++++++++++++++++++++------------------ 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/.psalm/baseline.xml b/.psalm/baseline.xml index b44554ba658..26a42470677 100644 --- a/.psalm/baseline.xml +++ b/.psalm/baseline.xml @@ -1294,9 +1294,6 @@ - - self::$directories === null - @@ -1430,11 +1427,6 @@ $stream - - - $filter - - sanitizeVersionNumber diff --git a/ChangeLog-9.5.md b/ChangeLog-9.5.md index 0b8a1287649..fb791cc26d6 100644 --- a/ChangeLog-9.5.md +++ b/ChangeLog-9.5.md @@ -8,6 +8,7 @@ All notable changes of the PHPUnit 9.5 release series are documented in this fil * [#5015](https://github.com/sebastianbergmann/phpunit/pull/5015): Ukraine banner unreadable on black background * [#5020](https://github.com/sebastianbergmann/phpunit/issues/5020): PHPUnit 9 breaks loading of PSR-0/PEAR style classes +* [#5022](https://github.com/sebastianbergmann/phpunit/issues/5022): `ExcludeList::addDirectory()` does not work correctly ## [9.5.21] - 2022-06-19 diff --git a/src/Util/ExcludeList.php b/src/Util/ExcludeList.php index 5c52fd54a92..7cfcf872a6d 100644 --- a/src/Util/ExcludeList.php +++ b/src/Util/ExcludeList.php @@ -166,7 +166,12 @@ final class ExcludeList /** * @var string[] */ - private static $directories; + private static $directories = []; + + /** + * @var bool + */ + private static $initialized = false; public static function addDirectory(string $directory): void { @@ -219,39 +224,41 @@ public function isExcluded(string $file): bool */ private function initialize(): void { - if (self::$directories === null) { - self::$directories = []; - - foreach (self::EXCLUDED_CLASS_NAMES as $className => $parent) { - if (!class_exists($className)) { - continue; - } - - try { - $directory = (new ReflectionClass($className))->getFileName(); - // @codeCoverageIgnoreStart - } catch (ReflectionException $e) { - throw new Exception( - $e->getMessage(), - (int) $e->getCode(), - $e - ); - } - // @codeCoverageIgnoreEnd - - for ($i = 0; $i < $parent; $i++) { - $directory = dirname($directory); - } - - self::$directories[] = $directory; + if (self::$initialized) { + return; + } + + foreach (self::EXCLUDED_CLASS_NAMES as $className => $parent) { + if (!class_exists($className)) { + continue; } - // Hide process isolation workaround on Windows. - if (DIRECTORY_SEPARATOR === '\\') { - // tempnam() prefix is limited to first 3 chars. - // @see https://php.net/manual/en/function.tempnam.php - self::$directories[] = sys_get_temp_dir() . '\\PHP'; + try { + $directory = (new ReflectionClass($className))->getFileName(); + // @codeCoverageIgnoreStart + } catch (ReflectionException $e) { + throw new Exception( + $e->getMessage(), + (int) $e->getCode(), + $e + ); } + // @codeCoverageIgnoreEnd + + for ($i = 0; $i < $parent; $i++) { + $directory = dirname($directory); + } + + self::$directories[] = $directory; } + + // Hide process isolation workaround on Windows. + if (DIRECTORY_SEPARATOR === '\\') { + // tempnam() prefix is limited to first 3 chars. + // @see https://php.net/manual/en/function.tempnam.php + self::$directories[] = sys_get_temp_dir() . '\\PHP'; + } + + self::$initialized = true; } }