From 1b00fac6ad866eeb619d9effec7d86de82383075 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sat, 22 Jun 2019 23:15:14 +0200 Subject: [PATCH] Minor improvements (#1029) * Another collection of minor improvements * Fix broken test * And style * Seriously?!? The order of returned types declared in a docblock is majorly important? --- src/PhpSpreadsheet/Cell/Cell.php | 2 +- src/PhpSpreadsheet/Cell/Coordinate.php | 4 +- src/PhpSpreadsheet/Collection/Cells.php | 4 +- src/PhpSpreadsheet/Reader/Csv.php | 7 +-- src/PhpSpreadsheet/Reader/Ods.php | 16 +++--- src/PhpSpreadsheet/ReferenceHelper.php | 12 ++--- src/PhpSpreadsheet/Writer/BaseWriter.php | 55 ------------------- src/PhpSpreadsheet/Writer/IWriter.php | 69 ++++++++++++++++++++++++ 8 files changed, 88 insertions(+), 81 deletions(-) diff --git a/src/PhpSpreadsheet/Cell/Cell.php b/src/PhpSpreadsheet/Cell/Cell.php index 416b4a99..813eee4a 100644 --- a/src/PhpSpreadsheet/Cell/Cell.php +++ b/src/PhpSpreadsheet/Cell/Cell.php @@ -523,7 +523,7 @@ class Cell /** * If this cell is in a merge range, then return the range. * - * @return string + * @return false|string */ public function getMergeRange() { diff --git a/src/PhpSpreadsheet/Cell/Coordinate.php b/src/PhpSpreadsheet/Cell/Coordinate.php index 12e3b689..5e20ec6d 100644 --- a/src/PhpSpreadsheet/Cell/Coordinate.php +++ b/src/PhpSpreadsheet/Cell/Coordinate.php @@ -157,14 +157,12 @@ abstract class Coordinate } // Build range - $imploded = []; $counter = count($pRange); for ($i = 0; $i < $counter; ++$i) { $pRange[$i] = implode(':', $pRange[$i]); } - $imploded = implode(',', $pRange); - return $imploded; + return implode(',', $pRange); } /** diff --git a/src/PhpSpreadsheet/Collection/Cells.php b/src/PhpSpreadsheet/Collection/Cells.php index 465a0698..84c3d300 100644 --- a/src/PhpSpreadsheet/Collection/Cells.php +++ b/src/PhpSpreadsheet/Collection/Cells.php @@ -241,7 +241,7 @@ class Cells */ public function getHighestColumn($row = null) { - if ($row == null) { + if ($row === null) { $colRow = $this->getHighestRowAndColumn(); return $colRow['column']; @@ -272,7 +272,7 @@ class Cells */ public function getHighestRow($column = null) { - if ($column == null) { + if ($column === null) { $colRow = $this->getHighestRowAndColumn(); return $colRow['row']; diff --git a/src/PhpSpreadsheet/Reader/Csv.php b/src/PhpSpreadsheet/Reader/Csv.php index 47f3d1a3..81ca1a8c 100644 --- a/src/PhpSpreadsheet/Reader/Csv.php +++ b/src/PhpSpreadsheet/Reader/Csv.php @@ -143,7 +143,7 @@ class Csv extends BaseReader return; } - return $this->skipBOM(); + $this->skipBOM(); } /** @@ -184,8 +184,9 @@ class Csv extends BaseReader // If number of lines is 0, nothing to infer : fall back to the default if ($numberLines === 0) { $this->delimiter = reset($potentialDelimiters); + $this->skipBOM(); - return $this->skipBOM(); + return; } // Calculate the mean square deviations for each delimiter (ignoring delimiters that haven't been found consistently) @@ -230,7 +231,7 @@ class Csv extends BaseReader $this->delimiter = reset($potentialDelimiters); } - return $this->skipBOM(); + $this->skipBOM(); } /** diff --git a/src/PhpSpreadsheet/Reader/Ods.php b/src/PhpSpreadsheet/Reader/Ods.php index 94ac467d..cbbcf7a0 100644 --- a/src/PhpSpreadsheet/Reader/Ods.php +++ b/src/PhpSpreadsheet/Reader/Ods.php @@ -52,7 +52,7 @@ class Ods extends BaseReader $stat = $zip->statName('mimetype'); if ($stat && ($stat['size'] <= 255)) { $mimeType = $zip->getFromName($stat['name']); - } elseif ($stat = $zip->statName('META-INF/manifest.xml')) { + } elseif ($zip->statName('META-INF/manifest.xml')) { $xml = simplexml_load_string( $this->securityScanner->scan($zip->getFromName('META-INF/manifest.xml')), 'SimpleXMLElement', @@ -513,7 +513,7 @@ class Ods extends BaseReader foreach ($paragraphs as $pData) { $dataArray[] = $this->scanElementForText($pData); } - $allCellDataText = implode($dataArray, "\n"); + $allCellDataText = implode("\n", $dataArray); $type = $cellData->getAttributeNS($officeNs, 'value-type'); @@ -580,12 +580,12 @@ class Ods extends BaseReader ); $dataValue = Date::formattedPHPToExcel( - $year, - $month, - $day, - $hour, - $minute, - $second + (int) $year, + (int) $month, + (int) $day, + (int) $hour, + (int) $minute, + (int) $second ); if ($dataValue != floor($dataValue)) { diff --git a/src/PhpSpreadsheet/ReferenceHelper.php b/src/PhpSpreadsheet/ReferenceHelper.php index 54bc182a..a14880b7 100644 --- a/src/PhpSpreadsheet/ReferenceHelper.php +++ b/src/PhpSpreadsheet/ReferenceHelper.php @@ -82,13 +82,10 @@ class ReferenceHelper */ public static function cellSort($a, $b) { - $ac = $bc = ''; - $ar = $br = 0; - sscanf($a, '%[A-Z]%d', $ac, $ar); sscanf($b, '%[A-Z]%d', $bc, $br); - if ($ar == $br) { + if ($ar === $br) { return strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc); } @@ -106,13 +103,10 @@ class ReferenceHelper */ public static function cellReverseSort($a, $b) { - $ac = $bc = ''; - $ar = $br = 0; - sscanf($a, '%[A-Z]%d', $ac, $ar); sscanf($b, '%[A-Z]%d', $bc, $br); - if ($ar == $br) { + if ($ar === $br) { return 1 - strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc); } @@ -625,7 +619,7 @@ class ReferenceHelper * Update references within formulas. * * @param string $pFormula Formula to update - * @param int $pBefore Insert before this one + * @param string $pBefore Insert before this one * @param int $pNumCols Number of columns to insert * @param int $pNumRows Number of rows to insert * @param string $sheetName Worksheet name/title diff --git a/src/PhpSpreadsheet/Writer/BaseWriter.php b/src/PhpSpreadsheet/Writer/BaseWriter.php index 122783f3..f13150d7 100644 --- a/src/PhpSpreadsheet/Writer/BaseWriter.php +++ b/src/PhpSpreadsheet/Writer/BaseWriter.php @@ -35,27 +35,11 @@ abstract class BaseWriter implements IWriter */ private $diskCachingDirectory = './'; - /** - * Write charts in workbook? - * If this is true, then the Writer will write definitions for any charts that exist in the PhpSpreadsheet object. - * If false (the default) it will ignore any charts defined in the PhpSpreadsheet object. - * - * @return bool - */ public function getIncludeCharts() { return $this->includeCharts; } - /** - * Set write charts in workbook - * Set to true, to advise the Writer to include any charts that exist in the PhpSpreadsheet object. - * Set to false (the default) to ignore charts. - * - * @param bool $pValue - * - * @return IWriter - */ public function setIncludeCharts($pValue) { $this->includeCharts = (bool) $pValue; @@ -63,30 +47,11 @@ abstract class BaseWriter implements IWriter return $this; } - /** - * Get Pre-Calculate Formulas flag - * If this is true (the default), then the writer will recalculate all formulae in a workbook when saving, - * so that the pre-calculated values are immediately available to MS Excel or other office spreadsheet - * viewer when opening the file - * If false, then formulae are not calculated on save. This is faster for saving in PhpSpreadsheet, but slower - * when opening the resulting file in MS Excel, because Excel has to recalculate the formulae itself. - * - * @return bool - */ public function getPreCalculateFormulas() { return $this->preCalculateFormulas; } - /** - * Set Pre-Calculate Formulas - * Set to true (the default) to advise the Writer to calculate all formulae on save - * Set to false to prevent precalculation of formulae on save. - * - * @param bool $pValue Pre-Calculate Formulas? - * - * @return IWriter - */ public function setPreCalculateFormulas($pValue) { $this->preCalculateFormulas = (bool) $pValue; @@ -94,26 +59,11 @@ abstract class BaseWriter implements IWriter return $this; } - /** - * Get use disk caching where possible? - * - * @return bool - */ public function getUseDiskCaching() { return $this->useDiskCaching; } - /** - * Set use disk caching where possible? - * - * @param bool $pValue - * @param string $pDirectory Disk caching directory - * - * @throws Exception when directory does not exist - * - * @return IWriter - */ public function setUseDiskCaching($pValue, $pDirectory = null) { $this->useDiskCaching = $pValue; @@ -129,11 +79,6 @@ abstract class BaseWriter implements IWriter return $this; } - /** - * Get disk caching directory. - * - * @return string - */ public function getDiskCachingDirectory() { return $this->diskCachingDirectory; diff --git a/src/PhpSpreadsheet/Writer/IWriter.php b/src/PhpSpreadsheet/Writer/IWriter.php index 9ce45a19..448b532f 100644 --- a/src/PhpSpreadsheet/Writer/IWriter.php +++ b/src/PhpSpreadsheet/Writer/IWriter.php @@ -13,6 +13,49 @@ interface IWriter */ public function __construct(Spreadsheet $spreadsheet); + /** + * Write charts in workbook? + * If this is true, then the Writer will write definitions for any charts that exist in the PhpSpreadsheet object. + * If false (the default) it will ignore any charts defined in the PhpSpreadsheet object. + * + * @return bool + */ + public function getIncludeCharts(); + + /** + * Set write charts in workbook + * Set to true, to advise the Writer to include any charts that exist in the PhpSpreadsheet object. + * Set to false (the default) to ignore charts. + * + * @param bool $pValue + * + * @return IWriter + */ + public function setIncludeCharts($pValue); + + /** + * Get Pre-Calculate Formulas flag + * If this is true (the default), then the writer will recalculate all formulae in a workbook when saving, + * so that the pre-calculated values are immediately available to MS Excel or other office spreadsheet + * viewer when opening the file + * If false, then formulae are not calculated on save. This is faster for saving in PhpSpreadsheet, but slower + * when opening the resulting file in MS Excel, because Excel has to recalculate the formulae itself. + * + * @return bool + */ + public function getPreCalculateFormulas(); + + /** + * Set Pre-Calculate Formulas + * Set to true (the default) to advise the Writer to calculate all formulae on save + * Set to false to prevent precalculation of formulae on save. + * + * @param bool $pValue Pre-Calculate Formulas? + * + * @return IWriter + */ + public function setPreCalculateFormulas($pValue); + /** * Save PhpSpreadsheet to file. * @@ -21,4 +64,30 @@ interface IWriter * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception */ public function save($pFilename); + + /** + * Get use disk caching where possible? + * + * @return bool + */ + public function getUseDiskCaching(); + + /** + * Set use disk caching where possible? + * + * @param bool $pValue + * @param string $pDirectory Disk caching directory + * + * @throws Exception when directory does not exist + * + * @return IWriter + */ + public function setUseDiskCaching($pValue, $pDirectory = null); + + /** + * Get disk caching directory. + * + * @return string + */ + public function getDiskCachingDirectory(); }