Completed
Push — master ( 674e24...32a903 )
by Théo
02:43
created

AutoloadPrefixer::mergePSR0And4()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 5
nop 2
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 4
rs 9.2
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
 * @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 7
    public static function prefixPackageAutoloads(array $content, string $prefix): array
29
    {
30 7
        if (isset($content['autoload'])) {
31 7
            $content['autoload'] = self::prefixAutoloads($content['autoload'], $prefix);
32
        }
33
34 7
        if (isset($content['autoload-dev'])) {
35 3
            $content['autoload-dev'] = self::prefixAutoloads($content['autoload-dev'], $prefix);
36
        }
37
38 7
        return $content;
39
    }
40
41 7
    private static function prefixAutoloads(array $autoload, string $prefix): array
42
    {
43 7
        $autoload['psr-4'] = isset($autoload['psr-4']) ? $autoload['psr-4'] : [];
44
45 7
        if (isset($autoload['psr-0'])) {
46 5
            $autoload['psr-4'] = self::mergePSR0And4($autoload['psr-0'], $autoload['psr-4']);
47
        }
48 7
        unset($autoload['psr-0']);
49
50 7
        if (isset($autoload['psr-4'])) {
51 7
            $autoload['psr-4'] = self::prefixAutoload($autoload['psr-4'], $prefix);
52
        }
53
54 7
        return $autoload;
55
    }
56
57 7
    private static function prefixAutoload(array $autoload, string $prefix): array
58
    {
59 7
        $loader = [];
60
61 7
        foreach ($autoload as $namespace => $paths) {
62 7
            $loader[sprintf('%s\\%s', $prefix, $namespace)] = $paths;
63
        }
64
65 7
        return $loader;
66
    }
67
68 5
    private static function mergePSR0And4(array $psr0, array $psr4): array
69
    {
70 5
        foreach ($psr0 as $namespace => $path) {
71
            //Append backslashes, if needed, since psr-0 does not require this
72 5
            if ('\\' !== substr($namespace, -1)) {
73 1
                $namespace .= '\\';
74
            }
75
76 5
            $path = self::updatePSR0Path($path, $namespace);
77
78 5
            if (!isset($psr4[$namespace])) {
79 2
                $psr4[$namespace] = $path;
80
81 2
                continue;
82
            }
83 3
            $psr4[$namespace] = self::mergeNamespaces($namespace, $path, $psr4);
84
        }
85
86 5
        return $psr4;
87
    }
88
89 5
    private static function updatePSR0Path($path, $namespace)
90
    {
91 5
        $namespaceForPsr = str_replace('\\', '/', $namespace);
92
93 5
        if (!is_array($path)) {
94 4
            if ('/' !== substr($path, -1)) {
95 1
                $path .= '/';
96
            }
97
98 4
            $path .= $namespaceForPsr.'/';
99
100 4
            return $path;
101
        }
102 2
        foreach ($path as $key => $item) {
103 2
            if ('/' !== substr($item, -1)) {
104 1
                $item .= '/';
105
            }
106
107 2
            $item .= $namespaceForPsr.'/';
108 2
            $path[$key] = $item;
109
        }
110
111 2
        return $path;
112
    }
113
114
    /**
115
     * Deals with the 4 possible scenarios:
116
     *       PSR0 | PSR4
117
     * array      |
118
     * string     |
119
     * or simply the namepace not existing as a psr-4 entry.
120
     *
121
     * @param string $psr0Namespace
122
     * @param string|array $psr0Path
123
     * @param string|array $psr4
124
     *
125
     * @return string|array
126
     */
127 3
    private static function mergeNamespaces(string $psr0Namespace, $psr0Path, $psr4)
128
    {
129
        // Both strings
130 3
        if (is_string($psr4[$psr0Namespace]) && is_string($psr0Path)) {
131 1
            return [$psr4[$psr0Namespace], $psr0Path];
132
        }
133
        //psr-4 is string, and psr-0 is array
134 2 View Code Duplication
        if (is_string($psr4[$psr0Namespace]) && is_array($psr0Path)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135 1
            $psr0Path[] = $psr4[$psr0Namespace];
136
137 1
            return $psr0Path;
138
        }
139
140
        //psr-4 is array and psr-0 is string
141 2 View Code Duplication
        if (is_array($psr4[$psr0Namespace]) && is_string($psr0Path)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142 1
            $psr4[$psr0Namespace][] = $psr0Path;
143
144 1
            return $psr4[$psr0Namespace];
145
        }
146
147 1
        if (is_array($psr4[$psr0Namespace]) && is_array($psr0Path)) {
148 1
            return array_merge($psr4[$psr0Namespace], $psr0Path);
149
        }
150
151
        return $psr0Path;
152
    }
153
}
154