Skip to content
Open
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
42 changes: 38 additions & 4 deletions src/Composer/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,46 @@ private static function join(string $elem, string ...$elems): string
return implode(DIRECTORY_SEPARATOR, [$elem, ...$elems]);
}

/** @return non-empty-string */
private static function trimPathSeparators(string $path): string
{
$trimmed = rtrim($path, '/\\');
if ($trimmed === '') {
return $path;
}
return $trimmed;
}

/** @return ?non-empty-string */
private static function discoverScipPhpVendorDir(): ?string
{
$packageRoot = self::join(__DIR__, '..', '..');
$candidates = [
self::join($packageRoot, 'vendor'),
self::join($packageRoot, '..', '..'),
];
foreach ($candidates as $candidate) {
$autoload = self::join($candidate, 'autoload.php');
if (!is_file($autoload)) {
continue;
}
$realPath = realpath($candidate);
if ($realPath !== false) {
return $realPath;
}
}
return null;
}

/** @param non-empty-string $projectRoot */
public function __construct(private readonly string $projectRoot)
{
$json = $this->parseJson('composer.json');
$autoload = is_array($json['autoload'] ?? null) ? $json['autoload'] : [];
$autoloadDev = is_array($json['autoload-dev'] ?? null) ? $json['autoload-dev'] : [];

$scipPhpVendorDir = self::join(__DIR__, '..', '..', 'vendor');
if (realpath($scipPhpVendorDir) === false) {
$scipPhpVendorDir = self::discoverScipPhpVendorDir();
if ($scipPhpVendorDir === null) {
// If the vendor directory relative to this file is not found, scip-php probably runs as a
// dev dependency of the project that it analyses and shares the vendor directory with it.
$cwd = getcwd();
Expand Down Expand Up @@ -123,7 +154,7 @@ public function __construct(private readonly string $projectRoot)
is_array($json['config'] ?? null)
&& is_string($json['config']['vendor-dir'] ?? null)
) {
$dir = trim($json['config']['vendor-dir'], '/');
$dir = trim($json['config']['vendor-dir'], '/\\');
if ($dir !== '') {
$vendorDir = $dir;
}
Expand All @@ -137,6 +168,9 @@ public function __construct(private readonly string $projectRoot)
if (!$loader instanceof ClassLoader) {
throw new RuntimeException("Cannot get autoload.php class loader.");
}
if ($autoloadDir !== $this->scipPhpVendorDir) {
$loader->unregister();
}
$this->loader = $loader;

$installed = require self::join($this->vendorDir, 'composer', 'installed.php');
Expand Down Expand Up @@ -279,7 +313,7 @@ private function loadProjectFiles(array $autoload): array
continue;
}
$p = self::join($this->projectRoot, $path);
$p = rtrim($p, DIRECTORY_SEPARATOR);
$p = self::trimPathSeparators($p);
$generator->scanPaths($p, $exclusionRegex, $t, $ns);
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

use function array_values;
use function str_replace;
use function str_starts_with;
use function substr;

final readonly class Indexer
{
Expand Down Expand Up @@ -57,6 +59,18 @@ public function __construct(
$this->types = new Types($this->composer, $this->namer);
}

/** @return non-empty-string */
private function relativePath(string $filename): string
{
$normalizedProjectRoot = str_replace('\\', '/', $this->projectRoot);
$normalizedFilename = str_replace('\\', '/', $filename);
$prefix = $normalizedProjectRoot . '/';
if (str_starts_with($normalizedFilename, $prefix)) {
return substr($normalizedFilename, \strlen($prefix));
}
return $normalizedFilename;
}

public function index(): Index
{
$projectFiles = $this->composer->projectFiles();
Expand All @@ -69,7 +83,7 @@ public function index(): Index
$this->parser->traverse($filename, $indexer, $indexer->index(...));
$documents[] = new Document([
'language' => Language::PHP,
'relative_path' => str_replace($this->projectRoot . '/', '', $filename),
'relative_path' => $this->relativePath($filename),
'occurrences' => $indexer->occurrences,
'symbols' => array_values($indexer->symbols),
'position_encoding' => PositionEncoding::UTF8CodeUnitOffsetFromLineStart,
Expand Down
21 changes: 21 additions & 0 deletions tests/Composer/ComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Override;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use ScipPhp\Composer\Composer;

use function count;
Expand Down Expand Up @@ -82,6 +83,26 @@ public function testProjectFiles(): void
self::assertStringEndsWith(self::join($root, 'tests', 'ClassATestCase.php'), $files[4]);
}

public function testTrimPathSeparatorsSupportsBothSlashStyles(): void
{
$method = new ReflectionMethod(Composer::class, 'trimPathSeparators');

self::assertSame('foo/bar', $method->invoke(null, 'foo/bar/'));
self::assertSame('foo\\bar', $method->invoke(null, 'foo\\bar\\'));
self::assertSame('foo/bar', $method->invoke(null, 'foo/bar\\'));
self::assertSame('foo\\bar', $method->invoke(null, 'foo\\bar/'));
}

public function testDiscoverScipPhpVendorDirFindsOwnVendorDirectory(): void
{
$method = new ReflectionMethod(Composer::class, 'discoverScipPhpVendorDir');
$vendorDir = $method->invoke(null);

self::assertIsString($vendorDir);
self::assertStringEndsWith(self::join('vendor'), $vendorDir);
self::assertFileExists(self::join($vendorDir, 'autoload.php'));
}

public function testIsDependency(): void
{
foreach ([...self::BUILTIN, ...self::DEPS, ...self::UNKNOWN] as $idents) {
Expand Down
12 changes: 12 additions & 0 deletions tests/Indexer/IndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Override;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ScipPhp\File\Reader;
Expand Down Expand Up @@ -91,6 +92,17 @@ public function testIndexer(): void
}
}

public function testRelativePathNormalizesWindowsSeparators(): void
{
$indexer = new Indexer(self::TESTDATA_DIR . 'scip-php-test', 'test', []);
$method = new ReflectionMethod(Indexer::class, 'relativePath');

self::assertSame(
'src/Foo.php',
$method->invoke($indexer, self::TESTDATA_DIR . 'scip-php-test\\src\\Foo.php'),
);
}

/** @return array<non-empty-string, non-empty-string> */
private static function files(string $dir): array
{
Expand Down