Skip to content

Commit f246ad7

Browse files
Jean85PowerKiKi
authored andcommitted
Remove redundant function calls in for loops
1 parent d383bc3 commit f246ad7

File tree

11 files changed

+30
-15
lines changed

11 files changed

+30
-15
lines changed

src/PhpSpreadsheet/Calculation/Financial.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,8 @@ public static function NPV(...$args)
16021602

16031603
// Calculate
16041604
$rate = array_shift($aArgs);
1605-
for ($i = 1; $i <= count($aArgs); ++$i) {
1605+
$countArgs = count($aArgs);
1606+
for ($i = 1; $i <= $countArgs; ++$i) {
16061607
// Is it a numeric value?
16071608
if (is_numeric($aArgs[$i - 1])) {
16081609
$returnValue += $aArgs[$i - 1] / pow(1 + $rate, $i);

src/PhpSpreadsheet/Calculation/Statistical.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2700,7 +2700,8 @@ public static function POISSON($value, $mean, $cumulative)
27002700
if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
27012701
if ($cumulative) {
27022702
$summer = 0;
2703-
for ($i = 0; $i <= floor($value); ++$i) {
2703+
$floor = floor($value);
2704+
for ($i = 0; $i <= $floor; ++$i) {
27042705
$summer += pow($mean, $i) / MathTrig::FACT($i);
27052706
}
27062707

src/PhpSpreadsheet/Chart/Renderer/JpGraph.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,8 @@ private function renderPlotStock($groupID)
540540

541541
$dataValuesPlot = [];
542542
// Flatten the plot arrays to a single dimensional array to work with jpgraph
543-
for ($j = 0; $j < count($dataValues[0]); ++$j) {
543+
$jMax = count($dataValues[0]);
544+
for ($j = 0; $j < $jMax; ++$j) {
544545
for ($i = 0; $i < $seriesCount; ++$i) {
545546
$dataValuesPlot[] = $dataValues[$i][$j];
546547
}

src/PhpSpreadsheet/Reader/Slk.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
250250
break;
251251
case 'S':
252252
$styleSettings = substr($rowDatum, 1);
253-
for ($i = 0; $i < strlen($styleSettings); ++$i) {
253+
$iMax = strlen($styleSettings);
254+
for ($i = 0; $i < $iMax; ++$i) {
254255
switch ($styleSettings[$i]) {
255256
case 'I':
256257
$formatArray['font']['italic'] = true;
@@ -386,7 +387,8 @@ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
386387
break;
387388
case 'S':
388389
$styleSettings = substr($rowDatum, 1);
389-
for ($i = 0; $i < strlen($styleSettings); ++$i) {
390+
$iMax = strlen($styleSettings);
391+
for ($i = 0; $i < $iMax; ++$i) {
390392
switch ($styleSettings[$i]) {
391393
case 'I':
392394
$styleData['font']['italic'] = true;

src/PhpSpreadsheet/Reader/Xls.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,7 +1889,8 @@ private function verifyPassword($password, $docid, $salt_data, $hashedsalt_data,
18891889
{
18901890
$pwarray = str_repeat("\0", 64);
18911891

1892-
for ($i = 0; $i < strlen($password); ++$i) {
1892+
$iMax = strlen($password);
1893+
for ($i = 0; $i < $iMax; ++$i) {
18931894
$o = ord(substr($password, $i, 1));
18941895
$pwarray[2 * $i] = chr($o & 0xff);
18951896
$pwarray[2 * $i + 1] = chr(($o >> 8) & 0xff);
@@ -3087,7 +3088,8 @@ private function readSst()
30873088
// 1st fragment compressed
30883089
// this fragment uncompressed
30893090
$newstr = '';
3090-
for ($j = 0; $j < strlen($retstr); ++$j) {
3091+
$jMax = strlen($retstr);
3092+
for ($j = 0; $j < $jMax; ++$j) {
30913093
$newstr .= $retstr[$j] . chr(0);
30923094
}
30933095
$retstr = $newstr;

src/PhpSpreadsheet/Shared/File.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ public static function realpath($pFilename)
8888
if ($returnValue == '' || ($returnValue === null)) {
8989
$pathArray = explode('/', $pFilename);
9090
while (in_array('..', $pathArray) && $pathArray[0] != '..') {
91-
for ($i = 0; $i < count($pathArray); ++$i) {
91+
$iMax = count($pathArray);
92+
for ($i = 0; $i < $iMax; ++$i) {
9293
if ($pathArray[$i] == '..' && $i > 0) {
9394
unset($pathArray[$i], $pathArray[$i - 1]);
9495

src/PhpSpreadsheet/Shared/JAMA/EigenvalueDecomposition.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,8 @@ private function hqr2()
607607
$this->H[$k + 1][$j] = $this->H[$k + 1][$j] - $p * $y;
608608
}
609609
// Column modification
610-
for ($i = 0; $i <= min($n, $k + 3); ++$i) {
610+
$iMax = min($n, $k + 3);
611+
for ($i = 0; $i <= $iMax; ++$i) {
611612
$p = $x * $this->H[$i][$k] + $y * $this->H[$i][$k + 1];
612613
if ($notlast) {
613614
$p = $p + $z * $this->H[$i][$k + 2];
@@ -762,7 +763,8 @@ private function hqr2()
762763
for ($j = $nn - 1; $j >= $low; --$j) {
763764
for ($i = $low; $i <= $high; ++$i) {
764765
$z = 0.0;
765-
for ($k = $low; $k <= min($j, $high); ++$k) {
766+
$kMax = min($j, $high);
767+
for ($k = $low; $k <= $kMax; ++$k) {
766768
$z = $z + $this->V[$i][$k] * $this->H[$k][$j];
767769
}
768770
$this->V[$i][$j] = $z;

src/PhpSpreadsheet/Shared/JAMA/SingularValueDecomposition.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public function __construct($Arg)
8181

8282
// Reduce A to bidiagonal form, storing the diagonal elements
8383
// in s and the super-diagonal elements in e.
84-
for ($k = 0; $k < max($nct, $nrt); ++$k) {
84+
$kMax = max($nct, $nrt);
85+
for ($k = 0; $k < $kMax; ++$k) {
8586
if ($k < $nct) {
8687
// Compute the transformation for the k-th column and
8788
// place the k-th diagonal in s[$k].
@@ -518,7 +519,8 @@ public function rank()
518519
$eps = pow(2.0, -52.0);
519520
$tol = max($this->m, $this->n) * $this->s[0] * $eps;
520521
$r = 0;
521-
for ($i = 0; $i < count($this->s); ++$i) {
522+
$iMax = count($this->s);
523+
for ($i = 0; $i < $iMax; ++$i) {
522524
if ($this->s[$i] > $tol) {
523525
++$r;
524526
}

src/PhpSpreadsheet/Shared/OLE.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,8 @@ public function getDataLength($index)
479479
public static function ascToUcs($ascii)
480480
{
481481
$rawname = '';
482-
for ($i = 0; $i < strlen($ascii); ++$i) {
482+
$iMax = strlen($ascii);
483+
for ($i = 0; $i < $iMax; ++$i) {
483484
$rawname .= $ascii[$i]
484485
. "\x00";
485486
}

src/PhpSpreadsheet/Writer/Xls/Workbook.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,8 @@ private function writeAllDefinedNamesBiff8()
543543
foreach ($namedRanges as $namedRange) {
544544
// Create absolute coordinate
545545
$range = Coordinate::splitRange($namedRange->getRange());
546-
for ($i = 0; $i < count($range); ++$i) {
546+
$iMax = count($range);
547+
for ($i = 0; $i < $iMax; ++$i) {
547548
$range[$i][0] = '\'' . str_replace("'", "''", $namedRange->getWorksheet()->getTitle()) . '\'!' . Coordinate::absoluteCoordinate($range[$i][0]);
548549
if (isset($range[$i][1])) {
549550
$range[$i][1] = Coordinate::absoluteCoordinate($range[$i][1]);

0 commit comments

Comments
 (0)