@@ -91,19 +91,14 @@ final class File extends AbstractNode
9191 private $ numTestedFunctions ;
9292
9393 /**
94- * @var array
94+ * @var bool
9595 */
96- private $ startLines = [] ;
96+ private $ cacheTokens ;
9797
9898 /**
9999 * @var array
100100 */
101- private $ endLines = [];
102-
103- /**
104- * @var bool
105- */
106- private $ cacheTokens ;
101+ private $ codeUnitsByLine = [];
107102
108103 public function __construct (string $ name , AbstractNode $ parent , array $ coverageData , array $ testData , bool $ cacheTokens )
109104 {
@@ -335,115 +330,43 @@ public function getNumTestedFunctions(): int
335330
336331 private function calculateStatistics (): void
337332 {
338- $ classStack = [];
339- $ methodStack = [];
340- $ functionStack = [];
341-
342333 if ($ this ->cacheTokens ) {
343334 $ tokens = \PHP_Token_Stream_CachingFactory::get ($ this ->getPath ());
344335 } else {
345336 $ tokens = new \PHP_Token_Stream ($ this ->getPath ());
346337 }
347338
339+ $ this ->linesOfCode = $ tokens ->getLinesOfCode ();
340+
341+ foreach (\range (1 , $ this ->linesOfCode ['loc ' ]) as $ lineNumber ) {
342+ $ this ->codeUnitsByLine [$ lineNumber ] = [];
343+ }
344+
348345 $ this ->processClasses ($ tokens );
349346 $ this ->processTraits ($ tokens );
350347 $ this ->processFunctions ($ tokens );
351- $ this ->linesOfCode = $ tokens ->getLinesOfCode ();
352348 unset($ tokens );
353349
354- for ($ lineNumber = 1 ; $ lineNumber <= $ this ->linesOfCode ['loc ' ]; $ lineNumber ++) {
355- if (isset ($ this ->startLines [$ lineNumber ]['className ' ])) {
356- if (isset ($ currentClass )) {
357- $ classStack [] = &$ currentClass ;
358- }
359-
360- $ currentClass = &$ this ->startLines [$ lineNumber ];
361- } elseif (isset ($ this ->startLines [$ lineNumber ]['traitName ' ])) {
362- $ currentTrait = &$ this ->startLines [$ lineNumber ];
363- } elseif (isset ($ this ->startLines [$ lineNumber ]['methodName ' ])) {
364- if (isset ($ currentMethod )) {
365- $ methodStack [] = &$ currentMethod ;
366- }
367-
368- $ currentMethod = &$ this ->startLines [$ lineNumber ];
369- } elseif (isset ($ this ->startLines [$ lineNumber ]['functionName ' ])) {
370- if (isset ($ currentFunction )) {
371- $ functionStack [] = &$ currentFunction ;
372- }
373-
374- $ currentFunction = &$ this ->startLines [$ lineNumber ];
375- }
376-
350+ foreach (\range (1 , $ this ->linesOfCode ['loc ' ]) as $ lineNumber ) {
377351 if (isset ($ this ->coverageData [$ lineNumber ])) {
378- if ( isset ( $ currentClass ) ) {
379- $ currentClass ['executableLines ' ]++;
352+ foreach ( $ this -> codeUnitsByLine [ $ lineNumber ] as & $ codeUnit ) {
353+ $ codeUnit ['executableLines ' ]++;
380354 }
381355
382- if (isset ($ currentTrait )) {
383- $ currentTrait ['executableLines ' ]++;
384- }
385-
386- if (isset ($ currentMethod )) {
387- $ currentMethod ['executableLines ' ]++;
388- }
389-
390- if (isset ($ currentFunction )) {
391- $ currentFunction ['executableLines ' ]++;
392- }
356+ unset($ codeUnit );
393357
394358 $ this ->numExecutableLines ++;
395359
396360 if (\count ($ this ->coverageData [$ lineNumber ]) > 0 ) {
397- if (isset ($ currentClass )) {
398- $ currentClass ['executedLines ' ]++;
399- }
400-
401- if (isset ($ currentTrait )) {
402- $ currentTrait ['executedLines ' ]++;
403- }
404-
405- if (isset ($ currentMethod )) {
406- $ currentMethod ['executedLines ' ]++;
361+ foreach ($ this ->codeUnitsByLine [$ lineNumber ] as &$ codeUnit ) {
362+ $ codeUnit ['executedLines ' ]++;
407363 }
408364
409- if (isset ($ currentFunction )) {
410- $ currentFunction ['executedLines ' ]++;
411- }
365+ unset($ codeUnit );
412366
413367 $ this ->numExecutedLines ++;
414368 }
415369 }
416-
417- if (isset ($ this ->endLines [$ lineNumber ]['className ' ])) {
418- unset($ currentClass );
419-
420- if ($ classStack ) {
421- \end ($ classStack );
422- $ key = \key ($ classStack );
423- $ currentClass = &$ classStack [$ key ];
424- unset($ classStack [$ key ]);
425- }
426- } elseif (isset ($ this ->endLines [$ lineNumber ]['traitName ' ])) {
427- unset($ currentTrait );
428- } elseif (isset ($ this ->endLines [$ lineNumber ]['methodName ' ])) {
429- unset($ currentMethod );
430-
431- if ($ methodStack ) {
432- \end ($ methodStack );
433- $ key = \key ($ methodStack );
434- $ currentMethod = &$ methodStack [$ key ];
435- unset($ methodStack [$ key ]);
436- }
437- } elseif (isset ($ this ->endLines [$ lineNumber ]['functionName ' ])) {
438- unset($ currentFunction );
439-
440- if ($ functionStack ) {
441- \end ($ functionStack );
442- $ key = \key ($ functionStack );
443- $ currentFunction = &$ functionStack [$ key ];
444- unset($ functionStack [$ key ]);
445- }
446- }
447370 }
448371
449372 foreach ($ this ->traits as &$ trait ) {
@@ -463,6 +386,8 @@ private function calculateStatistics(): void
463386 $ trait ['ccn ' ] += $ method ['ccn ' ];
464387 }
465388
389+ unset($ method );
390+
466391 if ($ trait ['executableLines ' ] > 0 ) {
467392 $ trait ['coverage ' ] = ($ trait ['executedLines ' ] /
468393 $ trait ['executableLines ' ]) * 100 ;
@@ -480,6 +405,8 @@ private function calculateStatistics(): void
480405 );
481406 }
482407
408+ unset($ trait );
409+
483410 foreach ($ this ->classes as &$ class ) {
484411 foreach ($ class ['methods ' ] as &$ method ) {
485412 if ($ method ['executableLines ' ] > 0 ) {
@@ -497,6 +424,8 @@ private function calculateStatistics(): void
497424 $ class ['ccn ' ] += $ method ['ccn ' ];
498425 }
499426
427+ unset($ method );
428+
500429 if ($ class ['executableLines ' ] > 0 ) {
501430 $ class ['coverage ' ] = ($ class ['executedLines ' ] /
502431 $ class ['executableLines ' ]) * 100 ;
@@ -514,6 +443,8 @@ private function calculateStatistics(): void
514443 );
515444 }
516445
446+ unset($ class );
447+
517448 foreach ($ this ->functions as &$ function ) {
518449 if ($ function ['executableLines ' ] > 0 ) {
519450 $ function ['coverage ' ] = ($ function ['executedLines ' ] /
@@ -557,14 +488,15 @@ private function processClasses(\PHP_Token_Stream $tokens): void
557488 'link ' => $ link . $ class ['startLine ' ]
558489 ];
559490
560- $ this ->startLines [$ class ['startLine ' ]] = &$ this ->classes [$ className ];
561- $ this ->endLines [$ class ['endLine ' ]] = &$ this ->classes [$ className ];
562-
563491 foreach ($ class ['methods ' ] as $ methodName => $ method ) {
564492 $ this ->classes [$ className ]['methods ' ][$ methodName ] = $ this ->newMethod ($ methodName , $ method , $ link );
565493
566- $ this ->startLines [$ method ['startLine ' ]] = &$ this ->classes [$ className ]['methods ' ][$ methodName ];
567- $ this ->endLines [$ method ['endLine ' ]] = &$ this ->classes [$ className ]['methods ' ][$ methodName ];
494+ foreach (\range ($ method ['startLine ' ], $ method ['endLine ' ]) as $ lineNumber ) {
495+ $ this ->codeUnitsByLine [$ lineNumber ] = [
496+ &$ this ->classes [$ className ],
497+ &$ this ->classes [$ className ]['methods ' ][$ methodName ]
498+ ];
499+ }
568500 }
569501 }
570502 }
@@ -589,14 +521,15 @@ private function processTraits(\PHP_Token_Stream $tokens): void
589521 'link ' => $ link . $ trait ['startLine ' ]
590522 ];
591523
592- $ this ->startLines [$ trait ['startLine ' ]] = &$ this ->traits [$ traitName ];
593- $ this ->endLines [$ trait ['endLine ' ]] = &$ this ->traits [$ traitName ];
594-
595524 foreach ($ trait ['methods ' ] as $ methodName => $ method ) {
596525 $ this ->traits [$ traitName ]['methods ' ][$ methodName ] = $ this ->newMethod ($ methodName , $ method , $ link );
597526
598- $ this ->startLines [$ method ['startLine ' ]] = &$ this ->traits [$ traitName ]['methods ' ][$ methodName ];
599- $ this ->endLines [$ method ['endLine ' ]] = &$ this ->traits [$ traitName ]['methods ' ][$ methodName ];
527+ foreach (\range ($ method ['startLine ' ], $ method ['endLine ' ]) as $ lineNumber ) {
528+ $ this ->codeUnitsByLine [$ lineNumber ] = [
529+ &$ this ->traits [$ traitName ],
530+ &$ this ->traits [$ traitName ]['methods ' ][$ methodName ]
531+ ];
532+ }
600533 }
601534 }
602535 }
@@ -620,8 +553,9 @@ private function processFunctions(\PHP_Token_Stream $tokens): void
620553 'link ' => $ link . $ function ['startLine ' ]
621554 ];
622555
623- $ this ->startLines [$ function ['startLine ' ]] = &$ this ->functions [$ functionName ];
624- $ this ->endLines [$ function ['endLine ' ]] = &$ this ->functions [$ functionName ];
556+ foreach (\range ($ function ['startLine ' ], $ function ['endLine ' ]) as $ lineNumber ) {
557+ $ this ->codeUnitsByLine [$ lineNumber ] = [&$ this ->functions [$ functionName ]];
558+ }
625559 }
626560 }
627561
0 commit comments