Passed
Push — master ( a2e26c...517afa )
by Théo
03:09
created

AutoloadPrefixer::prefixAutoloads()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\Scoper\Composer;
16
17
/**
18
 * @internal
19
 */
20
final class AutoloadPrefixer
21
{
22
    /**
23
     * @param array  $content Decoded JSON
24
     * @param string $prefix
25
     *
26
     * @return array Prefixed decoded JSON
27
     */
28
    public static function prefixPackageAutoloads(array $content, string $prefix): array
29
    {
30
        if (isset($content['autoload'])) {
31
            $content['autoload'] = self::prefixAutoloads($content['autoload'], $prefix);
32
        }
33
34
        if (isset($content['autoload-dev'])) {
35
            $content['autoload-dev'] = self::prefixAutoloads($content['autoload-dev'], $prefix);
36
        }
37
38
        return $content;
39
    }
40
41
    private static function prefixAutoloads(array $autoload, string $prefix): array
42
    {
43
        if (isset($autoload['psr-4'])) {
44
            $autoload['psr-4'] = self::prefixAutoload($autoload['psr-4'], $prefix);
45
        }
46
47
        return $autoload;
48
    }
49
50
    private static function prefixAutoload(array $autoload, string $prefix): array
51
    {
52
        $loader = [];
53
54
        foreach ($autoload as $namespace => $paths) {
55
            $loader[sprintf('%s\\%s', $prefix, $namespace)] = $paths;
56
        }
57
58
        return $loader;
59
    }
60
}
61