Passed
Branch master (c435fb)
by Théo
02:10
created

AutoloadPrefixer::prefixAutoload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
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
 * @private
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 2
    public static function prefixPackageAutoloads(array $content, string $prefix): array
29
    {
30 2
        if (isset($content['autoload'])) {
31 2
            $content['autoload'] = self::prefixAutoloads($content['autoload'], $prefix);
32
        }
33
34 2
        if (isset($content['autoload-dev'])) {
35 1
            $content['autoload-dev'] = self::prefixAutoloads($content['autoload-dev'], $prefix);
36
        }
37
38 2
        return $content;
39
    }
40
41 2
    private static function prefixAutoloads(array $autoload, string $prefix): array
42
    {
43 2
        if (isset($autoload['psr-4'])) {
44 2
            $autoload['psr-4'] = self::prefixAutoload($autoload['psr-4'], $prefix);
45
        }
46
47 2
        return $autoload;
48
    }
49
50 2
    private static function prefixAutoload(array $autoload, string $prefix): array
51
    {
52 2
        $loader = [];
53
54 2
        foreach ($autoload as $namespace => $paths) {
55 2
            $loader[sprintf('%s\\%s', $prefix, $namespace)] = $paths;
56
        }
57
58 2
        return $loader;
59
    }
60
}
61