-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathHookReflector.php
111 lines (98 loc) · 2.67 KB
/
HookReflector.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
<?php
namespace Aivec\Plugins\DocParser\Importer;
use phpDocumentor\Reflection\BaseReflector;
use PHPParser_PrettyPrinter_Default;
/**
* Custom reflector for WordPress hooks.
*/
class HookReflector extends BaseReflector
{
/**
* Returns name
*
* @return string
*/
public function getName() {
$printer = new PHPParser_PrettyPrinter_Default();
return $this->cleanupName($printer->prettyPrintExpr($this->node->args[0]->value));
}
/**
* Cleans up name
*
* @param string $name
* @return string
*/
private function cleanupName($name) {
$matches = [];
// quotes on both ends of a string
if (preg_match('/^[\'"]([^\'"]*)[\'"]$/', $name, $matches)) {
return $matches[1];
}
// two concatenated things, last one of them a variable
if (
preg_match(
'/(?:[\'"]([^\'"]*)[\'"]\s*\.\s*)?' . // First filter name string (optional)
'(\$[^\s]*)' . // Dynamic variable
'(?:\s*\.\s*[\'"]([^\'"]*)[\'"])?/', // Second filter name string (optional)
$name,
$matches
)
) {
if (isset($matches[3])) {
return $matches[1] . '{' . $matches[2] . '}' . $matches[3];
} else {
return $matches[1] . '{' . $matches[2] . '}';
}
}
return $name;
}
/**
* Returns short name
*
* @return string
*/
public function getShortName() {
return $this->getName();
}
/**
* Returns type
*
* @return string
*/
public function getType() {
$type = 'filter';
switch ((string)$this->node->name) {
case 'do_action':
$type = 'action';
break;
case 'do_action_ref_array':
$type = 'action_reference';
break;
case 'do_action_deprecated':
$type = 'action_deprecated';
break;
case 'apply_filters_ref_array':
$type = 'filter_reference';
break;
case 'apply_filters_deprecated':
$type = 'filter_deprecated';
break;
}
return $type;
}
/**
* Returns arguments
*
* @return array
*/
public function getArgs() {
$printer = new PrettyPrinter();
$args = [];
foreach ($this->node->args as $arg) {
$args[] = $printer->prettyPrintArg($arg);
}
// Skip the filter name
array_shift($args);
return $args;
}
}