Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion src/Analyser/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,12 @@ private function collectPhpFiles(array $layers, array $scanPaths, array $skipPat
$files = [];

foreach ($this->scanPaths($layers, $scanPaths) as $layerPath) {
$fullPath = rtrim($this->basePath, '/') . '/' . ltrim($layerPath, '/');
$isAbsolute = str_starts_with($layerPath, '/') || (strlen($layerPath) >= 2 && $layerPath[1] === ':');
$fullPath = $this->normalisePath(
$isAbsolute
? $layerPath
: rtrim($this->basePath, '/') . '/' . ltrim($layerPath, '/')
);

if (is_file($fullPath)) {
if (str_ends_with($fullPath, '.php') && ! $this->isSkipped($fullPath, $skipPaths)) {
Expand Down
21 changes: 17 additions & 4 deletions src/Rule/Rules/Composer/Psr4SourcePathsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use function rtrim;
use function sprintf;
use function str_replace;
use function str_starts_with;
use function strlen;
use function substr;
use function trim;

final readonly class Psr4SourcePathsRule implements ProjectRuleInterface
Expand All @@ -38,7 +41,7 @@ public function sourcePathsFor(string $basePath): array
return $this->psr4PathResolver->paths($basePath);
}

return $this->normalisePaths($this->sourcePaths);
return $this->normalisePaths($this->sourcePaths, $basePath);
}

public function evaluateProject(string $basePath, Architecture $architecture, array $skipPaths = []): ?RuleViolation
Expand Down Expand Up @@ -68,7 +71,7 @@ public function evaluateProject(string $basePath, Architecture $architecture, ar
$autoloadPaths = $this->psr4PathResolver->paths($basePath);
$missingPaths = [];

foreach ($this->normalisePaths($this->sourcePaths) as $sourcePath) {
foreach ($this->normalisePaths($this->sourcePaths, $basePath) as $sourcePath) {
if (! in_array($sourcePath, $autoloadPaths, true)) {
$missingPaths[] = $sourcePath;
}
Expand All @@ -91,10 +94,20 @@ public function evaluateProject(string $basePath, Architecture $architecture, ar
* @param list<string> $paths
* @return list<string>
*/
private function normalisePaths(array $paths): array
private function normalisePaths(array $paths, string $basePath): array
{
$normalisedBase = rtrim(str_replace('\\', '/', $basePath), '/');

return array_map(
static fn(string $path): string => rtrim(str_replace('\\', '/', trim($path)), '/'),
static function (string $path) use ($normalisedBase): string {
$path = rtrim(str_replace('\\', '/', trim($path)), '/');

if ($normalisedBase !== '' && str_starts_with($path, $normalisedBase . '/')) {
return substr($path, strlen($normalisedBase) + 1);
}

return $path;
},
$paths
);
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,40 @@ public function testFilesForAnalysisIgnoresDirectorySymlinks(): void
$this->assertStringEndsWith('/src/Foo.php', $this->normalisePath($files[0]));
}

public function testFilesForAnalysisWithAbsoluteScanPath(): void
{
$basePath = $this->makeTempProject([
'index.php' => '<?php namespace App; final class Index {}',
'src/Foo.php' => '<?php namespace App; final class Foo {}',
]);

$architecture = Architecture::define()
->layer('Source', 'src/');

$absoluteScanPath = $basePath . '/index.php';

$files = (new Analyser($basePath))->filesForAnalysis($architecture, [$absoluteScanPath]);

$this->assertCount(1, $files);
$this->assertStringEndsWith('/index.php', $this->normalisePath($files[0]));
}

public function testFilesForAnalysisWithRootRelativeScanPath(): void
{
$basePath = $this->makeTempProject([
'index.php' => '<?php namespace App; final class Index {}',
'src/Foo.php' => '<?php namespace App; final class Foo {}',
]);

$architecture = Architecture::define()
->layer('Source', 'src/');

$files = (new Analyser($basePath))->filesForAnalysis($architecture, ['index.php']);

$this->assertCount(1, $files);
$this->assertStringEndsWith('/index.php', $this->normalisePath($files[0]));
}

public function testFilesForAnalysisUsesPreResolvedLayersWhenProvided(): void
{
$basePath = $this->makeTempProject([
Expand Down
26 changes: 26 additions & 0 deletions tests/Rule/Composer/Psr4SourcePathsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,32 @@ public function testNormalisesExplicitSourcePaths(): void
$this->assertSame(['src', 'tests'], $psr4SourcePathsRule->sourcePathsFor($this->makeTempDir()));
}

public function testPassesWhenAbsoluteSourcePathsExistInComposerPsr4Autoloads(): void
{
$basePath = $this->makeTempProject(<<<'JSON'
{
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
}
}
JSON);

$psr4SourcePathsRule = new Psr4SourcePathsRule([$basePath . '/src', $basePath . '/tests']);

$this->assertNotInstanceOf(
RuleViolation::class,
$psr4SourcePathsRule->evaluateProject($basePath, Architecture::define())
);
$this->assertSame(['src', 'tests'], $psr4SourcePathsRule->sourcePathsFor($basePath));
}

private function makeTempProject(string $composerJson): string
{
$basePath = $this->makeTempDir();
Expand Down
Loading