Skip to content

Commit 5b45ec8

Browse files
committed
Use plain for-loop over foreach(range() ..)
1 parent 37047b5 commit 5b45ec8

File tree

7 files changed

+19
-24
lines changed

7 files changed

+19
-24
lines changed

src/Node/File.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use function array_filter;
1313
use function count;
14-
use function range;
1514
use SebastianBergmann\CodeCoverage\CodeCoverage;
1615
use SebastianBergmann\CodeCoverage\StaticAnalysis\AnalysisResult;
1716
use SebastianBergmann\CodeCoverage\StaticAnalysis\Class_;
@@ -372,15 +371,15 @@ public function numberOfTestedFunctions(): int
372371
*/
373372
private function calculateStatistics(array $classes, array $traits, array $functions): void
374373
{
375-
foreach (range(1, $this->linesOfCode->linesOfCode()) as $lineNumber) {
374+
for ($lineNumber = 1; $lineNumber <= $this->linesOfCode->linesOfCode(); $lineNumber++) {
376375
$this->codeUnitsByLine[$lineNumber] = [];
377376
}
378377

379378
$this->processClasses($classes);
380379
$this->processTraits($traits);
381380
$this->processFunctions($functions);
382381

383-
foreach (range(1, $this->linesOfCode->linesOfCode()) as $lineNumber) {
382+
for ($lineNumber = 1; $lineNumber <= $this->linesOfCode->linesOfCode(); $lineNumber++) {
384383
if (isset($this->lineCoverageData[$lineNumber])) {
385384
foreach ($this->codeUnitsByLine[$lineNumber] as &$codeUnit) {
386385
$codeUnit['executableLines']++;
@@ -511,7 +510,7 @@ private function processClasses(array $classes): void
511510
$this->numExecutablePaths += $methodData['executablePaths'];
512511
$this->numExecutedPaths += $methodData['executedPaths'];
513512

514-
foreach (range($method->startLine(), $method->endLine()) as $lineNumber) {
513+
for ($lineNumber = $method->startLine(); $lineNumber <= $method->endLine(); $lineNumber++) {
515514
$this->codeUnitsByLine[$lineNumber] = [
516515
&$this->classes[$className],
517516
&$this->classes[$className]['methods'][$methodName],
@@ -560,7 +559,7 @@ private function processTraits(array $traits): void
560559
$this->numExecutablePaths += $methodData['executablePaths'];
561560
$this->numExecutedPaths += $methodData['executedPaths'];
562561

563-
foreach (range($method->startLine(), $method->endLine()) as $lineNumber) {
562+
for ($lineNumber = $method->startLine(); $lineNumber <= $method->endLine(); $lineNumber++) {
564563
$this->codeUnitsByLine[$lineNumber] = [
565564
&$this->traits[$traitName],
566565
&$this->traits[$traitName]['methods'][$methodName],
@@ -596,7 +595,7 @@ private function processFunctions(array $functions): void
596595
'link' => $link . $function->startLine(),
597596
];
598597

599-
foreach (range($function->startLine(), $function->endLine()) as $lineNumber) {
598+
for ($lineNumber = $function->startLine(); $lineNumber <= $function->endLine(); $lineNumber++) {
600599
$this->codeUnitsByLine[$lineNumber] = [&$this->functions[$functionName]];
601600
}
602601

src/Report/Clover.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use function is_string;
1414
use function ksort;
1515
use function max;
16-
use function range;
1716
use function time;
1817
use DOMDocument;
1918
use SebastianBergmann\CodeCoverage\CodeCoverage;
@@ -95,7 +94,7 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
9594

9695
$methodCount = 0;
9796

98-
foreach (range($method['startLine'], $method['endLine']) as $line) {
97+
for ($line = $method['startLine']; $line <= $method['endLine']; $line++) {
9998
if (isset($coverageData[$line])) {
10099
$methodCount = max($methodCount, count($coverageData[$line]));
101100
}

src/Report/Cobertura.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use function basename;
1414
use function count;
1515
use function preg_match;
16-
use function range;
1716
use function str_replace;
1817
use function time;
1918
use DOMImplementation;
@@ -170,7 +169,7 @@ public function process(CodeCoverage $coverage, ?string $target = null): string
170169

171170
$methodElement->appendChild($methodLinesElement);
172171

173-
foreach (range($method['startLine'], $method['endLine']) as $line) {
172+
for ($line = $method['startLine']; $line <= $method['endLine']; $line++) {
174173
if (!isset($coverageData[$line])) {
175174
continue;
176175
}
@@ -251,7 +250,7 @@ public function process(CodeCoverage $coverage, ?string $target = null): string
251250

252251
$methodElement->appendChild($methodLinesElement);
253252

254-
foreach (range($function['startLine'], $function['endLine']) as $line) {
253+
for ($line = $function['startLine']; $line <= $function['endLine']; $line++) {
255254
if (!isset($coverageData[$line])) {
256255
continue;
257256
}

src/Report/Html/Renderer/File.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
609609

610610
foreach ($functionCoverageData as $method) {
611611
foreach ($method['branches'] as $branch) {
612-
foreach (range($branch['line_start'], $branch['line_end']) as $line) {
612+
for ($line = $branch['line_start']; $line <= $branch['line_end']; $line++) {
613613
if (!isset($lineData[$line])) { // blank line at end of file is sometimes included here
614614
continue;
615615
}
@@ -696,7 +696,7 @@ private function renderSourceWithPathCoverage(FileNode $node): string
696696
foreach ($functionCoverageData as $method) {
697697
foreach ($method['paths'] as $pathId => $path) {
698698
foreach ($path['path'] as $branchTaken) {
699-
foreach (range($method['branches'][$branchTaken]['line_start'], $method['branches'][$branchTaken]['line_end']) as $line) {
699+
for ($line = $method['branches'][$branchTaken]['line_start']; $line <= $method['branches'][$branchTaken]['line_end']; $line++) {
700700
if (!isset($lineData[$line])) {
701701
continue;
702702
}

src/Report/OpenClover.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use function is_string;
1616
use function ksort;
1717
use function max;
18-
use function range;
1918
use function str_replace;
2019
use function time;
2120
use DOMDocument;
@@ -99,7 +98,7 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
9998

10099
$methodCount = 0;
101100

102-
foreach (range($method['startLine'], $method['endLine']) as $line) {
101+
for ($line = $method['startLine']; $line <= $method['endLine']; $line++) {
103102
if (isset($coverageData[$line])) {
104103
$methodCount = max($methodCount, count($coverageData[$line]));
105104
}

src/StaticAnalysis/Visitor/ExecutableLinesFindingVisitor.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use function max;
1919
use function preg_match;
2020
use function preg_quote;
21-
use function range;
2221
use function reset;
2322
use function sprintf;
2423
use PhpParser\Node;
@@ -75,7 +74,7 @@ public function enterNode(Node $node): null
7574
$endLine = $node->getEndLine() - 1;
7675

7776
if ($startLine <= $endLine) {
78-
foreach (range($startLine, $endLine) as $line) {
77+
for ($line = $startLine; $line <= $endLine; $line++) {
7978
unset($this->executableLinesGroupedByBranch[$line]);
8079
}
8180
}
@@ -84,7 +83,7 @@ public function enterNode(Node $node): null
8483
}
8584

8685
if ($node instanceof Node\Stmt\Interface_) {
87-
foreach (range($node->getStartLine(), $node->getEndLine()) as $line) {
86+
for ($line = $node->getStartLine(); $line <= $node->getEndLine(); $line++) {
8887
$this->unsets[$line] = true;
8988
}
9089

@@ -144,7 +143,7 @@ public function enterNode(Node $node): null
144143
$unsets = [];
145144

146145
foreach ($node->getParams() as $param) {
147-
foreach (range($param->getStartLine(), $param->getEndLine()) as $line) {
146+
for ($line = $param->getStartLine(); $line <= $param->getEndLine(); $line++) {
148147
$unsets[$line] = true;
149148
}
150149
}
@@ -162,7 +161,7 @@ public function enterNode(Node $node): null
162161
continue;
163162
}
164163

165-
foreach (range($stmt->getStartLine(), $stmt->getEndLine()) as $line) {
164+
for ($line = $stmt->getStartLine(); $line <= $stmt->getEndLine(); $line++) {
166165
unset($this->executableLinesGroupedByBranch[$line]);
167166

168167
if (
@@ -395,7 +394,7 @@ public function executableLinesGroupedByBranch(): array
395394

396395
private function setLineBranch(int $start, int $end, int $branch): void
397396
{
398-
foreach (range($start, $end) as $line) {
397+
for ($line = $start; $line <= $end; $line++) {
399398
$this->executableLinesGroupedByBranch[$line] = $branch;
400399
}
401400
}

src/Target/MapBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function build(Filter $filter, FileAnalyser $analyser): array
111111

112112
$this->process($functions, $function->namespacedName(), $file, $function->startLine(), $function->endLine());
113113

114-
foreach (range($function->startLine(), $function->endLine()) as $line) {
114+
for ($line = $function->startLine(); $line <= $function->endLine(); $line++) {
115115
$reverseLookup[$file . ':' . $line] = $function->namespacedName();
116116
}
117117
}
@@ -202,7 +202,7 @@ private function processMethods(Class_|Trait_ $classOrTrait, string $file, array
202202

203203
$this->process($methods, $methodName, $file, $method->startLine(), $method->endLine());
204204

205-
foreach (range($method->startLine(), $method->endLine()) as $line) {
205+
for ($line = $method->startLine(); $line <= $method->endLine(); $line++) {
206206
$reverseLookup[$file . ':' . $line] = $methodName;
207207
}
208208
}
@@ -221,7 +221,7 @@ private function processNamespace(array &$data, string $namespace, string $file,
221221
{
222222
$parts = explode('\\', $namespace);
223223

224-
foreach (range(1, count($parts)) as $i) {
224+
for ($i = 1; $i <= count($parts); $i++) {
225225
$this->process($data, implode('\\', array_slice($parts, 0, $i)), $file, $startLine, $endLine);
226226
}
227227
}

0 commit comments

Comments
 (0)