Skip to content

Commit 71c309a

Browse files
committed
Sort candidates by file path length
The lengthiest paths are the ones who have the most in common with the provided class name, and should thus be given priority. Fixes phpactor/phpactor#712
1 parent 685fa0a commit 71c309a

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

lib/Adapter/Composer/ComposerFileToClass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ private function populateCandidates(FilePath $filePath, array $prefixes)
103103
}
104104
}
105105

106+
usort($candidates, function (array $leftCandidate, array $rightCandidate): int {
107+
return strlen($rightCandidate[0]) <=> strlen($leftCandidate[0]);
108+
});
109+
106110
return $candidates;
107111
}
108112
}

tests/Integration/Composer/ComposerFileToClassTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ public function testPsr4Fallback()
107107
]);
108108
}
109109

110+
public function testNestedPsr4Directories()
111+
{
112+
$this->loadExample('psr4-nested.json');
113+
$this->assertFilePathToClassName('/psr4/AppBundle/Foo.php', [
114+
'AppBundle\\Foo',
115+
'App\\AppBundle\\Foo'
116+
]);
117+
}
118+
119+
110120
/**
111121
* @testdox Loads from classmap
112122
*/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "dantleech/basic",
3+
"authors": [
4+
{
5+
"name": "dantleech",
6+
"email": "dan.t.leech@gmail.com"
7+
}
8+
],
9+
"require": {},
10+
"autoload": {
11+
"psr-4": {
12+
"App\\": "psr4/",
13+
"AppBundle\\": "psr4/AppBundle/"
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)