-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathNamespaces.php
More file actions
191 lines (181 loc) · 6.04 KB
/
Namespaces.php
File metadata and controls
191 lines (181 loc) · 6.04 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?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\Namespaces;
use Patchwork\CodeManipulation\Source;
use Patchwork\CodeManipulation\Actions\Generic;
/**
* @since 2.1.0
*/
function resolveName(Source $s, $pos, $type = 'class')
{
$name = scanQualifiedName($s, $pos);
$pieces = explode('\\', $name);
if ($pieces[0] === '') {
return $name;
}
$uses = collectUseDeclarations($s, $pos);
if (isset($uses[$type][$name])) {
return '\\' . ltrim($uses[$type][$name], ' \\');
}
if (isset($uses['class'][$pieces[0]])) {
$name = '\\' . ltrim($uses['class'][$pieces[0]] . '\\' . join('\\', array_slice($pieces, 1)), '\\');
} else {
$name = '\\' . ltrim(getNamespaceAt($s, $pos) . '\\' . $name, '\\');
}
return $name;
}
/**
* @since 2.1.0
*/
function getNamespaceAt(Source $s, $pos)
{
foreach (collectNamespaceBoundaries($s) as $namespace => $boundaryPairs) {
foreach ($boundaryPairs as $boundaries) {
list($begin, $end) = $boundaries;
if ($begin <= $pos && $pos <= $end) {
return $namespace;
}
}
}
return '';
}
function collectNamespaceBoundaries(Source $s)
{
// phpcs:disable PHPCompatibility.FunctionDeclarations.NewClosure.ThisFoundOutsideClass -- Using $this is okay. Source::cache() binds the closure.
return $s->cache([], function () {
if (!$this->has(T_NAMESPACE)) {
return ['' => [[0, INF]]];
}
$result = [];
foreach ($this->all(T_NAMESPACE) as $keyword) {
if ($this->next(';', $keyword) < $this->next(Generic\LEFT_CURLY, $keyword)) {
return [scanQualifiedName($this, $keyword + 1) => [[0, INF]]];
}
$begin = $this->next(Generic\LEFT_CURLY, $keyword) + 1;
$end = $this->match($begin - 1) - 1;
$name = scanQualifiedName($this, $keyword + 1);
if (!isset($result[$name])) {
$result[$name] = [];
}
$result[$name][] = [$begin, $end];
}
return $result;
});
// phpcs:enable
}
function collectUseDeclarations(Source $s, $begin)
{
foreach (collectNamespaceBoundaries($s) as $boundaryPairs) {
foreach ($boundaryPairs as $boundaries) {
list($leftBoundary, $rightBoundary) = $boundaries;
if ($leftBoundary <= $begin && $begin <= $rightBoundary) {
$begin = $leftBoundary;
break;
}
}
}
// phpcs:disable PHPCompatibility.FunctionDeclarations.NewClosure.ThisFoundOutsideClass -- Using $this is okay. Source::cache() binds the closure.
return $s->cache([$begin], function ($begin) {
$result = ['class' => [], 'function' => [], 'const' => []];
# only tokens that are siblings bracket-wise are considered,
# so trait-use instances are not an issue
foreach ($this->siblings(T_USE, $begin) as $keyword) {
# skip if closure-use
$next = $this->skip(Source::junk(), $keyword);
if ($this->is(Generic\LEFT_ROUND, $next)) {
continue;
}
parseUseDeclaration($this, $next, $result);
}
return $result;
});
// phpcs:enable
}
function parseUseDeclaration(Source $s, $pos, array &$aliases, $prefix = '', $type = 'class')
{
$lastPart = null;
$whole = $prefix;
while (true) {
switch ($s->tokens[$pos][Source::TYPE_OFFSET]) {
case T_FUNCTION:
$type = 'function';
break;
case T_CONST:
$type = 'const';
break;
case T_NS_SEPARATOR:
if (!empty($whole)) {
$whole .= '\\';
}
break;
case T_STRING:
case Generic\NAME_FULLY_QUALIFIED:
case Generic\NAME_QUALIFIED:
case Generic\NAME_RELATIVE:
$update = $s->tokens[$pos][Source::STRING_OFFSET];
$parts = explode('\\', $update);
$whole .= $update;
$lastPart = end($parts);
break;
case T_AS:
$pos = $s->skip(Source::junk(), $pos);
$aliases[$type][$s->tokens[$pos][Source::STRING_OFFSET]] = $whole;
$lastPart = null;
$whole = $prefix;
break;
case ',':
if ($lastPart !== null) {
$aliases[$type][$lastPart] = $whole;
}
$lastPart = null;
$whole = $prefix;
$type = 'class';
break;
case Generic\LEFT_CURLY:
parseUseDeclaration($s, $pos + 1, $aliases, $prefix . '\\', $type);
break;
case T_WHITESPACE:
case T_COMMENT:
case T_DOC_COMMENT:
break;
default:
if ($lastPart !== null) {
$aliases[$type][$lastPart] = $whole;
}
return;
}
$pos++;
}
}
function scanQualifiedName(Source $s, $begin)
{
$result = '';
while (true) {
switch ($s->tokens[$begin][Source::TYPE_OFFSET]) {
case T_NS_SEPARATOR:
if (!empty($result)) {
$result .= '\\';
}
# fall through
case T_STRING:
case Generic\NAME_FULLY_QUALIFIED:
case Generic\NAME_QUALIFIED:
case Generic\NAME_RELATIVE:
case T_STATIC:
$result .= $s->tokens[$begin][Source::STRING_OFFSET];
break;
case T_WHITESPACE:
case T_COMMENT:
case T_DOC_COMMENT:
break;
default:
return str_replace('\\\\', '\\', $result);
}
$begin++;
}
}