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; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\Console\Application; |
18
|
|
|
use Humbug\PhpScoper\Console\ApplicationFactory; |
19
|
|
|
use Iterator; |
20
|
|
|
use PackageVersions\Versions; |
21
|
|
|
use PhpParser\Node; |
22
|
|
|
use PhpParser\Node\Identifier; |
23
|
|
|
use PhpParser\Node\Name; |
24
|
|
|
use function array_map; |
25
|
|
|
use function array_pop; |
26
|
|
|
use function count; |
27
|
|
|
use function is_array; |
28
|
|
|
use function is_object; |
29
|
|
|
use function is_scalar; |
30
|
|
|
use function is_string; |
31
|
|
|
use function method_exists; |
32
|
|
|
use function serialize; |
33
|
|
|
use function str_split; |
34
|
|
|
use function strrpos; |
35
|
|
|
use function substr; |
36
|
|
|
use function unserialize; |
37
|
|
|
|
38
|
|
|
function create_application(): Application |
39
|
|
|
{ |
40
|
|
|
return (new ApplicationFactory())->create(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
function get_php_scoper_version(): string |
44
|
|
|
{ |
45
|
|
|
$rawVersion = Versions::getVersion('humbug/php-scoper'); |
46
|
|
|
|
47
|
|
|
[$prettyVersion, $commitHash] = explode('@', $rawVersion); |
48
|
|
|
|
49
|
|
|
return $prettyVersion.'@'.substr($commitHash, 0, 7); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string[] $paths Absolute paths |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
function get_common_path(array $paths): string |
58
|
|
|
{ |
59
|
|
|
$nbPaths = count($paths); |
60
|
|
|
if (0 === $nbPaths) { |
61
|
|
|
return ''; |
62
|
|
|
} |
63
|
|
|
$pathRef = array_pop($paths); |
64
|
|
|
if (1 === $nbPaths) { |
65
|
|
|
$commonPath = $pathRef; |
66
|
|
|
} else { |
67
|
|
|
$commonPath = ''; |
68
|
|
|
foreach (str_split($pathRef) as $pos => $char) { |
69
|
|
|
foreach ($paths as $path) { |
70
|
|
|
if (!isset($path[$pos]) || $path[$pos] !== $char) { |
71
|
|
|
break 2; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
$commonPath .= $char; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
foreach (['/', '\\'] as $separator) { |
78
|
|
|
$lastSeparatorPos = strrpos($commonPath, $separator); |
79
|
|
|
if (false !== $lastSeparatorPos) { |
80
|
|
|
$commonPath = rtrim(substr($commonPath, 0, $lastSeparatorPos), $separator); |
81
|
|
|
|
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $commonPath; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* In-house clone functions. Does a partial clone that should be enough to provide the immutability required in some |
91
|
|
|
* places for the scoper. It however does not guarantee a deep cloning as would be horribly slow for no good reasons. |
92
|
|
|
* A better alternative would be to find a way to push immutability upstream in PHP-Parser directly. |
93
|
|
|
* |
94
|
|
|
* @param Node $node |
95
|
|
|
* |
96
|
|
|
* @return Node |
97
|
|
|
*/ |
98
|
|
|
function clone_node(Node $node): Node |
99
|
|
|
{ |
100
|
|
|
$clone = deep_clone($node); |
101
|
|
|
|
102
|
|
|
foreach ($node->getAttributes() as $key => $attribute) { |
103
|
|
|
$clone->setAttribute($key, $attribute); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $clone; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param mixed $node |
111
|
|
|
* |
112
|
|
|
* @return mixed |
113
|
|
|
* |
114
|
|
|
* @internal |
115
|
|
|
*/ |
116
|
|
|
function deep_clone($node) |
117
|
|
|
{ |
118
|
|
|
if (is_array($node)) { |
119
|
|
|
return array_map(__FUNCTION__, $node); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (null === $node || is_scalar($node)) { |
123
|
|
|
return $node; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return unserialize(serialize($node)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
function chain(iterable ...$iterables): Iterator |
130
|
|
|
{ |
131
|
|
|
foreach ($iterables as $iterable) { |
132
|
|
|
foreach ($iterable as $key => $value) { |
133
|
|
|
yield $key => $value; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
function is_stringable($value): bool |
139
|
|
|
{ |
140
|
|
|
return |
141
|
|
|
null === $value |
142
|
|
|
|| is_string($value) |
143
|
|
|
|| $value instanceof Name |
144
|
|
|
|| $value instanceof Identifier |
145
|
|
|
|| (is_object($value) && method_exists($value, '__toString')) |
146
|
|
|
; |
147
|
|
|
} |
148
|
|
|
|