Skip to content

Commit 8693c99

Browse files
committed
WIP refactored docblock class/method parsing into getDocblock() method
1 parent 5ffea22 commit 8693c99

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/CodeCoverage/Util/Tokenizer.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,47 @@ private function getVisibility(array $tokens, $idx)
224224
}
225225
}
226226

227+
/**
228+
* Get the docblock for this token
229+
*
230+
* This method will fetch the docblock belonging to the current token. The
231+
* docblock must be placed on the line directly above the token to be
232+
* recognized.
233+
*
234+
* @return string|null Returns the docblock as a string if found
235+
*/
236+
private function getDocblock(array $tokens, $idx) {
237+
$currentLineNumber = $this->tline($tokens[$idx]);
238+
$prevLineNumber = $currentLineNumber - 1;
239+
240+
for ($i = $idx - 1; $i; $i--) {
241+
if (!isset($tokens[$i])) {
242+
return;
243+
}
244+
245+
$token = $tokens[$i];
246+
$tconst = $this->tconst($token);
247+
248+
if ($tconst === T_FUNCTION || $tconst === T_CLASS || $tconst === T_TRAIT) {
249+
// Some other trait, class or function, no docblock can be
250+
// used for the current token
251+
break;
252+
}
253+
254+
$line = $this->tline($token);
255+
256+
if ($line == $currentLineNumber || ($line == $prevLineNumber && $tconst === T_WHITESPACE)) {
257+
continue;
258+
}
259+
260+
if ($line < $currentLineNumber && $tconst === T_DOC_COMMENT) {
261+
break;
262+
}
263+
264+
return (string)$token;
265+
}
266+
}
267+
227268
public function tokenize() {
228269
$sourceCode = file_get_contents($this->filename);
229270
$tokens = token_get_all($sourceCode);
@@ -272,7 +313,7 @@ public function tokenize() {
272313
'parent' => $this->getParent($tokens, $i),
273314
'interfaces'=> $this->getInterfaces($tokens, $i),
274315
'keywords' => $this->getKeywords($tokens, $i),
275-
'docblock' => $token->getDocblock(),
316+
'docblock' => $this->getDocblock($tokens, $i),
276317
'startLine' => $this->tline($token),
277318
'endLine' => $token->getEndLine(),
278319
'package' => $token->getPackage(),
@@ -293,7 +334,7 @@ public function tokenize() {
293334
case 'PHP_Token_FUNCTION':
294335
$tname = $this->tname($tokens, $idx);
295336
$tmp = array(
296-
'docblock' => $token->getDocblock(),
337+
'docblock' => $this->getDocblock($tokens, $i),
297338
'keywords' => $this->getKeywords($tokens, $i),
298339
'visibility'=> $this->getVisibility($tokens, $i),
299340
'signature' => $token->getSignature(),

0 commit comments

Comments
 (0)