|null $includePaths * @param list $coverageFilter */ private function __construct( private ?string $path, private bool $suppressed, private ?string $bootstrap = null, private ?array $includePaths = null, private array $coverageFilter = [], ) {} /** * @param array $arguments */ public static function fromArguments(array $arguments, ?string $generatedPath = null): self { try { $configuration = new Builder(EventFacade::emitter())->fromParameters(self::configurationParameters($arguments)); } catch (Throwable) { return self::projectDefault(); } $bootstrap = $configuration->hasBootstrap() ? self::resolved($configuration->bootstrap()) : null; $includePaths = $configuration->hasIncludePath() ? array_map(self::resolved(...), explode(PATH_SEPARATOR, $configuration->includePath())) : null; $coverageFilter = $configuration->hasCoverageFilter() ? $configuration->coverageFilter() : []; if (! $configuration->hasConfigurationFile() && ! $configuration->useDefaultConfiguration()) { return new self(null, true, $bootstrap, $includePaths, $coverageFilter); } $file = new XmlConfigurationFileFinder()->find($configuration); $path = $file === false ? false : realpath($file); if ($path !== false && $generatedPath !== null && $path === realpath($generatedPath)) { return new self(null, false, $bootstrap, $includePaths, $coverageFilter); } return new self($path === false ? null : $path, false, $bootstrap, $includePaths, $coverageFilter); } public static function at(string $path): self { return new self($path, false); } public static function projectDefault(): self { return new self(null, false); } public function sourceScope(string $projectRoot): ?SourceScope { $path = $this->suppressed ? null : ($this->path ?? $this->defaultIn($projectRoot)); if ($path === null) { return SourceScope::fromSource($projectRoot, DefaultConfiguration::create()->source(), $this->coverageFilter); } try { $source = new Loader($this->silentEmitter())->load($path)->source(); } catch (Throwable) { return null; } return SourceScope::fromSource($projectRoot, $source, $this->coverageFilter); } public function bootstrapFingerprint(string $projectRoot): ?string { return $this->bootstrap === null ? null : $this->relativeTo($projectRoot, $this->bootstrap); } public function includePathFingerprint(string $projectRoot): ?string { if ($this->includePaths === null) { return null; } $paths = array_map( fn (string $path): string => $this->relativeTo($projectRoot, $path), $this->includePaths, ); return implode("\0", $paths); } public function fingerprint(string $projectRoot): ?string { if ($this->suppressed) { return self::SUPPRESSED; } $path = $this->path ?? $this->defaultIn($projectRoot); if ($path === null || ! is_file($path)) { return null; } $hash = @hash_file('xxh128', $path); return $hash === false ? null : $this->relativeTo($projectRoot, $path).':'.$hash; } private function defaultIn(string $projectRoot): ?string { foreach (self::DEFAULT_NAMES as $name) { if (is_file($projectRoot.DIRECTORY_SEPARATOR.$name)) { return $projectRoot.DIRECTORY_SEPARATOR.$name; } } return null; } private function relativeTo(string $projectRoot, string $path): string { $root = rtrim($this->withForwardSlashes(realpath($projectRoot) ?: $projectRoot), '/').'/'; $resolvedPath = $this->withForwardSlashes(self::resolved($path)); if ($resolvedPath.'/' === $root) { return '.'; } return str_starts_with($resolvedPath, $root) ? substr($resolvedPath, strlen($root)) : $resolvedPath; } /** * @param array $arguments * @return list */ private static function configurationParameters(array $arguments): array { $arguments = array_values($arguments); $parameters = []; foreach ($arguments as $index => $argument) { if ($argument === '--no-configuration') { $parameters[] = $argument; continue; } $option = self::withLongConfigurationOption($argument); foreach (self::VALUE_OPTIONS as $name) { if ($option === $name && isset($arguments[$index + 1])) { $parameters[] = $name; $parameters[] = $arguments[$index + 1]; } elseif (str_starts_with($option, $name.'=')) { $parameters[] = $name; $parameters[] = substr($option, strlen($name.'=')); } } } return $parameters; } private static function withLongConfigurationOption(string $argument): string { if ($argument === '-c') { return '--configuration'; } if (str_starts_with($argument, '-c')) { return '--configuration='.substr($argument, 2); } return $argument; } private function silentEmitter(): Emitter { return new DispatchingEmitter( new class implements Dispatcher { public function dispatch(Event $event): void {} }, new System( new SystemStopWatch, new SystemMemoryMeter, new SystemGarbageCollectorStatusProvider, new SystemCpuTimeMeter, ), ); } private function withForwardSlashes(string $path): string { return DIRECTORY_SEPARATOR === '\\' ? str_replace('\\', '/', $path) : $path; } private static function resolved(string $path): string { $real = $path === '' ? false : realpath($path); return $real === false ? $path : $real; } }