Skip to content

Commit 7bb2c4c

Browse files
committed
Optimize loop
1 parent 36d3a81 commit 7bb2c4c

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

src/CodeCoverage.php

+16-24
Original file line numberDiff line numberDiff line change
@@ -605,13 +605,24 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
605605

606606
$this->data[$file] = ['lines' => []];
607607

608+
foreach ($fileData['lines'] as $lineNumber => $flag) {
609+
if ($flag === PHP_CodeCoverage_Driver::LINE_NOT_EXECUTABLE) {
610+
$this->data[$file]['lines'][$lineNumber] = null;
611+
} else {
612+
$this->data[$file]['lines'][$lineNumber] = [
613+
'pathCovered' => false,
614+
'tests' => [],
615+
];
616+
}
617+
}
618+
608619
if ($this->pathCoverage) {
609620
$this->data[$file]['branches'] = [];
610621
$this->data[$file]['paths'] = [];
611622

612623
foreach ($fileData['functions'] as $functionName => $functionData) {
613624
$this->data[$file]['branches'][$functionName] = [];
614-
$this->data[$file]['paths'][$functionName] = [];
625+
$this->data[$file]['paths'][$functionName] = $functionData['paths'];
615626

616627
foreach ($functionData['branches'] as $index => $branch) {
617628
$this->data[$file]['branches'][$functionName][$index] = [
@@ -620,30 +631,11 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
620631
'line_end' => $branch['line_end'],
621632
'tests' => []
622633
];
623-
}
624-
625-
foreach ($functionData['paths'] as $path) {
626-
$this->data[$file]['paths'][$functionName][] = $path;
627-
}
628-
}
629-
}
630-
631-
foreach ($fileData['lines'] as $lineNumber => $flag) {
632-
if ($flag === PHP_CodeCoverage_Driver::LINE_NOT_EXECUTABLE) {
633-
$this->data[$file]['lines'][$lineNumber] = null;
634-
} else {
635-
$this->data[$file]['lines'][$lineNumber] = [
636-
'pathCovered' => false,
637-
'tests' => [],
638-
];
639-
}
640-
}
641634

642-
foreach ($this->data[$file]['branches'] as $function) {
643-
foreach ($function as $branch) {
644-
for ($i = $branch['line_start']; $i < $branch['line_end']; $i++) {
645-
if (isset($this->data[$file]['lines'][$i])) {
646-
$this->data[$file]['lines'][$i]['pathCovered'] = (bool) $branch['hit'];
635+
for ($i = $branch['line_start']; $i < $branch['line_end']; $i++) {
636+
if (isset($this->data[$file]['lines'][$i])) {
637+
$this->data[$file]['lines'][$i]['pathCovered'] = (bool) $branch['hit'];
638+
}
647639
}
648640
}
649641
}

0 commit comments

Comments
 (0)