@@ -5,6 +5,7 @@ class PHP_CodeCoverage_Util_Tokenizer {
5
5
private $ functions = [];
6
6
private $ classes = [];
7
7
private $ traits = [];
8
+ private $ tlines = [];
8
9
private $ linesOfCode = array ('loc ' => 0 , 'cloc ' => 0 , 'ncloc ' => 0 );
9
10
10
11
/**
@@ -64,11 +65,8 @@ private function tclass($token) {
64
65
}
65
66
}
66
67
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 ];
72
70
}
73
71
74
72
private function tname (array $ tokens , $ idx ) {
@@ -241,7 +239,7 @@ private function getVisibility(array $tokens, $idx)
241
239
* @return string|null Returns the docblock as a string if found
242
240
*/
243
241
private function getDocblock (array $ tokens , $ idx ) {
244
- $ currentLineNumber = $ this ->tline ($ tokens [ $ idx] );
242
+ $ currentLineNumber = $ this ->tline ($ idx );
245
243
$ prevLineNumber = $ currentLineNumber - 1 ;
246
244
247
245
for ($ i = $ idx - 1 ; $ i ; $ i --) {
@@ -258,7 +256,7 @@ private function getDocblock(array $tokens, $idx) {
258
256
break ;
259
257
}
260
258
261
- $ line = $ this ->tline ($ token );
259
+ $ line = $ this ->tline ($ i );
262
260
263
261
if ($ line == $ currentLineNumber || ($ line == $ prevLineNumber && $ tconst === T_WHITESPACE )) {
264
262
continue ;
@@ -314,7 +312,7 @@ private function getEndTokenId(array $tokens, $idx)
314
312
*/
315
313
private function getEndLine (array $ tokens , $ idx )
316
314
{
317
- return $ this ->tline ($ tokens [ $ this ->getEndTokenId ($ tokens , $ idx )] );
315
+ return $ this ->tline ($ this ->getEndTokenId ($ tokens , $ idx ));
318
316
}
319
317
320
318
/**
@@ -446,17 +444,17 @@ public function tokenize() {
446
444
// precalculate in which line the tokens reside, for later lookaheads
447
445
$ line = 1 ;
448
446
for ($ i = 0 ; $ i < $ numTokens ; ++$ i ) {
449
- $ token =& $ tokens [$ i ];
447
+ $ token = $ tokens [$ i ];
450
448
451
449
if (is_array ($ token )) {
452
450
$ name = substr (token_name ($ token [0 ]), 2 );
453
451
$ text = $ token [1 ];
454
-
455
- $ token [2 ] = $ line ;
456
452
} else {
457
453
$ text = $ token ;
458
454
}
459
455
456
+ $ this ->tlines [$ i ] = $ line ;
457
+
460
458
$ lines = substr_count ($ text , "\n" );
461
459
$ line += $ lines ;
462
460
}
@@ -489,7 +487,7 @@ public function tokenize() {
489
487
'interfaces ' => $ this ->getInterfaces ($ tokens , $ i ),
490
488
'keywords ' => $ this ->getKeywords ($ tokens , $ i ),
491
489
'docblock ' => $ this ->getDocblock ($ tokens , $ i ),
492
- 'startLine ' => $ this ->tline ($ token ),
490
+ 'startLine ' => $ this ->tline ($ i ),
493
491
'endLine ' => $ endLine ,
494
492
'package ' => $ this ->getPackage ($ tokens , $ i ),
495
493
'file ' => $ this ->filename
@@ -513,7 +511,7 @@ public function tokenize() {
513
511
'keywords ' => $ this ->getKeywords ($ tokens , $ i ),
514
512
'visibility ' => $ this ->getVisibility ($ tokens , $ i ),
515
513
'signature ' => $ this ->getSignature ($ tokens , $ i ),
516
- 'startLine ' => $ this ->tline ($ token ),
514
+ 'startLine ' => $ this ->tline ($ i ),
517
515
'endLine ' => $ this ->getEndLine ($ tokens , $ i ),
518
516
'ccn ' => $ this ->getCCN ($ tokens , $ i ),
519
517
'file ' => $ this ->filename
@@ -531,13 +529,13 @@ public function tokenize() {
531
529
break ;
532
530
533
531
case 'PHP_Token_CLOSE_CURLY ' :
534
- if ($ classEndLine !== false && $ classEndLine == $ this ->tline ($ token )) {
532
+ if ($ classEndLine !== false && $ classEndLine == $ this ->tline ($ i )) {
535
533
$ class = false ;
536
534
$ classEndLine = false ;
537
- } elseif ($ traitEndLine !== false && $ traitEndLine == $ this ->tline ($ token )) {
535
+ } elseif ($ traitEndLine !== false && $ traitEndLine == $ this ->tline ($ i )) {
538
536
$ trait = false ;
539
537
$ traitEndLine = false ;
540
- } elseif ($ interfaceEndLine !== false && $ interfaceEndLine == $ this ->tline ($ token )) {
538
+ } elseif ($ interfaceEndLine !== false && $ interfaceEndLine == $ this ->tline ($ i )) {
541
539
$ interface = false ;
542
540
$ interfaceEndLine = false ;
543
541
}
0 commit comments