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
|
<?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; }
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])) { if ($bufferComments[0]->getLine() === $value[2] - 1) { $newFunction->addComment($bufferComments[0]->getComment()); } }
array_unshift($bufferFunctions, $newFunction); $k = $j; } break; } break;
case T_COMMENT: $comment = $this->parsePhpComment($value[1]);
if ($comment !== null) { array_unshift($bufferComments, new ParsedComment($comment, $value[2]));
if (isset($bufferFunctions[0])) { $bufferFunctions[0]->addComment($comment); } } break;
default: if (isset($bufferFunctions[0])) { $bufferFunctions[0]->stopArgument(); } break; } }
return $functions; }
protected function parsePhpComment($value) { $result = null;
if ($this->extractComments !== false) { if ($value[0] === '#') { $value = substr($value, 1); } elseif ($value[1] === '/') { $value = substr($value, 2); } else { $value = substr($value, 2, -2); }
$value = trim($value);
if ($value !== '') { if (is_array($this->extractComments)) { foreach ($this->extractComments as $string) { if (strpos($value, $string) === 0) { $result = $value; break; } } } elseif ($this->extractComments === '' || strpos($value, $this->extractComments) === 0) { $result = $value; } } }
return $result; } }
|