This repository was archived by the owner on Aug 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDocumentingReflectionMethod.inc.php
260 lines (236 loc) · 5.22 KB
/
DocumentingReflectionMethod.inc.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/**
* DocumentingReflectionMethod Class Definition File
*
* This file contains the definition for the documenting reflection method class
*
* @copyright Copyright 2009, Ian Selby
* @author Ian Selby <[email protected]>
* @license MIT License
* @version 1.0
*/
/**
* DocumentingReflectionMethod Class
*
* This class extends the ReflectionMethod class and adds the ability
* to parse a class method's doc block comments.
*
* @version 1.0
*/
class DocumentingReflectionMethod extends ReflectionMethod
{
/**
* Newline Token Number
*
* @var int
*/
const T_DOCBLOCK_NEWLINE = 1;
/**
* Whitespace Token Number
*
* @var int
*/
const T_DOCBLOCK_WHITESPACE = 2;
/**
* Text Token Number
*
* @var int
*/
const T_DOCBLOCK_TEXT = 36;
/**
* Tag Token Number
*
* @var int
*/
const T_DOCBLOCK_TAG = 5;
/**
* Map of Token Numbers to Token Names
*
* @var array
*/
protected static $tokenNames = array(1 => 'DOCBLOCK_NEWLINE', 2 => 'DOCBLOCK_WHITESPACE', 5 => 'DOCBLOCK_TAG', 36 => 'DOCBLOCK_TEXT');
/**
* Array of Newline Characters
*
* These characters count as newlines by themselves when being tokenized.
*
* @var array
*/
protected static $newLineChars = array('/**', '*', '*/');
/**
* Regex to Match Whitespace
*
* @var string
*/
protected static $whitespaceRegex = '/\*[\s\t]+/';
/**
* Regex to Match Tags
*/
protected static $tagRegex = '/@[a-zA-Z0-9]*\s/';
/**
* All Parsed Comments
*
* @var array
*/
protected $comments;
/**
* All Parsed Tags
*
* This is an associative array of tags with the keys as tag names
* and the values as the tag value
*
* @var array
*/
protected $tags;
/**
* The Raw Tokens
*
* This is an array of token groups. A token group is an array of individual
* tokens
*
* @var array
*/
protected $tokens;
/**
* The Declaring Class for the Method We're Tokenizing the DocBlock of
*
* @var Object
*/
protected $declaringClass;
/**
* Class Constructor
*
* @param Object $object Instance of the object that contains the method we're going to parse comments for
* @param string $method The name of the method to parse comments for
*
* @return void
*/
public function __construct ($object, $method)
{
parent::__construct($object, $method);
$docComment = $this->getDocComment();
$this->declaringClass = $object;
$this->tokenizeDocComment($docComment);
$this->parseTokens();
}
/**
* Outputs the Doc Tokens
*
* @return void
*/
public function printDocTokens ()
{
$return = '';
foreach ($this->tokens as $tokens)
{
foreach ($tokens as $token)
{
$return .= $token[0] . '=' . self::$tokenNames[$token[0]] . '=' . $token[1] . "<br />";
}
$return .= '<br />';
}
echo $return;
}
/**
* Returns DocumentingReflectionMethod::$tags
*
* @return array
*/
public function getTags ()
{
return $this->tags;
}
/**
* Returns DocumentingReflectionMethod::$comments
*
* @return array
*/
public function getComments ()
{
return $this->comments;
}
/**
* Parses the Tokens
*
* This function loops over all the token groups and splits the tags and comments
* into the appropriate arrays on this class
*
* @return void
*/
protected function parseTokens ()
{
foreach ($this->tokens as $tokens)
{
$tagName = null;
foreach ($tokens as $token)
{
if ($token[0] == DocumentingReflectionMethod::T_DOCBLOCK_NEWLINE || $token[0] == DocumentingReflectionMethod::T_DOCBLOCK_WHITESPACE)
{
continue;
}
if ($token[0] == DocumentingReflectionMethod::T_DOCBLOCK_TAG)
{
$tagName = $token[1];
}
if ($token[0] == DocumentingReflectionMethod::T_DOCBLOCK_TEXT)
{
if ($tagName !== null)
{
$this->tags[str_replace('@', '', $tagName)] = $token[1];
$tagName = null;
}
else
{
$this->comments[] = $token[1];
}
}
}
}
}
/**
* Runs the Tokenizing Routine on a Doc Comment
*
* @param string $docComment The method's doc comment
*
* @return void
*/
protected function tokenizeDocComment ($docComment)
{
$lines = explode("\n", $docComment);
foreach ($lines as $line)
{
$this->tokens[] = $this->tokenizeLine($line);
}
}
/**
* Tokenizes a Line in the Doc Comment
*
* @param string $line A doc comment line
*
* @return array
*/
protected function tokenizeLine ($line)
{
$lineTokens = array();
$line = trim($line);
// check for newline
if (in_array($line, self::$newLineChars))
{
$lineTokens[] = array(DocumentingReflectionMethod::T_DOCBLOCK_NEWLINE, "\n");
return $lineTokens;
}
if (preg_match(self::$whitespaceRegex, $line, $matches))
{
$lineTokens[] = array(DocumentingReflectionMethod::T_DOCBLOCK_WHITESPACE, $matches[0]);
$line = str_replace($matches[0], '', $line);
}
if (preg_match(self::$tagRegex, $line, $matches))
{
$lineTokens[] = array(DocumentingReflectionMethod::T_DOCBLOCK_TAG, trim($matches[0]));
$line = str_replace($matches[0], '', $line);
}
$lineTokens[] = array(DocumentingReflectionMethod::T_DOCBLOCK_TEXT, trim($line));
$lineTokens[] = array(DocumentingReflectionMethod::T_DOCBLOCK_NEWLINE, "\n");
return $lineTokens;
}
}