Skip to content

Commit 38c014b

Browse files
committed
Loop refactor. Early continue loop
1 parent 2b0babf commit 38c014b

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

src/CodeCoverage.php

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -596,49 +596,51 @@ private function applyIgnoredLinesFilter(array &$data)
596596
private function initializeFilesThatAreSeenTheFirstTime(array $data)
597597
{
598598
foreach ($data as $file => $fileData) {
599-
if ($this->filter->isFile($file) && !isset($this->data[$file])) {
600-
$this->data[$file] = ['lines' => []];
601-
602-
if ($this->pathCoverage) {
603-
$this->data[$file]['branches'] = [];
604-
$this->data[$file]['paths'] = [];
605-
606-
foreach ($fileData['functions'] as $functionName => $functionData) {
607-
$this->data[$file]['branches'][$functionName] = [];
608-
$this->data[$file]['paths'][$functionName] = [];
609-
610-
foreach ($functionData['branches'] as $index => $branch) {
611-
$this->data[$file]['branches'][$functionName][$index] = [
612-
'hit' => $branch['hit'],
613-
'line_start' => $branch['line_start'],
614-
'line_end' => $branch['line_end'],
615-
'tests' => []
616-
];
617-
}
599+
if (!$this->filter->isFile($file) || isset($this->data[$file])) {
600+
continue;
601+
}
618602

619-
foreach ($functionData['paths'] as $path) {
620-
$this->data[$file]['paths'][$functionName][] = $path;
621-
}
622-
}
623-
}
603+
$this->data[$file] = ['lines' => []];
624604

625-
foreach ($fileData['lines'] as $lineNumber => $flag) {
626-
if ($flag === PHP_CodeCoverage_Driver::LINE_NOT_EXECUTABLE) {
627-
$this->data[$file]['lines'][$lineNumber] = null;
628-
} else {
629-
$this->data[$file]['lines'][$lineNumber] = [
630-
'pathCovered' => false,
631-
'tests' => [],
605+
if ($this->pathCoverage) {
606+
$this->data[$file]['branches'] = [];
607+
$this->data[$file]['paths'] = [];
608+
609+
foreach ($fileData['functions'] as $functionName => $functionData) {
610+
$this->data[$file]['branches'][$functionName] = [];
611+
$this->data[$file]['paths'][$functionName] = [];
612+
613+
foreach ($functionData['branches'] as $index => $branch) {
614+
$this->data[$file]['branches'][$functionName][$index] = [
615+
'hit' => $branch['hit'],
616+
'line_start' => $branch['line_start'],
617+
'line_end' => $branch['line_end'],
618+
'tests' => []
632619
];
633620
}
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+
];
634636
}
637+
}
635638

636-
foreach ($this->data[$file]['branches'] as $function) {
637-
foreach ($function as $branch) {
638-
for ($i = $branch['line_start']; $i < $branch['line_end']; $i++) {
639-
if (isset($this->data[$file]['lines'][$i])) {
640-
$this->data[$file]['lines'][$i]['pathCovered'] = (bool) $branch['hit'];
641-
}
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'];
642644
}
643645
}
644646
}

0 commit comments

Comments
 (0)