Skip to content

Commit b21fbfe

Browse files
authored
Do not add empty PSR-4 autoload entry (#173)
Do not add empty PSR-4 autoload entry Closes #166
1 parent 4203433 commit b21fbfe

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/Scoper/Composer/AutoloadPrefixer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
namespace Humbug\PhpScoper\Scoper\Composer;
1616

17+
use function array_key_exists;
18+
1719
/**
1820
* @private
1921
*/
@@ -40,10 +42,12 @@ public static function prefixPackageAutoloads(array $contents, string $prefix):
4042

4143
private static function prefixAutoloads(array $autoload, string $prefix): array
4244
{
43-
$autoload['psr-4'] = isset($autoload['psr-4']) ? $autoload['psr-4'] : [];
45+
if (false === array_key_exists('psr-4', $autoload) && false === array_key_exists('psr-0', $autoload)) {
46+
return $autoload;
47+
}
4448

4549
if (isset($autoload['psr-0'])) {
46-
$autoload['psr-4'] = self::mergePSR0And4($autoload['psr-0'], $autoload['psr-4']);
50+
$autoload['psr-4'] = self::mergePSR0And4($autoload['psr-0'], $autoload['psr-4'] ?? []);
4751
}
4852
unset($autoload['psr-0']);
4953

tests/Scoper/Composer/JsonFileScoperTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ public function providePSR0ComposerFiles()
212212
}
213213
JSON
214214
];
215+
215216
yield 'psr zero and four with the same namespace get merged' => [
216217
<<<'JSON'
217218
{
@@ -239,6 +240,7 @@ public function providePSR0ComposerFiles()
239240
}
240241
JSON
241242
];
243+
242244
yield 'psr zero and four get merged if either of them have multiple entries' => [
243245
<<<'JSON'
244246
{
@@ -290,6 +292,7 @@ public function providePSR0ComposerFiles()
290292
}
291293
JSON
292294
];
295+
293296
yield 'psr zero gets converted to psr4' => [
294297
<<<'JSON'
295298
{
@@ -311,6 +314,7 @@ public function providePSR0ComposerFiles()
311314
}
312315
JSON
313316
];
317+
314318
yield 'psr zero and four get merged when both are arrays' => [
315319
<<<'JSON'
316320
{
@@ -344,6 +348,27 @@ public function providePSR0ComposerFiles()
344348
}
345349
}
346350
}
351+
JSON
352+
];
353+
354+
yield [
355+
<<<'JSON'
356+
{
357+
"autoload": {
358+
"classmap": ["src"]
359+
}
360+
}
361+
362+
JSON
363+
,
364+
<<<'JSON'
365+
{
366+
"autoload": {
367+
"classmap": [
368+
"src"
369+
]
370+
}
371+
}
347372
JSON
348373
];
349374
}

0 commit comments

Comments
 (0)