-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathRedefinitionOfInternals.php
More file actions
156 lines (147 loc) · 5.48 KB
/
RedefinitionOfInternals.php
File metadata and controls
156 lines (147 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* @link http://patchwork2.org/
* @author Ignas Rudaitis <ignas.rudaitis@gmail.com>
* @copyright 2010-2018 Ignas Rudaitis
* @license http://www.opensource.org/licenses/mit-license.html
*/
namespace Patchwork\CodeManipulation\Actions\RedefinitionOfInternals;
use Patchwork\Config;
use Patchwork\CallRerouting;
use Patchwork\CodeManipulation\Source;
use Patchwork\CodeManipulation\Actions\Generic;
use Patchwork\CodeManipulation\Actions\Namespaces;
const DYNAMIC_CALL_REPLACEMENT = '\Patchwork\CallRerouting\dispatchDynamic(%s, \Patchwork\Utils\args(%s))';
function spliceNamedFunctionCalls()
{
if (Config\getRedefinableInternals() === []) {
return function () {
};
}
$names = [];
foreach (Config\getRedefinableInternals() as $name) {
$names[strtolower($name)] = true;
}
return function (Source $s) use ($names) {
foreach (Namespaces\collectNamespaceBoundaries($s) as $namespace => $boundaryList) {
foreach ($boundaryList as $boundaries) {
list($begin, $end) = $boundaries;
$aliases = Namespaces\collectUseDeclarations($s, $begin)['function'];
# Receive all aliases, leave only those for redefinable internals
foreach ($aliases as $alias => $qualified) {
if (!isset($names[$qualified])) {
unset($aliases[$alias]);
} else {
$aliases[strtolower($alias)] = strtolower($qualified);
}
}
spliceNamedCallsWithin($s, $begin, $end, $names, $aliases);
}
}
};
}
function spliceNamedCallsWithin(Source $s, $begin, $end, array $names, array $aliases)
{
$tokenList = [
T_FUNCTION,
T_OBJECT_OPERATOR,
T_DOUBLE_COLON,
T_STRING,
T_NEW,
Generic\NAME_FULLY_QUALIFIED,
Generic\NAME_QUALIFIED,
Generic\NAME_RELATIVE,
];
foreach ($s->within([T_STRING, Generic\NAME_FULLY_QUALIFIED, Generic\NAME_QUALIFIED, Generic\NAME_RELATIVE], $begin, $end) as $string) {
$original = strtolower($s->read($string));
if ($original[0] == '\\') {
$original = substr($original, 1);
}
if (isset($names[$original]) || isset($aliases[$original])) {
$previous = $s->skipBack(Source::junk(), $string);
$hadBackslash = false;
if ($s->is(T_NS_SEPARATOR, $previous) || $s->is(Generic\NAME_FULLY_QUALIFIED, $string)) {
if (!isset($names[$original])) {
# use-aliased name cannot have a leading backslash
continue;
}
if ($s->is(T_NS_SEPARATOR, $previous)) {
$s->splice('', $previous, 1);
$previous = $s->skipBack(Source::junk(), $previous);
}
$hadBackslash = true;
}
if ($s->is($tokenList, $previous)) {
continue;
}
$next = $s->skip(Source::junk(), $string);
if (!$s->is(Generic\LEFT_ROUND, $next)) {
continue;
}
if (isset($aliases[$original])) {
$original = $aliases[$original];
}
$secondNext = $s->skip(Source::junk(), $next);
$splice = '\\' . CallRerouting\INTERNAL_REDEFINITION_NAMESPACE . '\\';
$splice .= $original . Generic\LEFT_ROUND;
# prepend a namespace-of-origin argument to handle cases like Acme\time() vs time()
$splice .= !$hadBackslash ? '__NAMESPACE__' : '""';
if (!$s->is(Generic\RIGHT_ROUND, $secondNext)) {
# right parenthesis doesn't follow immediately => there are arguments
$splice .= ', ';
}
$s->splice($splice, $string, $secondNext - $string);
}
}
}
function spliceDynamicCalls()
{
if (Config\getRedefinableInternals() === []) {
return function () {
};
}
return function (Source $s) {
spliceDynamicCallsWithin($s, 0, count($s->tokens) - 1);
};
}
function spliceDynamicCallsWithin(Source $s, $first, $last)
{
$pos = $first;
$anchor = INF;
$suppress = false;
while ($pos <= $last) {
switch ($s->tokens[$pos][Source::TYPE_OFFSET]) {
case '$':
case T_VARIABLE:
$anchor = min($pos, $anchor);
break;
case Generic\LEFT_ROUND:
if ($anchor !== INF && !$suppress) {
$callable = $s->read($anchor, $pos - $anchor);
$arguments = $s->read($pos + 1, $s->match($pos) - $pos - 1);
$pos = $s->match($pos);
$replacement = sprintf(DYNAMIC_CALL_REPLACEMENT, $callable, $arguments);
$s->splice($replacement, $anchor, $pos - $anchor + 1);
}
break;
case Generic\LEFT_SQUARE:
case Generic\LEFT_CURLY:
spliceDynamicCallsWithin($s, $pos + 1, $s->match($pos) - 1);
$pos = $s->match($pos);
break;
case T_WHITESPACE:
case T_COMMENT:
case T_DOC_COMMENT:
break;
case T_OBJECT_OPERATOR:
case T_DOUBLE_COLON:
case T_NEW:
$suppress = true;
break;
default:
$suppress = false;
$anchor = INF;
}
$pos++;
}
}