$directories Project-relative directory prefixes (no trailing slash). * @param list $files Project-relative file paths. * @param list $suffixes Filename suffixes (e.g. '.php'). */ public function __construct( private array $directories, private array $files, private array $suffixes, ) {} public static function fromProjectRoot(string $projectRoot): self { $directories = []; $files = []; $suffixes = []; try { $configuration = Registry::get(); foreach ($configuration->testSuite() as $suite) { foreach ($suite->directories() as $directory) { foreach (self::expandWildcards($directory->path()) as $path) { $rel = self::toRelative($path, $projectRoot); if ($rel !== null) { $directories[] = $rel; } } $suffixes[] = $directory->suffix(); } foreach ($suite->files() as $file) { $rel = self::toRelative($file->path(), $projectRoot); if ($rel !== null) { $files[] = $rel; } } } if ($suffixes === []) { foreach ($configuration->testSuffixes() as $suffix) { $suffixes[] = $suffix; } } } catch (Throwable) { // } if ($suffixes === []) { $suffixes = ['.php']; } if ($directories === [] && $files === []) { $fallback = self::testSuiteFallback($projectRoot); if ($fallback !== null) { $directories[] = $fallback; } } return new self( array_values(array_unique($directories)), array_values(array_unique($files)), array_values(array_unique($suffixes)), ); } public function isTestFile(string $relativePath): bool { if (in_array($relativePath, $this->files, true)) { return true; } $matchesSuffix = array_any($this->suffixes, fn (string $suffix): bool => str_ends_with($relativePath, $suffix)); if (! $matchesSuffix) { return false; } foreach ($this->directories as $dir) { if ($dir === '') { continue; } if (str_starts_with($relativePath, $dir.'/')) { return true; } } return false; } /** * @return list */ private static function expandWildcards(string $path): array { if (! str_contains($path, '*')) { return [$path]; } return glob($path, GLOB_ONLYDIR) ?: [$path]; } private static function toRelative(string $value, string $projectRoot): ?string { $value = trim($value); if ($value === '') { return null; } $real = @realpath($value); $resolved = $real === false ? $value : $real; $resolved = str_replace(DIRECTORY_SEPARATOR, '/', $resolved); $root = str_replace(DIRECTORY_SEPARATOR, '/', rtrim($projectRoot, '/\\')).'/'; if (! str_starts_with($resolved.'/', $root)) { return null; } return rtrim(substr($resolved, strlen($root)), '/'); } private static function testSuiteFallback(string $projectRoot): ?string { try { $testPath = TestSuite::getInstance()->testPath; } catch (Throwable) { return null; } $real = @realpath($testPath); $resolved = $real === false ? $testPath : $real; $resolved = str_replace(DIRECTORY_SEPARATOR, '/', $resolved); $root = str_replace(DIRECTORY_SEPARATOR, '/', rtrim($projectRoot, '/\\')).'/'; if (! str_starts_with($resolved.'/', $root)) { return null; } return rtrim(substr($resolved, strlen($root)), '/'); } }