Files
pest/src/Plugins/Tia/ConfigurationFile.php
T
Jhon Farias cd5bae7e91 fix(tia): restrict Xdebug coverage to the source scope before tracing (#2091)
* fix(tia): restrict Xdebug coverage to the source scope before tracing

* fix(tia): set the Xdebug exclude filter before the bootstrap and keep case twins traced

---------

Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
2026-12-04 14:13:25 +05:30

236 lines
7.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Pest\Plugins\Tia;
use PHPUnit\Event\Dispatcher;
use PHPUnit\Event\DispatchingEmitter;
use PHPUnit\Event\Emitter;
use PHPUnit\Event\Event;
use PHPUnit\Event\Facade as EventFacade;
use PHPUnit\Event\Telemetry\System;
use PHPUnit\Event\Telemetry\SystemCpuTimeMeter;
use PHPUnit\Event\Telemetry\SystemGarbageCollectorStatusProvider;
use PHPUnit\Event\Telemetry\SystemMemoryMeter;
use PHPUnit\Event\Telemetry\SystemStopWatch;
use PHPUnit\TextUI\CliArguments\Builder;
use PHPUnit\TextUI\CliArguments\XmlConfigurationFileFinder;
use PHPUnit\TextUI\XmlConfiguration\DefaultConfiguration;
use PHPUnit\TextUI\XmlConfiguration\Loader;
use Throwable;
/**
* @internal
*/
final readonly class ConfigurationFile
{
private const string SUPPRESSED = 'none';
private const array DEFAULT_NAMES = ['phpunit.xml', 'phpunit.dist.xml', 'phpunit.xml.dist'];
private const array VALUE_OPTIONS = ['--configuration', '--bootstrap', '--include-path', '--coverage-filter'];
/**
* @param list<string>|null $includePaths
* @param list<string> $coverageFilter
*/
private function __construct(
private ?string $path,
private bool $suppressed,
private ?string $bootstrap = null,
private ?array $includePaths = null,
private array $coverageFilter = [],
) {}
/**
* @param array<int, string> $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<int, string> $arguments
* @return list<string>
*/
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;
}
}