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
20 changes: 12 additions & 8 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public function getPrimaryNamespace(PackageInterface $package): string
{
$primaryNs = null;
$autoLoad = $package->getAutoload();
/** @var array<string, string> $pathMap */
/** @var array<string, array<string>|string> $pathMap */
foreach ($autoLoad as $type => $pathMap) {
if ($type !== 'psr-4') {
continue;
Expand All @@ -364,16 +364,20 @@ public function getPrimaryNamespace(PackageInterface $package): string
break;
}

$matches = preg_grep('#^(\./)?src/?$#', $pathMap);
if ($matches) {
$primaryNs = key($matches);
break;
// A namespace can map to a list of paths.
foreach ($pathMap as $namespace => $paths) {
if (preg_grep('#^(\./)?src/?$#', (array)$paths)) {
$primaryNs = $namespace;
break 2;
}
}

foreach (['', '.'] as $path) {
$key = array_search($path, $pathMap, true);
if ($key !== false) {
$primaryNs = $key;
foreach ($pathMap as $namespace => $paths) {
if (in_array($path, (array)$paths, true)) {
$primaryNs = $namespace;
break;
}
}
}
break;
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,25 @@ public function testPreAutoloadDumpNonExistentPluginPaths()
$this->assertEquals($expected, $package->getDevAutoload());
}

public function testGetPrimaryNamespaceWithPathLists()
{
$this->package->setAutoload([
'psr-4' => [
'Acme\\Foo\\Test\\' => 'tests/',
'Acme\\Foo\\' => ['lib/', 'src/'],
],
]);
$this->assertSame('Acme\\Foo', $this->plugin->getPrimaryNamespace($this->package));

$this->package->setAutoload([
'psr-4' => [
'Acme\\Foo\\Test\\' => ['tests/'],
'Acme\\Foo\\' => ['', 'lib/'],
],
]);
$this->assertSame('Acme\\Foo', $this->plugin->getPrimaryNamespace($this->package));
}

public function testGetPrimaryNamespace()
{
$autoload = [
Expand Down
Loading