Skip to content

Commit 905fe15

Browse files
committed
Optimize loop
1 parent 38c014b commit 905fe15

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
@@ -602,13 +602,24 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
602602

603603
$this->data[$file] = ['lines' => []];
604604

605+
foreach ($fileData['lines'] as $lineNumber => $flag) {
606+
if ($flag === PHP_CodeCoverage_Driver::LINE_NOT_EXECUTABLE) {
607+
$this->data[$file]['lines'][$lineNumber] = null;
608+
} else {
609+
$this->data[$file]['lines'][$lineNumber] = [
610+
'pathCovered' => false,
611+
'tests' => [],
612+
];
613+
}
614+
}
615+
605616
if ($this->pathCoverage) {
606617
$this->data[$file]['branches'] = [];
607618
$this->data[$file]['paths'] = [];
608619

609620
foreach ($fileData['functions'] as $functionName => $functionData) {
610621
$this->data[$file]['branches'][$functionName] = [];
611-
$this->data[$file]['paths'][$functionName] = [];
622+
$this->data[$file]['paths'][$functionName] = $functionData['paths'];
612623

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

639-
foreach ($this->data[$file]['branches'] as $function) {
640-
foreach ($function as $branch) {
641-
for ($i = $branch['line_start']; $i < $branch['line_end']; $i++) {
642-
if (isset($this->data[$file]['lines'][$i])) {
643-
$this->data[$file]['lines'][$i]['pathCovered'] = (bool) $branch['hit'];
632+
for ($i = $branch['line_start']; $i < $branch['line_end']; $i++) {
633+
if (isset($this->data[$file]['lines'][$i])) {
634+
$this->data[$file]['lines'][$i]['pathCovered'] = (bool) $branch['hit'];
635+
}
644636
}
645637
}
646638
}

0 commit comments

Comments
 (0)