-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathPhpFunctionsScanner.php
199 lines (168 loc) · 5.91 KB
/
PhpFunctionsScanner.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
<?php
namespace Gettext\Utils;
use Gettext\Extractors\PhpCode;
class PhpFunctionsScanner extends FunctionsScanner
{
/**
* PHP tokens of the code to be parsed.
*
* @var array
*/
protected $tokens;
/**
* If not false, comments will be extracted.
*
* @var string|false|array
*/
protected $extractComments = false;
/**
* Enable extracting comments that start with a tag (if $tag is empty all the comments will be extracted).
*
* @param mixed $tag
*/
public function enableCommentsExtraction($tag = '')
{
$this->extractComments = $tag;
}
/**
* Disable comments extraction.
*/
public function disableCommentsExtraction()
{
$this->extractComments = false;
}
/**
* Constructor.
*
* @param string $code The php code to scan
*/
public function __construct($code)
{
$this->tokens = array_values(
array_filter(
token_get_all($code),
function ($token) {
return !is_array($token) || $token[0] !== T_WHITESPACE;
}
)
);
}
/**
* {@inheritdoc}
*/
public function getFunctions(array $constants = [])
{
$count = count($this->tokens);
/* @var ParsedFunction[] $bufferFunctions */
$bufferFunctions = [];
/* @var ParsedComment[] $bufferComments */
$bufferComments = [];
/* @var array $functions */
$functions = [];
for ($k = 0; $k < $count; ++$k) {
$value = $this->tokens[$k];
if (is_string($value)) {
if (isset($bufferFunctions[0])) {
switch ($value) {
case ',':
$bufferFunctions[0]->nextArgument();
break;
case ')':
$functions[] = array_shift($bufferFunctions)->close();
break;
case '.':
break;
default:
$bufferFunctions[0]->stopArgument();
break;
}
}
continue;
}
if (defined('T_NAME_FULLY_QUALIFIED') && T_NAME_FULLY_QUALIFIED === $value[0]) {
$value[0] = T_STRING;
$value[1] = ltrim($value[1], '\\');
}
switch ($value[0]) {
case T_CONSTANT_ENCAPSED_STRING:
//add an argument to the current function
if (isset($bufferFunctions[0])) {
$bufferFunctions[0]->addArgumentChunk(PhpCode::convertString($value[1]));
}
break;
case T_STRING:
if (isset($bufferFunctions[0])) {
if (isset($constants[$value[1]])) {
$bufferFunctions[0]->addArgumentChunk($constants[$value[1]]);
break;
}
if (strtolower($value[1]) === 'null') {
$bufferFunctions[0]->addArgumentChunk(null);
break;
}
$bufferFunctions[0]->stopArgument();
}
//new function found
for ($j = $k + 1; $j < $count; ++$j) {
$nextToken = $this->tokens[$j];
if (is_array($nextToken) && $nextToken[0] === T_COMMENT) {
continue;
}
if ($nextToken === '(') {
$newFunction = new ParsedFunction($value[1], $value[2]);
// add comment that was on the line before.
if (isset($bufferComments[0])) {
$comment = $bufferComments[0];
if ($comment->isRelatedWith($newFunction)) {
$newFunction->addComment($comment);
}
}
array_unshift($bufferFunctions, $newFunction);
$k = $j;
}
break;
}
break;
case T_COMMENT:
$comment = $this->parsePhpComment($value[1], $value[2]);
if ($comment) {
array_unshift($bufferComments, $comment);
// The comment is inside the function call.
if (isset($bufferFunctions[0])) {
$bufferFunctions[0]->addComment($comment);
}
}
break;
default:
if (isset($bufferFunctions[0])) {
$bufferFunctions[0]->stopArgument();
}
break;
}
}
return $functions;
}
/**
* Extract the actual text from a PHP comment.
*
* If set, only returns comments that match the prefix(es).
*
* @param string $value The PHP comment.
* @param int $line Line number.
*
* @return null|ParsedComment Comment or null if comment extraction is disabled or if there is a prefix mismatch.
*/
protected function parsePhpComment($value, $line)
{
if ($this->extractComments === false) {
return null;
}
//this returns a comment or null
$comment = ParsedComment::create($value, $line);
$prefixes = array_filter((array) $this->extractComments);
if ($comment && $comment->checkPrefixes($prefixes)) {
return $comment;
}
return null;
}
}