From 71f3631cfb84bfee37943c799c6a8a9cad23b889 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Fri, 14 Jun 2019 08:31:27 +0200 Subject: [PATCH] Rectorification (#1010) * A few loose to strict comparisons * More loose to strict comparisons * Revert accidental composer hacks that shouldn't have been committed --- composer.json | 2 +- composer.lock | 2 +- src/PhpSpreadsheet/Writer/Xls/Parser.php | 33 ++++++++++---------- src/PhpSpreadsheet/Writer/Xls/Workbook.php | 10 +++--- src/PhpSpreadsheet/Writer/Xlsx/Workbook.php | 4 +-- src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php | 18 +++++------ 6 files changed, 33 insertions(+), 36 deletions(-) diff --git a/composer.json b/composer.json index f6f20696..7ea10715 100644 --- a/composer.json +++ b/composer.json @@ -84,4 +84,4 @@ "PhpOffice\\PhpSpreadsheetTests\\": "tests/PhpSpreadsheetTests" } } -} +} \ No newline at end of file diff --git a/composer.lock b/composer.lock index 19940ce8..a23c7f8d 100644 --- a/composer.lock +++ b/composer.lock @@ -3208,4 +3208,4 @@ "ext-zlib": "*" }, "platform-dev": [] -} +} \ No newline at end of file diff --git a/src/PhpSpreadsheet/Writer/Xls/Parser.php b/src/PhpSpreadsheet/Writer/Xls/Parser.php index e87d09a2..6049e328 100644 --- a/src/PhpSpreadsheet/Writer/Xls/Parser.php +++ b/src/PhpSpreadsheet/Writer/Xls/Parser.php @@ -1009,7 +1009,7 @@ class Parser break; case '>': - if ($this->lookAhead == '=') { // it's a GE token + if ($this->lookAhead === '=') { // it's a GE token break; } @@ -1018,7 +1018,7 @@ class Parser break; case '<': // it's a LE or a NE token - if (($this->lookAhead == '=') or ($this->lookAhead == '>')) { + if (($this->lookAhead === '=') or ($this->lookAhead === '>')) { break; } @@ -1027,12 +1027,12 @@ class Parser break; default: // if it's a reference A1 or $A$1 or $A1 or A$1 - if (preg_match('/^\$?[A-Ia-i]?[A-Za-z]\$?\d+$/', $token) and !preg_match('/\d/', $this->lookAhead) and ($this->lookAhead != ':') and ($this->lookAhead != '.') and ($this->lookAhead != '!')) { + if (preg_match('/^\$?[A-Ia-i]?[A-Za-z]\$?\d+$/', $token) and !preg_match('/\d/', $this->lookAhead) and ($this->lookAhead !== ':') and ($this->lookAhead !== '.') and ($this->lookAhead !== '!')) { return $token; - } elseif (preg_match('/^' . self::REGEX_SHEET_TITLE_UNQUOTED . '(\\:' . self::REGEX_SHEET_TITLE_UNQUOTED . ')?\\!\$?[A-Ia-i]?[A-Za-z]\$?\\d+$/u', $token) and !preg_match('/\d/', $this->lookAhead) and ($this->lookAhead != ':') and ($this->lookAhead != '.')) { + } elseif (preg_match('/^' . self::REGEX_SHEET_TITLE_UNQUOTED . '(\\:' . self::REGEX_SHEET_TITLE_UNQUOTED . ')?\\!\$?[A-Ia-i]?[A-Za-z]\$?\\d+$/u', $token) and !preg_match('/\d/', $this->lookAhead) and ($this->lookAhead !== ':') and ($this->lookAhead !== '.')) { // If it's an external reference (Sheet1!A1 or Sheet1:Sheet2!A1 or Sheet1!$A$1 or Sheet1:Sheet2!$A$1) return $token; - } elseif (preg_match("/^'" . self::REGEX_SHEET_TITLE_QUOTED . '(\\:' . self::REGEX_SHEET_TITLE_QUOTED . ")?'\\!\\$?[A-Ia-i]?[A-Za-z]\\$?\\d+$/u", $token) and !preg_match('/\d/', $this->lookAhead) and ($this->lookAhead != ':') and ($this->lookAhead != '.')) { + } elseif (preg_match("/^'" . self::REGEX_SHEET_TITLE_QUOTED . '(\\:' . self::REGEX_SHEET_TITLE_QUOTED . ")?'\\!\\$?[A-Ia-i]?[A-Za-z]\\$?\\d+$/u", $token) and !preg_match('/\d/', $this->lookAhead) and ($this->lookAhead !== ':') and ($this->lookAhead !== '.')) { // If it's an external reference ('Sheet1'!A1 or 'Sheet1:Sheet2'!A1 or 'Sheet1'!$A$1 or 'Sheet1:Sheet2'!$A$1) return $token; } elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+:(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+$/', $token) && !preg_match('/\d/', $this->lookAhead)) { @@ -1044,19 +1044,19 @@ class Parser } elseif (preg_match("/^'" . self::REGEX_SHEET_TITLE_QUOTED . '(\\:' . self::REGEX_SHEET_TITLE_QUOTED . ")?'\\!\\$?([A-Ia-i]?[A-Za-z])?\\$?\\d+:\\$?([A-Ia-i]?[A-Za-z])?\\$?\\d+$/u", $token) and !preg_match('/\d/', $this->lookAhead)) { // If it's an external range like 'Sheet1'!A1:B2 or 'Sheet1:Sheet2'!A1:B2 or 'Sheet1'!$A$1:$B$2 or 'Sheet1:Sheet2'!$A$1:$B$2 return $token; - } elseif (is_numeric($token) and (!is_numeric($token . $this->lookAhead) or ($this->lookAhead == '')) and ($this->lookAhead != '!') and ($this->lookAhead != ':')) { + } elseif (is_numeric($token) and (!is_numeric($token . $this->lookAhead) or ($this->lookAhead == '')) and ($this->lookAhead !== '!') and ($this->lookAhead !== ':')) { // If it's a number (check that it's not a sheet name or range) return $token; - } elseif (preg_match('/"([^"]|""){0,255}"/', $token) and $this->lookAhead != '"' and (substr_count($token, '"') % 2 == 0)) { + } elseif (preg_match('/"([^"]|""){0,255}"/', $token) and $this->lookAhead !== '"' and (substr_count($token, '"') % 2 == 0)) { // If it's a string (of maximum 255 characters) return $token; - } elseif (preg_match('/^#[A-Z0\\/]{3,5}[!?]{1}$/', $token) or $token == '#N/A') { + } elseif (preg_match('/^#[A-Z0\\/]{3,5}[!?]{1}$/', $token) or $token === '#N/A') { // If it's an error code return $token; - } elseif (preg_match("/^[A-Z0-9\xc0-\xdc\\.]+$/i", $token) and ($this->lookAhead == '(')) { + } elseif (preg_match("/^[A-Z0-9\xc0-\xdc\\.]+$/i", $token) and ($this->lookAhead === '(')) { // if it's a function call return $token; - } elseif (substr($token, -1) == ')') { + } elseif (substr($token, -1) === ')') { // It's an argument of some description (e.g. a named range), // precise nature yet to be determined return $token; @@ -1078,8 +1078,7 @@ class Parser { $this->currentCharacter = 0; $this->formula = $formula; - $this->lookAhead = isset($formula[1]) ? $formula[1] - : ''; + $this->lookAhead = isset($formula[1]) ? $formula[1] : ''; $this->advance(); $this->parseTree = $this->condition(); @@ -1248,10 +1247,10 @@ class Parser */ private function fact() { - if ($this->currentToken == '(') { + if ($this->currentToken === '(') { $this->advance(); // eat the "(" $result = $this->parenthesizedExpression(); - if ($this->currentToken != ')') { + if ($this->currentToken !== ')') { throw new WriterException("')' token expected."); } $this->advance(); // eat the ")" @@ -1299,7 +1298,7 @@ class Parser return $result; } elseif (is_numeric($this->currentToken)) { // If it's a number or a percent - if ($this->lookAhead == '%') { + if ($this->lookAhead === '%') { $result = $this->createTree('ptgPercent', $this->currentToken, ''); $this->advance(); // Skip the percentage operator once we've pre-built that tree } else { @@ -1331,9 +1330,9 @@ class Parser $result = ''; // initialize result $this->advance(); $this->advance(); // eat the "(" - while ($this->currentToken != ')') { + while ($this->currentToken !== ')') { if ($num_args > 0) { - if ($this->currentToken == ',' || $this->currentToken == ';') { + if ($this->currentToken === ',' || $this->currentToken === ';') { $this->advance(); // eat the "," or ";" } else { throw new WriterException("Syntax error: comma expected in function $function, arg #{$num_args}"); diff --git a/src/PhpSpreadsheet/Writer/Xls/Workbook.php b/src/PhpSpreadsheet/Writer/Xls/Workbook.php index b463ce4a..41c8e64e 100644 --- a/src/PhpSpreadsheet/Writer/Xls/Workbook.php +++ b/src/PhpSpreadsheet/Writer/Xls/Workbook.php @@ -267,9 +267,7 @@ class Workbook extends BIFFwriter $this->xfWriters[] = $xfWriter; - $xfIndex = count($this->xfWriters) - 1; - - return $xfIndex; + return count($this->xfWriters) - 1; } /** @@ -319,7 +317,7 @@ class Workbook extends BIFFwriter if ($colorIndex) { $this->colors[$rgb] = $colorIndex; } else { - if (count($this->colors) == 0) { + if (count($this->colors) === 0) { $lastColor = 7; } else { $lastColor = end($this->colors); @@ -437,7 +435,7 @@ class Workbook extends BIFFwriter // Prepare part 3 of the workbook global stream, what goes after the SHEET records $part3 = ''; - if ($this->countryCode != -1) { + if ($this->countryCode !== -1) { $part3 .= $this->writeCountry(); } $part3 .= $this->writeRecalcId(); @@ -918,7 +916,7 @@ class Workbook extends BIFFwriter $record = 0x0022; // Record identifier $length = 0x0002; // Bytes to follow - $f1904 = (Date::getExcelCalendar() == Date::CALENDAR_MAC_1904) + $f1904 = (Date::getExcelCalendar() === Date::CALENDAR_MAC_1904) ? 1 : 0; // Flag for 1904 date system diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php b/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php index e3ddb03c..b9f8f99b 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php @@ -93,7 +93,7 @@ class Workbook extends WriterPart { $objWriter->startElement('workbookPr'); - if (Date::getExcelCalendar() == Date::CALENDAR_MAC_1904) { + if (Date::getExcelCalendar() === Date::CALENDAR_MAC_1904) { $objWriter->writeAttribute('date1904', '1'); } @@ -225,7 +225,7 @@ class Workbook extends WriterPart $objWriter->startElement('sheet'); $objWriter->writeAttribute('name', $pSheetname); $objWriter->writeAttribute('sheetId', $pSheetId); - if ($sheetState != 'visible' && $sheetState != '') { + if ($sheetState !== 'visible' && $sheetState != '') { $objWriter->writeAttribute('state', $sheetState); } $objWriter->writeAttribute('r:id', 'rId' . $pRelId); diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index 7f48a6c1..b39ca2c2 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -141,7 +141,7 @@ class Worksheet extends WriterPart $objWriter->startElement('sheetPr'); if ($pSheet->getParent()->hasMacros()) { //if the workbook have macros, we need to have codeName for the sheet - if ($pSheet->hasCodeName() == false) { + if (!$pSheet->hasCodeName()) { $pSheet->setCodeName($pSheet->getTitle()); } $objWriter->writeAttribute('codeName', $pSheet->getCodeName()); @@ -322,7 +322,7 @@ class Worksheet extends WriterPart } // Set Zero Height row - if ((string) $pSheet->getDefaultRowDimension()->getZeroHeight() == '1' || + if ((string) $pSheet->getDefaultRowDimension()->getZeroHeight() === '1' || strtolower((string) $pSheet->getDefaultRowDimension()->getZeroHeight()) == 'true') { $objWriter->writeAttribute('zeroHeight', '1'); } @@ -428,7 +428,7 @@ class Worksheet extends WriterPart // sheetProtection $objWriter->startElement('sheetProtection'); - if ($pSheet->getProtection()->getPassword() != '') { + if ($pSheet->getProtection()->getPassword() !== '') { $objWriter->writeAttribute('password', $pSheet->getProtection()->getPassword()); } @@ -627,7 +627,7 @@ class Worksheet extends WriterPart $objWriter->writeAttribute('location', str_replace('sheet://', '', $hyperlink->getUrl())); } - if ($hyperlink->getTooltip() != '') { + if ($hyperlink->getTooltip() !== '') { $objWriter->writeAttribute('tooltip', $hyperlink->getTooltip()); $objWriter->writeAttribute('display', $hyperlink->getTooltip()); } @@ -995,12 +995,12 @@ class Worksheet extends WriterPart } // Row visibility - if ($rowDimension->getVisible() == false) { + if (!$rowDimension->getVisible()) { $objWriter->writeAttribute('hidden', 'true'); } // Collapsed - if ($rowDimension->getCollapsed() == true) { + if ($rowDimension->getCollapsed()) { $objWriter->writeAttribute('collapsed', 'true'); } @@ -1105,7 +1105,7 @@ class Worksheet extends WriterPart break; case 'f': // Formula $attributes = $pCell->getFormulaAttributes(); - if ($attributes['t'] == 'array') { + if ($attributes['t'] === 'array') { $objWriter->startElement('f'); $objWriter->writeAttribute('t', 'array'); $objWriter->writeAttribute('ref', $pCellAddress); @@ -1118,7 +1118,7 @@ class Worksheet extends WriterPart } if ($this->getParentWriter()->getOffice2003Compatibility() === false) { if ($this->getParentWriter()->getPreCalculateFormulas()) { - if (!is_array($calculatedValue) && substr($calculatedValue, 0, 1) != '#') { + if (!is_array($calculatedValue) && substr($calculatedValue, 0, 1) !== '#') { $objWriter->writeElement('v', StringHelper::formatNumber($calculatedValue)); } else { $objWriter->writeElement('v', '0'); @@ -1139,7 +1139,7 @@ class Worksheet extends WriterPart break; case 'e': // Error - if (substr($cellValue, 0, 1) == '=') { + if (substr($cellValue, 0, 1) === '=') { $objWriter->writeElement('f', substr($cellValue, 1)); $objWriter->writeElement('v', substr($cellValue, 1)); } else {