From f246ad731dabff67112a5d14c8a9524581f5bac6 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Fri, 22 Dec 2017 10:02:44 +0100 Subject: [PATCH] Remove redundant function calls in for loops --- src/PhpSpreadsheet/Calculation/Financial.php | 3 ++- src/PhpSpreadsheet/Calculation/Statistical.php | 3 ++- src/PhpSpreadsheet/Chart/Renderer/JpGraph.php | 3 ++- src/PhpSpreadsheet/Reader/Slk.php | 6 ++++-- src/PhpSpreadsheet/Reader/Xls.php | 6 ++++-- src/PhpSpreadsheet/Shared/File.php | 3 ++- src/PhpSpreadsheet/Shared/JAMA/EigenvalueDecomposition.php | 6 ++++-- .../Shared/JAMA/SingularValueDecomposition.php | 6 ++++-- src/PhpSpreadsheet/Shared/OLE.php | 3 ++- src/PhpSpreadsheet/Writer/Xls/Workbook.php | 3 ++- src/PhpSpreadsheet/Writer/Xlsx/Workbook.php | 3 ++- 11 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/Financial.php b/src/PhpSpreadsheet/Calculation/Financial.php index 919eb9e1..59bd334f 100644 --- a/src/PhpSpreadsheet/Calculation/Financial.php +++ b/src/PhpSpreadsheet/Calculation/Financial.php @@ -1602,7 +1602,8 @@ class Financial // Calculate $rate = array_shift($aArgs); - for ($i = 1; $i <= count($aArgs); ++$i) { + $countArgs = count($aArgs); + for ($i = 1; $i <= $countArgs; ++$i) { // Is it a numeric value? if (is_numeric($aArgs[$i - 1])) { $returnValue += $aArgs[$i - 1] / pow(1 + $rate, $i); diff --git a/src/PhpSpreadsheet/Calculation/Statistical.php b/src/PhpSpreadsheet/Calculation/Statistical.php index eafccc9d..a851eeee 100644 --- a/src/PhpSpreadsheet/Calculation/Statistical.php +++ b/src/PhpSpreadsheet/Calculation/Statistical.php @@ -2700,7 +2700,8 @@ class Statistical if ((is_numeric($cumulative)) || (is_bool($cumulative))) { if ($cumulative) { $summer = 0; - for ($i = 0; $i <= floor($value); ++$i) { + $floor = floor($value); + for ($i = 0; $i <= $floor; ++$i) { $summer += pow($mean, $i) / MathTrig::FACT($i); } diff --git a/src/PhpSpreadsheet/Chart/Renderer/JpGraph.php b/src/PhpSpreadsheet/Chart/Renderer/JpGraph.php index 95308f1b..d33fb8a8 100644 --- a/src/PhpSpreadsheet/Chart/Renderer/JpGraph.php +++ b/src/PhpSpreadsheet/Chart/Renderer/JpGraph.php @@ -540,7 +540,8 @@ class JpGraph implements IRenderer $dataValuesPlot = []; // Flatten the plot arrays to a single dimensional array to work with jpgraph - for ($j = 0; $j < count($dataValues[0]); ++$j) { + $jMax = count($dataValues[0]); + for ($j = 0; $j < $jMax; ++$j) { for ($i = 0; $i < $seriesCount; ++$i) { $dataValuesPlot[] = $dataValues[$i][$j]; } diff --git a/src/PhpSpreadsheet/Reader/Slk.php b/src/PhpSpreadsheet/Reader/Slk.php index 7892bb0e..979353da 100644 --- a/src/PhpSpreadsheet/Reader/Slk.php +++ b/src/PhpSpreadsheet/Reader/Slk.php @@ -250,7 +250,8 @@ class Slk extends BaseReader break; case 'S': $styleSettings = substr($rowDatum, 1); - for ($i = 0; $i < strlen($styleSettings); ++$i) { + $iMax = strlen($styleSettings); + for ($i = 0; $i < $iMax; ++$i) { switch ($styleSettings[$i]) { case 'I': $formatArray['font']['italic'] = true; @@ -386,7 +387,8 @@ class Slk extends BaseReader break; case 'S': $styleSettings = substr($rowDatum, 1); - for ($i = 0; $i < strlen($styleSettings); ++$i) { + $iMax = strlen($styleSettings); + for ($i = 0; $i < $iMax; ++$i) { switch ($styleSettings[$i]) { case 'I': $styleData['font']['italic'] = true; diff --git a/src/PhpSpreadsheet/Reader/Xls.php b/src/PhpSpreadsheet/Reader/Xls.php index 02d99f01..6c727ad9 100644 --- a/src/PhpSpreadsheet/Reader/Xls.php +++ b/src/PhpSpreadsheet/Reader/Xls.php @@ -1889,7 +1889,8 @@ class Xls extends BaseReader { $pwarray = str_repeat("\0", 64); - for ($i = 0; $i < strlen($password); ++$i) { + $iMax = strlen($password); + for ($i = 0; $i < $iMax; ++$i) { $o = ord(substr($password, $i, 1)); $pwarray[2 * $i] = chr($o & 0xff); $pwarray[2 * $i + 1] = chr(($o >> 8) & 0xff); @@ -3087,7 +3088,8 @@ class Xls extends BaseReader // 1st fragment compressed // this fragment uncompressed $newstr = ''; - for ($j = 0; $j < strlen($retstr); ++$j) { + $jMax = strlen($retstr); + for ($j = 0; $j < $jMax; ++$j) { $newstr .= $retstr[$j] . chr(0); } $retstr = $newstr; diff --git a/src/PhpSpreadsheet/Shared/File.php b/src/PhpSpreadsheet/Shared/File.php index 76157f75..d24b63d7 100644 --- a/src/PhpSpreadsheet/Shared/File.php +++ b/src/PhpSpreadsheet/Shared/File.php @@ -88,7 +88,8 @@ class File if ($returnValue == '' || ($returnValue === null)) { $pathArray = explode('/', $pFilename); while (in_array('..', $pathArray) && $pathArray[0] != '..') { - for ($i = 0; $i < count($pathArray); ++$i) { + $iMax = count($pathArray); + for ($i = 0; $i < $iMax; ++$i) { if ($pathArray[$i] == '..' && $i > 0) { unset($pathArray[$i], $pathArray[$i - 1]); diff --git a/src/PhpSpreadsheet/Shared/JAMA/EigenvalueDecomposition.php b/src/PhpSpreadsheet/Shared/JAMA/EigenvalueDecomposition.php index e72604f4..604c8e3e 100644 --- a/src/PhpSpreadsheet/Shared/JAMA/EigenvalueDecomposition.php +++ b/src/PhpSpreadsheet/Shared/JAMA/EigenvalueDecomposition.php @@ -607,7 +607,8 @@ class EigenvalueDecomposition $this->H[$k + 1][$j] = $this->H[$k + 1][$j] - $p * $y; } // Column modification - for ($i = 0; $i <= min($n, $k + 3); ++$i) { + $iMax = min($n, $k + 3); + for ($i = 0; $i <= $iMax; ++$i) { $p = $x * $this->H[$i][$k] + $y * $this->H[$i][$k + 1]; if ($notlast) { $p = $p + $z * $this->H[$i][$k + 2]; @@ -762,7 +763,8 @@ class EigenvalueDecomposition for ($j = $nn - 1; $j >= $low; --$j) { for ($i = $low; $i <= $high; ++$i) { $z = 0.0; - for ($k = $low; $k <= min($j, $high); ++$k) { + $kMax = min($j, $high); + for ($k = $low; $k <= $kMax; ++$k) { $z = $z + $this->V[$i][$k] * $this->H[$k][$j]; } $this->V[$i][$j] = $z; diff --git a/src/PhpSpreadsheet/Shared/JAMA/SingularValueDecomposition.php b/src/PhpSpreadsheet/Shared/JAMA/SingularValueDecomposition.php index a2fd69d5..238c24fc 100644 --- a/src/PhpSpreadsheet/Shared/JAMA/SingularValueDecomposition.php +++ b/src/PhpSpreadsheet/Shared/JAMA/SingularValueDecomposition.php @@ -81,7 +81,8 @@ class SingularValueDecomposition // Reduce A to bidiagonal form, storing the diagonal elements // in s and the super-diagonal elements in e. - for ($k = 0; $k < max($nct, $nrt); ++$k) { + $kMax = max($nct, $nrt); + for ($k = 0; $k < $kMax; ++$k) { if ($k < $nct) { // Compute the transformation for the k-th column and // place the k-th diagonal in s[$k]. @@ -518,7 +519,8 @@ class SingularValueDecomposition $eps = pow(2.0, -52.0); $tol = max($this->m, $this->n) * $this->s[0] * $eps; $r = 0; - for ($i = 0; $i < count($this->s); ++$i) { + $iMax = count($this->s); + for ($i = 0; $i < $iMax; ++$i) { if ($this->s[$i] > $tol) { ++$r; } diff --git a/src/PhpSpreadsheet/Shared/OLE.php b/src/PhpSpreadsheet/Shared/OLE.php index 4912b388..c3f6b128 100644 --- a/src/PhpSpreadsheet/Shared/OLE.php +++ b/src/PhpSpreadsheet/Shared/OLE.php @@ -479,7 +479,8 @@ class OLE public static function ascToUcs($ascii) { $rawname = ''; - for ($i = 0; $i < strlen($ascii); ++$i) { + $iMax = strlen($ascii); + for ($i = 0; $i < $iMax; ++$i) { $rawname .= $ascii[$i] . "\x00"; } diff --git a/src/PhpSpreadsheet/Writer/Xls/Workbook.php b/src/PhpSpreadsheet/Writer/Xls/Workbook.php index c083a626..564f9512 100644 --- a/src/PhpSpreadsheet/Writer/Xls/Workbook.php +++ b/src/PhpSpreadsheet/Writer/Xls/Workbook.php @@ -543,7 +543,8 @@ class Workbook extends BIFFwriter foreach ($namedRanges as $namedRange) { // Create absolute coordinate $range = Coordinate::splitRange($namedRange->getRange()); - for ($i = 0; $i < count($range); ++$i) { + $iMax = count($range); + for ($i = 0; $i < $iMax; ++$i) { $range[$i][0] = '\'' . str_replace("'", "''", $namedRange->getWorksheet()->getTitle()) . '\'!' . Coordinate::absoluteCoordinate($range[$i][0]); if (isset($range[$i][1])) { $range[$i][1] = Coordinate::absoluteCoordinate($range[$i][1]); diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php b/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php index b208466a..e4752884 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php @@ -315,7 +315,8 @@ class Workbook extends WriterPart // Create absolute coordinate and write as raw text $range = Coordinate::splitRange($pNamedRange->getRange()); - for ($i = 0; $i < count($range); ++$i) { + $iMax = count($range); + for ($i = 0; $i < $iMax; ++$i) { $range[$i][0] = '\'' . str_replace("'", "''", $pNamedRange->getWorksheet()->getTitle()) . '\'!' . Coordinate::absoluteReference($range[$i][0]); if (isset($range[$i][1])) { $range[$i][1] = Coordinate::absoluteReference($range[$i][1]);