Skip to content

Commit 50c9372

Browse files
committed
Fix coverage for nested functions and classes
1 parent 40103f9 commit 50c9372

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/CodeCoverage/Report/Node/File.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ public function getNumTestedFunctions()
351351
*/
352352
protected function calculateStatistics()
353353
{
354+
$classStack = $functionStack = [];
355+
354356
if ($this->cacheTokens) {
355357
$tokens = PHP_Token_Stream_CachingFactory::get($this->getPath());
356358
} else {
@@ -367,6 +369,10 @@ protected function calculateStatistics()
367369
if (isset($this->startLines[$lineNumber])) {
368370
// Start line of a class.
369371
if (isset($this->startLines[$lineNumber]['className'])) {
372+
if (isset($currentClass)) {
373+
$classStack[] = &$currentClass;
374+
}
375+
370376
$currentClass = &$this->startLines[$lineNumber];
371377
} // Start line of a trait.
372378
elseif (isset($this->startLines[$lineNumber]['traitName'])) {
@@ -376,12 +382,15 @@ protected function calculateStatistics()
376382
$currentMethod = &$this->startLines[$lineNumber];
377383
} // Start line of a function.
378384
elseif (isset($this->startLines[$lineNumber]['functionName'])) {
385+
if (isset($currentFunction)) {
386+
$functionStack[] = &$currentFunction;
387+
}
388+
379389
$currentFunction = &$this->startLines[$lineNumber];
380390
}
381391
}
382392

383-
if (isset($this->coverageData[$lineNumber]) &&
384-
$this->coverageData[$lineNumber] !== null) {
393+
if (isset($this->coverageData[$lineNumber])) {
385394
if (isset($currentClass)) {
386395
$currentClass['executableLines']++;
387396
}
@@ -425,6 +434,13 @@ protected function calculateStatistics()
425434
// End line of a class.
426435
if (isset($this->endLines[$lineNumber]['className'])) {
427436
unset($currentClass);
437+
438+
if ($classStack) {
439+
end($classStack);
440+
$key = key($classStack);
441+
$currentClass = &$classStack[$key];
442+
unset($classStack[$key]);
443+
}
428444
} // End line of a trait.
429445
elseif (isset($this->endLines[$lineNumber]['traitName'])) {
430446
unset($currentTrait);
@@ -434,6 +450,13 @@ protected function calculateStatistics()
434450
} // End line of a function.
435451
elseif (isset($this->endLines[$lineNumber]['functionName'])) {
436452
unset($currentFunction);
453+
454+
if ($functionStack) {
455+
end($functionStack);
456+
$key = key($functionStack);
457+
$currentFunction = &$functionStack[$key];
458+
unset($functionsStack[$key]);
459+
}
437460
}
438461
}
439462
}

0 commit comments

Comments
 (0)