-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHeuristics.php
221 lines (196 loc) · 5.7 KB
/
Heuristics.php
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace dokuwiki\plugin\xref;
/**
* Figure out what to send to Grok to hopefully show the right, single hit
*/
class Heuristics
{
/** @var string the definition to search */
protected $def = '';
/** @var string the path to use */
protected $path = '';
/** @var array deprecated classes and their replacements */
protected $deprecations;
/**
* Try to gues what the given reference means and how to best search for it
*
* @param string $reference
*/
public function __construct($reference)
{
$this->loadDeprecations();
if ($reference !== '') $reference = $this->checkDeprecation($reference);
if ($reference !== '') $reference = $this->checkHash($reference);
if ($reference !== '') $reference = $this->checkFilename($reference);
if ($reference !== '') $reference = $this->checkNamespace($reference);
if ($reference !== '') $reference = $this->checkClassPrefix($reference);
if ($reference !== '') $reference = $this->checkVariable($reference);
if ($reference !== '') $reference = $this->checkFunction($reference);
if ($reference !== '') $reference = $this->checkPSRClass($reference);
if ($reference !== '') $this->def = $reference;
}
/**
* @return string
*/
public function getDef()
{
return trim(preg_replace('/[^\w]+/', '', $this->def));
}
/**
* @return string
*/
public function getPath()
{
return trim(preg_replace('/[^\w.]+/', ' ', $this->path));
}
/**
* @return array
*/
public function getDeprecations()
{
return $this->deprecations;
}
/**
* Replace deprecated classes
*
* @param string $reference
* @return string
*/
protected function checkDeprecation($reference)
{
if (isset($this->deprecations[$reference])) {
return $this->deprecations[$reference];
}
return $reference;
}
/**
* Handle things in the form path#symbol
*
* @param string $reference
* @return string
*/
protected function checkHash($reference)
{
if (strpos($reference, '#') === false) return $reference;
list($this->path, $this->def) = explode('#', $reference, 2);
return '';
}
/**
* Known file extension?
*
* @param string $reference
* @return mixed|string
*/
protected function checkFilename($reference)
{
if (preg_match('/\.(php|js|css|html)$/', $reference)) {
$this->def = '';
$this->path = $reference;
return '';
}
return $reference;
}
/**
* Namespaces are paths
*
* @param string $reference
* @return string
*/
protected function checkNamespace($reference)
{
if (strpos($reference, '\\') === false) return $reference;
$parts = explode('\\', $reference);
$parts = array_values(array_filter($parts));
$reference = array_pop($parts); // last part may be more than a class
// our classes are in inc
if ($parts[0] == 'dokuwiki') $parts[0] = 'inc';
$this->path = join(' ', $parts);
return $reference;
}
/**
* Is there something called on a class?
*/
protected function checkClassPrefix($reference)
{
if (
strpos($reference, '::') === false &&
strpos($reference, '->') === false
) {
return $reference;
}
list($class, $reference) = preg_split('/(::|->)/', $reference, 2);
$this->path .= ' ' . $class;
$this->def = $reference;
return '';
}
/**
* Clearly a variable
*
* @param string $reference
* @return string
*/
protected function checkVariable($reference)
{
if ($reference[0] == '$') {
$this->def = $reference;
return '';
}
return $reference;
}
/**
* It's a function
*
* @param string $reference
* @return string
*/
protected function checkFunction($reference)
{
if (substr($reference, -2) == '()') {
$this->def = $reference;
return '';
}
if (preg_match('/\(.+?\)$/', $reference)) {
[$reference, /* $arguments */] = explode('(', $reference, 2);
$this->def = $reference;
return '';
}
return $reference;
}
/**
* Upercase followed by lowercase letter, must be a class
*
* Those are in their own files, so add it to the path
* @param $reference
* @return mixed|string
*/
protected function checkPSRClass($reference)
{
if (preg_match('/^[A-Z][a-z]/', $reference)) {
$this->def = $reference;
$this->path .= ' ' . $reference;
return '';
}
return $reference;
}
/**
* Load deprecated classes info
*/
protected function loadDeprecations()
{
$this->deprecations = [];
// class aliases
$legacy = file_get_contents(DOKU_INC . 'inc/legacy.php');
if (preg_match_all('/class_alias\(\'([^\']+)\', *\'([^\']+)\'\)/', $legacy, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$this->deprecations[$match[2]] = $match[1];
}
}
// deprecated classes
$deprecations = file_get_contents(DOKU_INC . 'inc/deprecated.php');
if (preg_match_all('/class (.+?) extends (\\\\dokuwiki\\\\.+?)(\s|$|{)/', $deprecations, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$this->deprecations[$match[1]] = $match[2];
}
}
}
}