Skip to content

Commit e9956f3

Browse files
committed
make tline() aware of scalar tokens
1 parent b11d7bf commit e9956f3

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/CodeCoverage/Util/Tokenizer.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class PHP_CodeCoverage_Util_Tokenizer {
55
private $functions = [];
66
private $classes = [];
77
private $traits = [];
8+
private $tlines = [];
89
private $linesOfCode = array('loc' => 0, 'cloc' => 0, 'ncloc' => 0);
910

1011
/**
@@ -64,11 +65,8 @@ private function tclass($token) {
6465
}
6566
}
6667

67-
private function tline($token) {
68-
if (isset($token[3])) {
69-
return $token[3];
70-
}
71-
throw new Exception('Scalar tokens are not yet precalculated');
68+
private function tline($idx) {
69+
return $this->tlines[$idx];
7270
}
7371

7472
private function tname(array $tokens, $idx) {
@@ -241,7 +239,7 @@ private function getVisibility(array $tokens, $idx)
241239
* @return string|null Returns the docblock as a string if found
242240
*/
243241
private function getDocblock(array $tokens, $idx) {
244-
$currentLineNumber = $this->tline($tokens[$idx]);
242+
$currentLineNumber = $this->tline($idx);
245243
$prevLineNumber = $currentLineNumber - 1;
246244

247245
for ($i = $idx - 1; $i; $i--) {
@@ -258,7 +256,7 @@ private function getDocblock(array $tokens, $idx) {
258256
break;
259257
}
260258

261-
$line = $this->tline($token);
259+
$line = $this->tline($i);
262260

263261
if ($line == $currentLineNumber || ($line == $prevLineNumber && $tconst === T_WHITESPACE)) {
264262
continue;
@@ -314,7 +312,7 @@ private function getEndTokenId(array $tokens, $idx)
314312
*/
315313
private function getEndLine(array $tokens, $idx)
316314
{
317-
return $this->tline($tokens[$this->getEndTokenId($tokens, $idx)]);
315+
return $this->tline($this->getEndTokenId($tokens, $idx));
318316
}
319317

320318
/**
@@ -446,17 +444,17 @@ public function tokenize() {
446444
// precalculate in which line the tokens reside, for later lookaheads
447445
$line = 1;
448446
for ($i = 0; $i < $numTokens; ++$i) {
449-
$token =& $tokens[$i];
447+
$token = $tokens[$i];
450448

451449
if (is_array($token)) {
452450
$name = substr(token_name($token[0]), 2);
453451
$text = $token[1];
454-
455-
$token[2] = $line;
456452
} else {
457453
$text = $token;
458454
}
459455

456+
$this->tlines[$i] = $line;
457+
460458
$lines = substr_count($text, "\n");
461459
$line += $lines;
462460
}
@@ -489,7 +487,7 @@ public function tokenize() {
489487
'interfaces'=> $this->getInterfaces($tokens, $i),
490488
'keywords' => $this->getKeywords($tokens, $i),
491489
'docblock' => $this->getDocblock($tokens, $i),
492-
'startLine' => $this->tline($token),
490+
'startLine' => $this->tline($i),
493491
'endLine' => $endLine,
494492
'package' => $this->getPackage($tokens, $i),
495493
'file' => $this->filename
@@ -513,7 +511,7 @@ public function tokenize() {
513511
'keywords' => $this->getKeywords($tokens, $i),
514512
'visibility'=> $this->getVisibility($tokens, $i),
515513
'signature' => $this->getSignature($tokens, $i),
516-
'startLine' => $this->tline($token),
514+
'startLine' => $this->tline($i),
517515
'endLine' => $this->getEndLine($tokens, $i),
518516
'ccn' => $this->getCCN($tokens, $i),
519517
'file' => $this->filename
@@ -531,13 +529,13 @@ public function tokenize() {
531529
break;
532530

533531
case 'PHP_Token_CLOSE_CURLY':
534-
if ($classEndLine !== false && $classEndLine == $this->tline($token)) {
532+
if ($classEndLine !== false && $classEndLine == $this->tline($i)) {
535533
$class = false;
536534
$classEndLine = false;
537-
} elseif ($traitEndLine !== false && $traitEndLine == $this->tline($token)) {
535+
} elseif ($traitEndLine !== false && $traitEndLine == $this->tline($i)) {
538536
$trait = false;
539537
$traitEndLine = false;
540-
} elseif ($interfaceEndLine !== false && $interfaceEndLine == $this->tline($token)) {
538+
} elseif ($interfaceEndLine !== false && $interfaceEndLine == $this->tline($i)) {
541539
$interface = false;
542540
$interfaceEndLine = false;
543541
}

0 commit comments

Comments
 (0)