Completed
Push — master ( 420343...b21fbf )
by Théo
07:34 queued 03:15
created

AutoloadPrefixer::prefixAutoloads()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 5
nop 2
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 5
rs 8.8571
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
use function array_key_exists;
18
19
/**
20
 * @private
21
 */
22
final class AutoloadPrefixer
23
{
24
    /**
25
     * @param array  $contents Decoded JSON
26
     * @param string $prefix
27
     *
28
     * @return array Prefixed decoded JSON
29
     */
30 8
    public static function prefixPackageAutoloads(array $contents, string $prefix): array
31
    {
32 8
        if (isset($contents['autoload'])) {
33 8
            $contents['autoload'] = self::prefixAutoloads($contents['autoload'], $prefix);
34
        }
35
36 8
        if (isset($contents['autoload-dev'])) {
37 3
            $contents['autoload-dev'] = self::prefixAutoloads($contents['autoload-dev'], $prefix);
38
        }
39
40 8
        return $contents;
41
    }
42
43 8
    private static function prefixAutoloads(array $autoload, string $prefix): array
44
    {
45 8
        if (false === array_key_exists('psr-4', $autoload) && false === array_key_exists('psr-0', $autoload)) {
46 1
            return $autoload;
47
        }
48
49 7
        if (isset($autoload['psr-0'])) {
50 5
            $autoload['psr-4'] = self::mergePSR0And4($autoload['psr-0'], $autoload['psr-4'] ?? []);
51
        }
52 7
        unset($autoload['psr-0']);
53
54 7
        if (isset($autoload['psr-4'])) {
55 7
            $autoload['psr-4'] = self::prefixAutoload($autoload['psr-4'], $prefix);
56
        }
57
58 7
        return $autoload;
59
    }
60
61 7
    private static function prefixAutoload(array $autoload, string $prefix): array
62
    {
63 7
        $loader = [];
64
65 7
        foreach ($autoload as $namespace => $paths) {
66 7
            $loader[sprintf('%s\\%s', $prefix, $namespace)] = $paths;
67
        }
68
69 7
        return $loader;
70
    }
71
72 5
    private static function mergePSR0And4(array $psr0, array $psr4): array
73
    {
74 5
        foreach ($psr0 as $namespace => $path) {
75
            //Append backslashes, if needed, since psr-0 does not require this
76 5
            if ('\\' !== substr($namespace, -1)) {
77 1
                $namespace .= '\\';
78
            }
79
80 5
            $path = self::updatePSR0Path($path, $namespace);
81
82 5
            if (!isset($psr4[$namespace])) {
83 2
                $psr4[$namespace] = $path;
84
85 2
                continue;
86
            }
87 3
            $psr4[$namespace] = self::mergeNamespaces($namespace, $path, $psr4);
88
        }
89
90 5
        return $psr4;
91
    }
92
93 5
    private static function updatePSR0Path($path, $namespace)
94
    {
95 5
        $namespaceForPsr = str_replace('\\', '/', $namespace);
96
97 5
        if (!is_array($path)) {
98 4
            if ('/' !== substr($path, -1)) {
99 1
                $path .= '/';
100
            }
101
102 4
            $path .= $namespaceForPsr.'/';
103
104 4
            return $path;
105
        }
106 2
        foreach ($path as $key => $item) {
107 2
            if ('/' !== substr($item, -1)) {
108 1
                $item .= '/';
109
            }
110
111 2
            $item .= $namespaceForPsr.'/';
112 2
            $path[$key] = $item;
113
        }
114
115 2
        return $path;
116
    }
117
118
    /**
119
     * Deals with the 4 possible scenarios:
120
     *       PSR0 | PSR4
121
     * array      |
122
     * string     |
123
     * or simply the namepace not existing as a psr-4 entry.
124
     *
125
     * @param string       $psr0Namespace
126
     * @param string|array $psr0Path
127
     * @param string|array $psr4
128
     *
129
     * @return string|array
130
     */
131 3
    private static function mergeNamespaces(string $psr0Namespace, $psr0Path, $psr4)
132
    {
133
        // Both strings
134 3
        if (is_string($psr4[$psr0Namespace]) && is_string($psr0Path)) {
135 1
            return [$psr4[$psr0Namespace], $psr0Path];
136
        }
137
        //psr-4 is string, and psr-0 is array
138 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...
139 1
            $psr0Path[] = $psr4[$psr0Namespace];
140
141 1
            return $psr0Path;
142
        }
143
144
        //psr-4 is array and psr-0 is string
145 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...
146 1
            $psr4[$psr0Namespace][] = $psr0Path;
147
148 1
            return $psr4[$psr0Namespace];
149
        }
150
151 1
        if (is_array($psr4[$psr0Namespace]) && is_array($psr0Path)) {
152 1
            return array_merge($psr4[$psr0Namespace], $psr0Path);
153
        }
154
155
        return $psr0Path;
156
    }
157
}
158