|
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 Humbug\PhpScoper\Whitelist; |
|
18
|
|
|
use stdClass; |
|
19
|
|
|
use function array_map; |
|
20
|
|
|
use function is_array; |
|
21
|
|
|
use function is_string; |
|
22
|
|
|
use function str_replace; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @private |
|
26
|
|
|
*/ |
|
27
|
|
|
final class AutoloadPrefixer |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @param stdClass $contents Decoded JSON |
|
31
|
|
|
* @param string $prefix |
|
32
|
|
|
* |
|
33
|
|
|
* @return stdClass Prefixed decoded JSON |
|
34
|
|
|
*/ |
|
35
|
9 |
|
public static function prefixPackageAutoloadStatements(stdClass $contents, string $prefix, Whitelist $whitelist): stdClass |
|
36
|
|
|
{ |
|
37
|
9 |
|
if (isset($contents->autoload)) { |
|
38
|
9 |
|
$contents->autoload = self::prefixAutoloadStatements($contents->autoload, $prefix, $whitelist); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
9 |
|
if (isset($contents->{'autoload-dev'})) { |
|
42
|
3 |
|
$contents->{'autoload-dev'} = self::prefixAutoloadStatements($contents->{'autoload-dev'}, $prefix, $whitelist); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
9 |
|
if (isset($contents->extra, $contents->extra->laravel, $contents->extra->laravel->providers)) { |
|
46
|
1 |
|
$contents->extra->laravel->providers = self::prefixLaravelProviders($contents->extra->laravel->providers, $prefix, $whitelist); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
9 |
|
return $contents; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
9 |
|
private static function prefixAutoloadStatements(stdClass $autoload, string $prefix, Whitelist $whitelist): stdClass |
|
53
|
|
|
{ |
|
54
|
9 |
|
if (false === isset($autoload->{'psr-4'}) && false === isset($autoload->{'psr-0'})) { |
|
55
|
1 |
|
return $autoload; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
8 |
|
if (isset($autoload->{'psr-0'})) { |
|
59
|
5 |
|
$autoload->{'psr-4'} = self::mergePSR0And4( |
|
60
|
5 |
|
(array) $autoload->{'psr-0'}, |
|
61
|
5 |
|
(array) ($autoload->{'psr-4'} ?? new stdClass()) |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
8 |
|
unset($autoload->{'psr-0'}); |
|
65
|
|
|
|
|
66
|
8 |
|
if (isset($autoload->{'psr-4'})) { |
|
67
|
8 |
|
$autoload->{'psr-4'} = self::prefixAutoload((array) $autoload->{'psr-4'}, $prefix, $whitelist); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
8 |
|
return $autoload; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
8 |
|
private static function prefixAutoload(array $autoload, string $prefix, Whitelist $whitelist): array |
|
74
|
|
|
{ |
|
75
|
8 |
|
$loader = []; |
|
76
|
|
|
|
|
77
|
8 |
|
foreach ($autoload as $namespace => $paths) { |
|
78
|
8 |
|
$newNamespace = $whitelist->belongsToWhitelistedNamespace($namespace) |
|
79
|
|
|
? $namespace |
|
80
|
8 |
|
: sprintf('%s\\%s', $prefix, $namespace) |
|
81
|
|
|
; |
|
82
|
|
|
|
|
83
|
8 |
|
$loader[$newNamespace] = $paths; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
8 |
|
return $loader; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param (string|string[])[] $psr0 |
|
91
|
|
|
* @param (string|string[])[] $psr4 |
|
92
|
|
|
* |
|
93
|
|
|
* @return (string|string[])[] |
|
94
|
|
|
*/ |
|
95
|
5 |
|
private static function mergePSR0And4(array $psr0, array $psr4): array |
|
96
|
|
|
{ |
|
97
|
5 |
|
foreach ($psr0 as $namespace => $path) { |
|
98
|
|
|
//Append backslashes, if needed, since psr-0 does not require this |
|
99
|
5 |
|
if ('\\' !== substr($namespace, -1)) { |
|
100
|
1 |
|
$namespace .= '\\'; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
5 |
|
$path = self::updatePSR0Path($path, (string) $namespace); |
|
104
|
|
|
|
|
105
|
5 |
|
if (!isset($psr4[$namespace])) { |
|
106
|
2 |
|
$psr4[$namespace] = $path; |
|
107
|
|
|
|
|
108
|
2 |
|
continue; |
|
109
|
|
|
} |
|
110
|
3 |
|
$psr4[$namespace] = self::mergeNamespaces((string) $namespace, $path, $psr4); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
5 |
|
return $psr4; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string|string[] $path |
|
118
|
|
|
* |
|
119
|
|
|
* @return string|string[] |
|
120
|
|
|
*/ |
|
121
|
5 |
|
private static function updatePSR0Path($path, string $namespace) |
|
122
|
|
|
{ |
|
123
|
5 |
|
$namespaceForPsr = str_replace('\\', '/', $namespace); |
|
124
|
|
|
|
|
125
|
5 |
|
if (false === is_array($path)) { |
|
126
|
4 |
|
if ('/' !== substr($path, -1)) { |
|
127
|
1 |
|
$path .= '/'; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
4 |
|
$path .= $namespaceForPsr.'/'; |
|
131
|
|
|
|
|
132
|
4 |
|
return $path; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
2 |
|
foreach ($path as $key => $item) { |
|
136
|
2 |
|
if ('/' !== substr($item, -1)) { |
|
137
|
1 |
|
$item .= '/'; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
2 |
|
$item .= $namespaceForPsr.'/'; |
|
141
|
2 |
|
$path[$key] = $item; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
2 |
|
return $path; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Deals with the 4 possible scenarios: |
|
149
|
|
|
* PSR0 | PSR4 |
|
150
|
|
|
* array | |
|
151
|
|
|
* string | |
|
152
|
|
|
* or simply the namespace not existing as a psr-4 entry. |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $psr0Namespace |
|
155
|
|
|
* @param string|string[] $psr0Path |
|
156
|
|
|
* @param (string|string[])[] $psr4 |
|
157
|
|
|
* |
|
158
|
|
|
* @return string|string[] |
|
159
|
|
|
*/ |
|
160
|
3 |
|
private static function mergeNamespaces(string $psr0Namespace, $psr0Path, array $psr4) |
|
161
|
|
|
{ |
|
162
|
|
|
// Both strings |
|
163
|
3 |
|
if (is_string($psr4[$psr0Namespace]) && is_string($psr0Path)) { |
|
164
|
1 |
|
return [$psr4[$psr0Namespace], $psr0Path]; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
// PSR-4 is string, and PSR-0 is array |
|
168
|
2 |
View Code Duplication |
if (is_string($psr4[$psr0Namespace]) && is_array($psr0Path)) { |
|
|
|
|
|
|
169
|
1 |
|
$psr0Path[] = $psr4[$psr0Namespace]; |
|
170
|
|
|
|
|
171
|
1 |
|
return $psr0Path; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
// Psr-4 is array and psr-0 is string |
|
175
|
2 |
View Code Duplication |
if (is_array($psr4[$psr0Namespace]) && is_string($psr0Path)) { |
|
|
|
|
|
|
176
|
1 |
|
$psr4[$psr0Namespace][] = $psr0Path; |
|
177
|
|
|
|
|
178
|
1 |
|
return $psr4[$psr0Namespace]; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
1 |
|
if (is_array($psr4[$psr0Namespace]) && is_array($psr0Path)) { |
|
182
|
1 |
|
return array_merge($psr4[$psr0Namespace], $psr0Path); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $psr0Path; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
1 |
|
private static function prefixLaravelProviders(array $providers, string $prefix, Whitelist $whitelist): array |
|
189
|
|
|
{ |
|
190
|
1 |
|
return array_map( |
|
191
|
|
|
static function (string $provider) use ($prefix, $whitelist): string { |
|
192
|
1 |
|
return $whitelist->belongsToWhitelistedNamespace($provider) |
|
193
|
|
|
? $provider |
|
194
|
1 |
|
: sprintf('%s\\%s', $prefix, $provider) |
|
195
|
|
|
; |
|
196
|
1 |
|
}, |
|
197
|
1 |
|
$providers |
|
198
|
|
|
); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.