Skip to content

Commit c395ff8

Browse files
committed
WIP refactored endline parsing into getEndLine() method
1 parent 8693c99 commit c395ff8

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

src/CodeCoverage/Util/Tokenizer.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,51 @@ private function getDocblock(array $tokens, $idx) {
265265
}
266266
}
267267

268+
/**
269+
* @return integer
270+
*/
271+
private function getEndTokenId(array $tokens, $idx)
272+
{
273+
$block = 0;
274+
$i = $idx;
275+
$endTokenId = null;
276+
277+
while ($endTokenId === null && isset($tokens[$i])) {
278+
$token = $tokens[$i];
279+
$tclass = $this->tclass($token);
280+
$tconst = $this->tconst($token);
281+
if ($tclass === 'PHP_Token_OPEN_CURLY' || $tclass === 'PHP_Token_CURLY_OPEN') {
282+
$block++;
283+
} elseif ($tclass === 'PHP_Token_CLOSE_CURLY') {
284+
$block--;
285+
286+
if ($block === 0) {
287+
$endTokenId = $i;
288+
}
289+
} elseif (($tconst === T_FUNCTION || $tconst === T_NAMESPACE) && $tclass === 'PHP_Token_SEMICOLON') {
290+
if ($block === 0) {
291+
$endTokenId = $i;
292+
}
293+
}
294+
295+
$i++;
296+
}
297+
298+
if ($endTokenId === null) {
299+
$endTokenId = $idx;
300+
}
301+
302+
return $endTokenId;
303+
}
304+
305+
/**
306+
* @return integer
307+
*/
308+
private function getEndLine(array $tokens, $idx)
309+
{
310+
return $this->tline($tokens[$this->getEndTokenId($tokens, $idx)]);
311+
}
312+
268313
public function tokenize() {
269314
$sourceCode = file_get_contents($this->filename);
270315
$tokens = token_get_all($sourceCode);
@@ -308,25 +353,27 @@ public function tokenize() {
308353

309354
case 'PHP_Token_CLASS':
310355
case 'PHP_Token_TRAIT':
356+
$endLine = $this->getEndLine($tokens, $i);
357+
311358
$tmp = array(
312359
'methods' => array(),
313360
'parent' => $this->getParent($tokens, $i),
314361
'interfaces'=> $this->getInterfaces($tokens, $i),
315362
'keywords' => $this->getKeywords($tokens, $i),
316363
'docblock' => $this->getDocblock($tokens, $i),
317364
'startLine' => $this->tline($token),
318-
'endLine' => $token->getEndLine(),
365+
'endLine' => $endLine,
319366
'package' => $token->getPackage(),
320367
'file' => $this->filename
321368
);
322369

323370
if ($this->tconst($token) === T_CLASS) {
324371
$class = $this->tname($tokens, $i + 2);
325-
$classEndLine = $token->getEndLine();
372+
$classEndLine = $endLine;
326373
$this->classes[$class] = $tmp;
327374
} else {
328375
$trait = $this->tname($tokens, $i + 2);
329-
$traitEndLine = $token->getEndLine();
376+
$traitEndLine = $endLine;
330377
$this->traits[$trait] = $tmp;
331378
}
332379
break;
@@ -339,7 +386,7 @@ public function tokenize() {
339386
'visibility'=> $this->getVisibility($tokens, $i),
340387
'signature' => $token->getSignature(),
341388
'startLine' => $this->tline($token),
342-
'endLine' => $token->getEndLine(),
389+
'endLine' => $this->getEndLine($tokens, $i),
343390
'ccn' => $token->getCCN(),
344391
'file' => $this->filename
345392
);

0 commit comments

Comments
 (0)