Skip to content

Commit 6692c95

Browse files
authored
Fix case where installed path is symlink (#33)
One can install packages from a local path via composers repositories type "path". Those are typically symlinks which were not resolved. We therefore add a realpath() call, but falling back to old behavior if it didn't resolve. That way we keep independent of realpath() as requested by the inline comment. Resolves: phpactor/phpactor#2727
1 parent 2fcf99a commit 6692c95

5 files changed

Lines changed: 54 additions & 4 deletions

File tree

lib/Adapter/Composer/ComposerFileToClass.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ private function populateCandidates(FilePath $filePath, array $prefixes)
7777
$pathPrefixes = (array) $pathPrefixes;
7878

7979
// remove any relativeness from the paths
80-
//
81-
// TODO: realpath will return void if the path does not exist
82-
// we should not depend on the file path existing.
80+
// we should not depend on the file path existing.
8381
$pathPrefixes = array_map(function ($pathPrefix) {
84-
return Path::canonicalize($pathPrefix);
82+
$canonicalizedPath = Path::canonicalize($pathPrefix);
83+
$realPath = realpath($canonicalizedPath);
84+
return $realPath ?: $canonicalizedPath;
8585
}, $pathPrefixes);
8686

8787
foreach ($pathPrefixes as $pathPrefix) {

tests/Integration/Composer/ComposerFileToClassTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public function testPsr4WithRelativePathComponents(): void
3535
$this->assertFilePathToClassName('/psr4/Foo/../Foo/Class.php', ['Acme\\Test\\Foo\\Class']);
3636
}
3737

38+
/**
39+
* @testdox PSR-4 file with symlinked path components
40+
*/
41+
public function testPsr4Symlinked(): void
42+
{
43+
$this->loadExample('psr4-symlinked-project.json');
44+
$this->assertFilePathToClassName('/symlinked-package/Class.php', ['Acme\\Test\\Class']);
45+
}
46+
3847
/**
3948
* @testdox PSR-4 multiple matching prefixes
4049
*/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "dantleech/basic",
3+
"authors": [
4+
{
5+
"name": "dantleech",
6+
"email": "dan.t.leech@gmail.com"
7+
}
8+
],
9+
"repositories": [
10+
{
11+
"type": "path",
12+
"url": "./*"
13+
}
14+
],
15+
"require": {
16+
"dantleech/symlinked": "1.0.0"
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Acme\Test;
4+
5+
class Class
6+
{
7+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "dantleech/symlinked",
3+
"version": "1.0.0",
4+
"authors": [
5+
{
6+
"name": "dantleech",
7+
"email": "dan.t.leech@gmail.com"
8+
}
9+
],
10+
"require": {},
11+
"autoload": {
12+
"psr-4": {
13+
"Acme\\Test\\": "./"
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)