Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 29 additions & 49 deletions src/Analyser/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@

use const ARRAY_FILTER_USE_BOTH;

final readonly class Analyser
final class Analyser
{
private string $basePath;
private readonly string $basePath;

private string $normalisedBasePath;
private readonly string $normalisedBasePath;

/** @var array<string, string> */
private array $normalisedPaths = [];

public function __construct(
string $basePath = '',
private ?AnalysisResultCache $analysisResultCache = null,
private string $classNodeCacheNamespace = '',
private readonly ?AnalysisResultCache $analysisResultCache = null,
private readonly string $classNodeCacheNamespace = '',
) {
$this->basePath = $basePath !== '' ? $basePath : (string) getcwd();
$this->normalisedBasePath = $this->normalisePath($this->basePath);
Expand Down Expand Up @@ -549,11 +552,7 @@ private function collectPhpFiles(array $layers, array $scanPaths, array $skipPat

if (is_file($fullPath)) {
if (str_ends_with($fullPath, '.php') && ! $this->isSkipped($fullPath, $skipPaths)) {
$realPath = realpath($fullPath);

if ($realPath !== false) {
$files[] = $realPath;
}
$files[] = $fullPath;
}

continue;
Expand Down Expand Up @@ -623,70 +622,51 @@ private function isSkipped(string $path, array $skipPaths): bool
return false;
}

$path = $this->normalisePath($path);
$relativePath = $this->relativePath($path);
$normalisedPath = $this->normalisePath($path);

foreach ($skipPaths as $skipPath) {
if (
$this->matchesSkipPath($relativePath, $skipPath)
|| $this->matchesSkipPath($path, $skipPath)
) {
$absoluteSkipPath = $this->toAbsoluteSkipPath($this->normaliseSkipPath($skipPath));

if ($this->matchesSkipPath($normalisedPath, $absoluteSkipPath)) {
return true;
}
}

return false;
}

private function matchesSkipPath(string $path, string $skipPath): bool
private function toAbsoluteSkipPath(string $normalisedSkipPath): string
{
$skipPath = $this->normaliseSkipPath($skipPath);

if (fnmatch($skipPath, $path)) {
return true;
if (
str_starts_with($normalisedSkipPath, '/')
|| (strlen($normalisedSkipPath) >= 2 && $normalisedSkipPath[1] === ':')
) {
return $normalisedSkipPath;
}

if (strpbrk($skipPath, '*?[') !== false) {
return false;
}
return $this->normalisedBasePath . '/' . $normalisedSkipPath;
}

if (str_starts_with($skipPath, '/') || (strlen($skipPath) >= 2 && $skipPath[1] === ':')) {
$fullSkipPath = $this->normalisePath($skipPath);
} else {
$fullSkipPath = $this->normalisePath($this->normalisedBasePath . '/' . $skipPath);
private function matchesSkipPath(string $normalisedPath, string $absoluteSkipPath): bool
{
if (strpbrk($absoluteSkipPath, '*?[') !== false) {
return fnmatch($absoluteSkipPath, $normalisedPath);
}

$normalisedPath = $this->normalisePath($path);
$resolvedSkipPath = $this->normalisePath($absoluteSkipPath);

return $normalisedPath === $fullSkipPath
|| str_starts_with($normalisedPath, $fullSkipPath . '/')
|| $path === $skipPath
|| str_starts_with($path, rtrim($skipPath, '/') . '/');
return $normalisedPath === $resolvedSkipPath
|| str_starts_with($normalisedPath, $resolvedSkipPath . '/');
}

private function normaliseSkipPath(string $path): string
{
return rtrim(str_replace('\\', '/', $path), '/');
}

private function relativePath(string $path): string
{
$basePath = $this->normalisedBasePath;

if ($path === $basePath) {
return '';
}

if (str_starts_with($path, $basePath . '/')) {
return substr($path, strlen($basePath) + 1);
}

return $path;
}

private function normalisePath(string $path): string
{
return rtrim(str_replace('\\', '/', realpath($path) ?: $path), '/');
return $this->normalisedPaths[$path] ??= rtrim(str_replace('\\', '/', realpath($path) ?: $path), '/');
}

/**
Expand Down
Loading