From 14c0605f00272cd55a605e4531f7864bfbfb3a45 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 17 Feb 2021 18:16:59 +0100 Subject: [PATCH 01/10] Let's see what Scrutinizer makes of these changes --- src/PhpSpreadsheet/Worksheet/CellIterator.php | 4 +-- src/PhpSpreadsheet/Worksheet/Column.php | 2 +- .../Worksheet/ColumnCellIterator.php | 11 +++---- .../Worksheet/ColumnDimension.php | 22 +++++++------- .../Worksheet/ColumnIterator.php | 16 ++++++---- src/PhpSpreadsheet/Worksheet/Dimension.php | 30 +++++++++---------- src/PhpSpreadsheet/Worksheet/Iterator.php | 4 +-- src/PhpSpreadsheet/Worksheet/Row.php | 4 +-- .../Worksheet/RowCellIterator.php | 13 ++++---- src/PhpSpreadsheet/Worksheet/RowDimension.php | 18 +++++------ src/PhpSpreadsheet/Worksheet/RowIterator.php | 12 ++++---- .../Functions/MathTrig/SubTotalTest.php | 6 ++-- 12 files changed, 75 insertions(+), 67 deletions(-) diff --git a/src/PhpSpreadsheet/Worksheet/CellIterator.php b/src/PhpSpreadsheet/Worksheet/CellIterator.php index 45f76cab37..af838bf51d 100644 --- a/src/PhpSpreadsheet/Worksheet/CellIterator.php +++ b/src/PhpSpreadsheet/Worksheet/CellIterator.php @@ -33,7 +33,7 @@ public function __destruct() * * @return bool */ - public function getIterateOnlyExistingCells() + public function getIterateOnlyExistingCells(): bool { return $this->onlyExistingCells; } @@ -48,7 +48,7 @@ abstract protected function adjustForExistingOnlyRange(); * * @param bool $value */ - public function setIterateOnlyExistingCells($value): void + public function setIterateOnlyExistingCells(bool $value): void { $this->onlyExistingCells = (bool) $value; diff --git a/src/PhpSpreadsheet/Worksheet/Column.php b/src/PhpSpreadsheet/Worksheet/Column.php index 410e80735a..555173d12c 100644 --- a/src/PhpSpreadsheet/Worksheet/Column.php +++ b/src/PhpSpreadsheet/Worksheet/Column.php @@ -44,7 +44,7 @@ public function __destruct() * * @return string */ - public function getColumnIndex() + public function getColumnIndex(): string { return $this->columnIndex; } diff --git a/src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php b/src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php index 714ee7ce3b..96e9f1d88f 100644 --- a/src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php +++ b/src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheet\Worksheet; +use PhpOffice\PhpSpreadsheet\Cell\Cell; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; @@ -59,7 +60,7 @@ public function __construct(?Worksheet $subject = null, $columnIndex = 'A', $sta * * @return $this */ - public function resetStart($startRow = 1) + public function resetStart(int $startRow = 1) { $this->startRow = $startRow; $this->adjustForExistingOnlyRange(); @@ -90,7 +91,7 @@ public function resetEnd($endRow = null) * * @return $this */ - public function seek($row = 1) + public function seek(int $row = 1) { if ($this->onlyExistingCells && !($this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $row))) { throw new PhpSpreadsheetException('In "IterateOnlyExistingCells" mode and Cell does not exist'); @@ -116,7 +117,7 @@ public function rewind(): void * * @return \PhpOffice\PhpSpreadsheet\Cell\Cell */ - public function current() + public function current(): Cell { return $this->worksheet->getCellByColumnAndRow($this->columnIndex, $this->currentRow); } @@ -126,7 +127,7 @@ public function current() * * @return int */ - public function key() + public function key(): int { return $this->currentRow; } @@ -164,7 +165,7 @@ public function prev(): void * * @return bool */ - public function valid() + public function valid(): bool { return $this->currentRow <= $this->endRow && $this->currentRow >= $this->startRow; } diff --git a/src/PhpSpreadsheet/Worksheet/ColumnDimension.php b/src/PhpSpreadsheet/Worksheet/ColumnDimension.php index 4e87a344c8..2ed6f59087 100644 --- a/src/PhpSpreadsheet/Worksheet/ColumnDimension.php +++ b/src/PhpSpreadsheet/Worksheet/ColumnDimension.php @@ -46,7 +46,7 @@ public function __construct($pIndex = 'A') * * @return string */ - public function getColumnIndex() + public function getColumnIndex(): string { return $this->columnIndex; } @@ -54,13 +54,13 @@ public function getColumnIndex() /** * Set column index as string eg: 'A'. * - * @param string $pValue + * @param string $index * * @return $this */ - public function setColumnIndex($pValue) + public function setColumnIndex(string $index) { - $this->columnIndex = $pValue; + $this->columnIndex = $index; return $this; } @@ -78,13 +78,13 @@ public function getWidth() /** * Set Width. * - * @param float $pValue + * @param float $width * * @return $this */ - public function setWidth($pValue) + public function setWidth($width) { - $this->width = $pValue; + $this->width = $width; return $this; } @@ -94,7 +94,7 @@ public function setWidth($pValue) * * @return bool */ - public function getAutoSize() + public function getAutoSize(): bool { return $this->autoSize; } @@ -102,13 +102,13 @@ public function getAutoSize() /** * Set Auto Size. * - * @param bool $pValue + * @param bool $autosizeEnabled * * @return $this */ - public function setAutoSize($pValue) + public function setAutoSize(bool $autosizeEnabled) { - $this->autoSize = $pValue; + $this->autoSize = $autosizeEnabled; return $this; } diff --git a/src/PhpSpreadsheet/Worksheet/ColumnIterator.php b/src/PhpSpreadsheet/Worksheet/ColumnIterator.php index d0bb20cc96..0ee8a73da4 100644 --- a/src/PhpSpreadsheet/Worksheet/ColumnIterator.php +++ b/src/PhpSpreadsheet/Worksheet/ColumnIterator.php @@ -67,11 +67,13 @@ public function __destruct() * * @return $this */ - public function resetStart($startColumn = 'A') + public function resetStart(string $startColumn = 'A') { $startColumnIndex = Coordinate::columnIndexFromString($startColumn); if ($startColumnIndex > Coordinate::columnIndexFromString($this->worksheet->getHighestColumn())) { - throw new Exception("Start column ({$startColumn}) is beyond highest column ({$this->worksheet->getHighestColumn()})"); + throw new Exception( + "Start column ({$startColumn}) is beyond highest column ({$this->worksheet->getHighestColumn()})" + ); } $this->startColumnIndex = $startColumnIndex; @@ -105,11 +107,13 @@ public function resetEnd($endColumn = null) * * @return $this */ - public function seek($column = 'A') + public function seek(string $column = 'A') { $column = Coordinate::columnIndexFromString($column); if (($column < $this->startColumnIndex) || ($column > $this->endColumnIndex)) { - throw new PhpSpreadsheetException("Column $column is out of range ({$this->startColumnIndex} - {$this->endColumnIndex})"); + throw new PhpSpreadsheetException( + "Column $column is out of range ({$this->startColumnIndex} - {$this->endColumnIndex})" + ); } $this->currentColumnIndex = $column; @@ -139,7 +143,7 @@ public function current() * * @return string */ - public function key() + public function key(): string { return Coordinate::stringFromColumnIndex($this->currentColumnIndex); } @@ -165,7 +169,7 @@ public function prev(): void * * @return bool */ - public function valid() + public function valid(): bool { return $this->currentColumnIndex <= $this->endColumnIndex && $this->currentColumnIndex >= $this->startColumnIndex; } diff --git a/src/PhpSpreadsheet/Worksheet/Dimension.php b/src/PhpSpreadsheet/Worksheet/Dimension.php index a27daf092d..bebead11f2 100644 --- a/src/PhpSpreadsheet/Worksheet/Dimension.php +++ b/src/PhpSpreadsheet/Worksheet/Dimension.php @@ -50,7 +50,7 @@ public function __construct($initialValue = null) * * @return bool */ - public function getVisible() + public function getVisible(): bool { return $this->visible; } @@ -58,13 +58,13 @@ public function getVisible() /** * Set Visible. * - * @param bool $pValue + * @param bool $visible * * @return $this */ - public function setVisible($pValue) + public function setVisible(bool $visible) { - $this->visible = (bool) $pValue; + $this->visible = $visible; return $this; } @@ -74,7 +74,7 @@ public function setVisible($pValue) * * @return int */ - public function getOutlineLevel() + public function getOutlineLevel(): int { return $this->outlineLevel; } @@ -83,17 +83,17 @@ public function getOutlineLevel() * Set Outline Level. * Value must be between 0 and 7. * - * @param int $pValue + * @param int $level * * @return $this */ - public function setOutlineLevel($pValue) + public function setOutlineLevel(int $level) { - if ($pValue < 0 || $pValue > 7) { + if ($level < 0 || $level > 7) { throw new PhpSpreadsheetException('Outline level must range between 0 and 7.'); } - $this->outlineLevel = $pValue; + $this->outlineLevel = $level; return $this; } @@ -103,7 +103,7 @@ public function setOutlineLevel($pValue) * * @return bool */ - public function getCollapsed() + public function getCollapsed(): bool { return $this->collapsed; } @@ -111,13 +111,13 @@ public function getCollapsed() /** * Set Collapsed. * - * @param bool $pValue + * @param bool $collapsed * * @return $this */ - public function setCollapsed($pValue) + public function setCollapsed(bool $collapsed) { - $this->collapsed = (bool) $pValue; + $this->collapsed = $collapsed; return $this; } @@ -127,7 +127,7 @@ public function setCollapsed($pValue) * * @return int */ - public function getXfIndex() + public function getXfIndex(): ?int { return $this->xfIndex; } @@ -139,7 +139,7 @@ public function getXfIndex() * * @return $this */ - public function setXfIndex($pValue) + public function setXfIndex(int $pValue) { $this->xfIndex = $pValue; diff --git a/src/PhpSpreadsheet/Worksheet/Iterator.php b/src/PhpSpreadsheet/Worksheet/Iterator.php index 6cfed37a47..555d5fe5e0 100644 --- a/src/PhpSpreadsheet/Worksheet/Iterator.php +++ b/src/PhpSpreadsheet/Worksheet/Iterator.php @@ -50,7 +50,7 @@ public function rewind(): void * * @return Worksheet */ - public function current() + public function current(): Worksheet { return $this->subject->getSheet($this->position); } @@ -60,7 +60,7 @@ public function current() * * @return int */ - public function key() + public function key(): int { return $this->position; } diff --git a/src/PhpSpreadsheet/Worksheet/Row.php b/src/PhpSpreadsheet/Worksheet/Row.php index 4f48a34618..05aee0d069 100644 --- a/src/PhpSpreadsheet/Worksheet/Row.php +++ b/src/PhpSpreadsheet/Worksheet/Row.php @@ -44,7 +44,7 @@ public function __destruct() * * @return int */ - public function getRowIndex() + public function getRowIndex(): int { return $this->rowIndex; } @@ -67,7 +67,7 @@ public function getCellIterator($startColumn = 'A', $endColumn = null) * * @return Worksheet */ - public function getWorksheet() + public function getWorksheet(): Worksheet { return $this->worksheet; } diff --git a/src/PhpSpreadsheet/Worksheet/RowCellIterator.php b/src/PhpSpreadsheet/Worksheet/RowCellIterator.php index 9b9d54eb61..91e0690455 100644 --- a/src/PhpSpreadsheet/Worksheet/RowCellIterator.php +++ b/src/PhpSpreadsheet/Worksheet/RowCellIterator.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheet\Worksheet; +use PhpOffice\PhpSpreadsheet\Cell\Cell; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; @@ -59,7 +60,7 @@ public function __construct(?Worksheet $worksheet = null, $rowIndex = 1, $startC * * @return $this */ - public function resetStart($startColumn = 'A') + public function resetStart(string $startColumn = 'A') { $this->startColumnIndex = Coordinate::columnIndexFromString($startColumn); $this->adjustForExistingOnlyRange(); @@ -91,7 +92,7 @@ public function resetEnd($endColumn = null) * * @return $this */ - public function seek($column = 'A') + public function seek(string $column = 'A') { $columnx = $column; $column = Coordinate::columnIndexFromString($column); @@ -117,9 +118,9 @@ public function rewind(): void /** * Return the current cell in this worksheet row. * - * @return \PhpOffice\PhpSpreadsheet\Cell\Cell + * @return Cell */ - public function current() + public function current(): Cell { return $this->worksheet->getCellByColumnAndRow($this->currentColumnIndex, $this->rowIndex); } @@ -129,7 +130,7 @@ public function current() * * @return string */ - public function key() + public function key(): string { return Coordinate::stringFromColumnIndex($this->currentColumnIndex); } @@ -169,7 +170,7 @@ public function valid() * * @return int */ - public function getCurrentColumnIndex() + public function getCurrentColumnIndex(): int { return $this->currentColumnIndex; } diff --git a/src/PhpSpreadsheet/Worksheet/RowDimension.php b/src/PhpSpreadsheet/Worksheet/RowDimension.php index c4a87bdb55..f1aa6f9101 100644 --- a/src/PhpSpreadsheet/Worksheet/RowDimension.php +++ b/src/PhpSpreadsheet/Worksheet/RowDimension.php @@ -46,7 +46,7 @@ public function __construct($pIndex = 0) * * @return int */ - public function getRowIndex() + public function getRowIndex(): int { return $this->rowIndex; } @@ -54,13 +54,13 @@ public function getRowIndex() /** * Set Row Index. * - * @param int $pValue + * @param int $index * * @return $this */ - public function setRowIndex($pValue) + public function setRowIndex(int $index) { - $this->rowIndex = $pValue; + $this->rowIndex = $index; return $this; } @@ -78,13 +78,13 @@ public function getRowHeight() /** * Set Row Height. * - * @param float $pValue + * @param float $height * * @return $this */ - public function setRowHeight($pValue) + public function setRowHeight($height) { - $this->height = $pValue; + $this->height = $height; return $this; } @@ -94,7 +94,7 @@ public function setRowHeight($pValue) * * @return bool */ - public function getZeroHeight() + public function getZeroHeight(): bool { return $this->zeroHeight; } @@ -106,7 +106,7 @@ public function getZeroHeight() * * @return $this */ - public function setZeroHeight($pValue) + public function setZeroHeight(bool $pValue) { $this->zeroHeight = $pValue; diff --git a/src/PhpSpreadsheet/Worksheet/RowIterator.php b/src/PhpSpreadsheet/Worksheet/RowIterator.php index 4254253313..44794c61cd 100644 --- a/src/PhpSpreadsheet/Worksheet/RowIterator.php +++ b/src/PhpSpreadsheet/Worksheet/RowIterator.php @@ -65,10 +65,12 @@ public function __destruct() * * @return $this */ - public function resetStart($startRow = 1) + public function resetStart(int $startRow = 1) { if ($startRow > $this->subject->getHighestRow()) { - throw new PhpSpreadsheetException("Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})"); + throw new PhpSpreadsheetException( + "Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})" + ); } $this->startRow = $startRow; @@ -101,7 +103,7 @@ public function resetEnd($endRow = null) * * @return $this */ - public function seek($row = 1) + public function seek(int $row = 1) { if (($row < $this->startRow) || ($row > $this->endRow)) { throw new PhpSpreadsheetException("Row $row is out of range ({$this->startRow} - {$this->endRow})"); @@ -134,7 +136,7 @@ public function current() * * @return int */ - public function key() + public function key(): int { return $this->position; } @@ -160,7 +162,7 @@ public function prev(): void * * @return bool */ - public function valid() + public function valid(): bool { return $this->position <= $this->endRow && $this->position >= $this->startRow; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php index 14865673f1..bead7edacf 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php @@ -59,9 +59,9 @@ public function providerSUBTOTAL() protected function rowVisibility() { - $data = [1 => false, 2 => true, 3 => false, 4 => true, 5 => false, 6 => false, 7 => false, 8 => true, 9 => false, 10 => true, 11 => true]; - foreach ($data as $k => $v) { - yield $k => $v; + $data = [1 => false, 2 => true, 3 => false, 4 => true, 5 => false, 6 => false, 7 => false, 8 => true, 9 => false, 10 => true, 11 => true, 12 => false]; + foreach ($data as $row => $visibility) { + yield $row => $visibility; } } From 8061aff06550a3e0eb8d2f6be6333d4dc7eabe75 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 17 Feb 2021 18:45:13 +0100 Subject: [PATCH 02/10] PHPCS Fixes --- src/PhpSpreadsheet/Style/Style.php | 2 +- src/PhpSpreadsheet/Worksheet/CellIterator.php | 4 --- src/PhpSpreadsheet/Worksheet/Column.php | 2 -- .../Worksheet/ColumnCellIterator.php | 8 +----- .../Worksheet/ColumnDimension.php | 8 ------ .../Worksheet/ColumnIterator.php | 4 --- src/PhpSpreadsheet/Worksheet/Dimension.php | 14 ---------- src/PhpSpreadsheet/Worksheet/Iterator.php | 4 --- src/PhpSpreadsheet/Worksheet/Row.php | 4 --- .../Worksheet/RowCellIterator.php | 8 +----- src/PhpSpreadsheet/Worksheet/RowDimension.php | 8 ------ src/PhpSpreadsheet/Worksheet/RowIterator.php | 4 --- .../Functions/MathTrig/SubTotalTest.php | 8 +++--- .../Calculation/MathTrig/SUBTOTALHIDDEN.php | 26 +++++++++++++++++++ 14 files changed, 33 insertions(+), 71 deletions(-) diff --git a/src/PhpSpreadsheet/Style/Style.php b/src/PhpSpreadsheet/Style/Style.php index f7c1be23fc..df53a16ca8 100644 --- a/src/PhpSpreadsheet/Style/Style.php +++ b/src/PhpSpreadsheet/Style/Style.php @@ -367,7 +367,7 @@ public function applyFromArray(array $pStyles, $pAdvanced = true) case 'ROW': $oldXfIndexes = []; for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) { - if ($this->getActiveSheet()->getRowDimension($row)->getXfIndex() == null) { + if ($this->getActiveSheet()->getRowDimension($row)->getXfIndex() === null) { $oldXfIndexes[0] = true; // row without explicit style should be formatted based on default style } else { $oldXfIndexes[$this->getActiveSheet()->getRowDimension($row)->getXfIndex()] = true; diff --git a/src/PhpSpreadsheet/Worksheet/CellIterator.php b/src/PhpSpreadsheet/Worksheet/CellIterator.php index af838bf51d..e0f35b641f 100644 --- a/src/PhpSpreadsheet/Worksheet/CellIterator.php +++ b/src/PhpSpreadsheet/Worksheet/CellIterator.php @@ -30,8 +30,6 @@ public function __destruct() /** * Get loop only existing cells. - * - * @return bool */ public function getIterateOnlyExistingCells(): bool { @@ -45,8 +43,6 @@ abstract protected function adjustForExistingOnlyRange(); /** * Set the iterator to loop only existing cells. - * - * @param bool $value */ public function setIterateOnlyExistingCells(bool $value): void { diff --git a/src/PhpSpreadsheet/Worksheet/Column.php b/src/PhpSpreadsheet/Worksheet/Column.php index 555173d12c..763806a0e5 100644 --- a/src/PhpSpreadsheet/Worksheet/Column.php +++ b/src/PhpSpreadsheet/Worksheet/Column.php @@ -41,8 +41,6 @@ public function __destruct() /** * Get column index as string eg: 'A'. - * - * @return string */ public function getColumnIndex(): string { diff --git a/src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php b/src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php index 96e9f1d88f..f406f8adc2 100644 --- a/src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php +++ b/src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php @@ -114,18 +114,14 @@ public function rewind(): void /** * Return the current cell in this worksheet column. - * - * @return \PhpOffice\PhpSpreadsheet\Cell\Cell */ - public function current(): Cell + public function current(): ?Cell { return $this->worksheet->getCellByColumnAndRow($this->columnIndex, $this->currentRow); } /** * Return the current iterator key. - * - * @return int */ public function key(): int { @@ -162,8 +158,6 @@ public function prev(): void /** * Indicate if more rows exist in the worksheet range of rows that we're iterating. - * - * @return bool */ public function valid(): bool { diff --git a/src/PhpSpreadsheet/Worksheet/ColumnDimension.php b/src/PhpSpreadsheet/Worksheet/ColumnDimension.php index 2ed6f59087..cb80cdee07 100644 --- a/src/PhpSpreadsheet/Worksheet/ColumnDimension.php +++ b/src/PhpSpreadsheet/Worksheet/ColumnDimension.php @@ -43,8 +43,6 @@ public function __construct($pIndex = 'A') /** * Get column index as string eg: 'A'. - * - * @return string */ public function getColumnIndex(): string { @@ -54,8 +52,6 @@ public function getColumnIndex(): string /** * Set column index as string eg: 'A'. * - * @param string $index - * * @return $this */ public function setColumnIndex(string $index) @@ -91,8 +87,6 @@ public function setWidth($width) /** * Get Auto Size. - * - * @return bool */ public function getAutoSize(): bool { @@ -102,8 +96,6 @@ public function getAutoSize(): bool /** * Set Auto Size. * - * @param bool $autosizeEnabled - * * @return $this */ public function setAutoSize(bool $autosizeEnabled) diff --git a/src/PhpSpreadsheet/Worksheet/ColumnIterator.php b/src/PhpSpreadsheet/Worksheet/ColumnIterator.php index 0ee8a73da4..c0200b69de 100644 --- a/src/PhpSpreadsheet/Worksheet/ColumnIterator.php +++ b/src/PhpSpreadsheet/Worksheet/ColumnIterator.php @@ -140,8 +140,6 @@ public function current() /** * Return the current iterator key. - * - * @return string */ public function key(): string { @@ -166,8 +164,6 @@ public function prev(): void /** * Indicate if more columns exist in the worksheet range of columns that we're iterating. - * - * @return bool */ public function valid(): bool { diff --git a/src/PhpSpreadsheet/Worksheet/Dimension.php b/src/PhpSpreadsheet/Worksheet/Dimension.php index bebead11f2..4b3a0da869 100644 --- a/src/PhpSpreadsheet/Worksheet/Dimension.php +++ b/src/PhpSpreadsheet/Worksheet/Dimension.php @@ -47,8 +47,6 @@ public function __construct($initialValue = null) /** * Get Visible. - * - * @return bool */ public function getVisible(): bool { @@ -58,8 +56,6 @@ public function getVisible(): bool /** * Set Visible. * - * @param bool $visible - * * @return $this */ public function setVisible(bool $visible) @@ -71,8 +67,6 @@ public function setVisible(bool $visible) /** * Get Outline Level. - * - * @return int */ public function getOutlineLevel(): int { @@ -83,8 +77,6 @@ public function getOutlineLevel(): int * Set Outline Level. * Value must be between 0 and 7. * - * @param int $level - * * @return $this */ public function setOutlineLevel(int $level) @@ -100,8 +92,6 @@ public function setOutlineLevel(int $level) /** * Get Collapsed. - * - * @return bool */ public function getCollapsed(): bool { @@ -111,8 +101,6 @@ public function getCollapsed(): bool /** * Set Collapsed. * - * @param bool $collapsed - * * @return $this */ public function setCollapsed(bool $collapsed) @@ -135,8 +123,6 @@ public function getXfIndex(): ?int /** * Set index to cellXf. * - * @param int $pValue - * * @return $this */ public function setXfIndex(int $pValue) diff --git a/src/PhpSpreadsheet/Worksheet/Iterator.php b/src/PhpSpreadsheet/Worksheet/Iterator.php index 555d5fe5e0..93b0bb04b7 100644 --- a/src/PhpSpreadsheet/Worksheet/Iterator.php +++ b/src/PhpSpreadsheet/Worksheet/Iterator.php @@ -47,8 +47,6 @@ public function rewind(): void /** * Current Worksheet. - * - * @return Worksheet */ public function current(): Worksheet { @@ -57,8 +55,6 @@ public function current(): Worksheet /** * Current key. - * - * @return int */ public function key(): int { diff --git a/src/PhpSpreadsheet/Worksheet/Row.php b/src/PhpSpreadsheet/Worksheet/Row.php index 05aee0d069..cf93574301 100644 --- a/src/PhpSpreadsheet/Worksheet/Row.php +++ b/src/PhpSpreadsheet/Worksheet/Row.php @@ -41,8 +41,6 @@ public function __destruct() /** * Get row index. - * - * @return int */ public function getRowIndex(): int { @@ -64,8 +62,6 @@ public function getCellIterator($startColumn = 'A', $endColumn = null) /** * Returns bound worksheet. - * - * @return Worksheet */ public function getWorksheet(): Worksheet { diff --git a/src/PhpSpreadsheet/Worksheet/RowCellIterator.php b/src/PhpSpreadsheet/Worksheet/RowCellIterator.php index 91e0690455..f5f97d956c 100644 --- a/src/PhpSpreadsheet/Worksheet/RowCellIterator.php +++ b/src/PhpSpreadsheet/Worksheet/RowCellIterator.php @@ -117,18 +117,14 @@ public function rewind(): void /** * Return the current cell in this worksheet row. - * - * @return Cell */ - public function current(): Cell + public function current(): ?Cell { return $this->worksheet->getCellByColumnAndRow($this->currentColumnIndex, $this->rowIndex); } /** * Return the current iterator key. - * - * @return string */ public function key(): string { @@ -167,8 +163,6 @@ public function valid() /** * Return the current iterator position. - * - * @return int */ public function getCurrentColumnIndex(): int { diff --git a/src/PhpSpreadsheet/Worksheet/RowDimension.php b/src/PhpSpreadsheet/Worksheet/RowDimension.php index f1aa6f9101..d86dd80f8b 100644 --- a/src/PhpSpreadsheet/Worksheet/RowDimension.php +++ b/src/PhpSpreadsheet/Worksheet/RowDimension.php @@ -43,8 +43,6 @@ public function __construct($pIndex = 0) /** * Get Row Index. - * - * @return int */ public function getRowIndex(): int { @@ -54,8 +52,6 @@ public function getRowIndex(): int /** * Set Row Index. * - * @param int $index - * * @return $this */ public function setRowIndex(int $index) @@ -91,8 +87,6 @@ public function setRowHeight($height) /** * Get ZeroHeight. - * - * @return bool */ public function getZeroHeight(): bool { @@ -102,8 +96,6 @@ public function getZeroHeight(): bool /** * Set ZeroHeight. * - * @param bool $pValue - * * @return $this */ public function setZeroHeight(bool $pValue) diff --git a/src/PhpSpreadsheet/Worksheet/RowIterator.php b/src/PhpSpreadsheet/Worksheet/RowIterator.php index 44794c61cd..e0328bb439 100644 --- a/src/PhpSpreadsheet/Worksheet/RowIterator.php +++ b/src/PhpSpreadsheet/Worksheet/RowIterator.php @@ -133,8 +133,6 @@ public function current() /** * Return the current iterator key. - * - * @return int */ public function key(): int { @@ -159,8 +157,6 @@ public function prev(): void /** * Indicate if more rows exist in the worksheet range of rows that we're iterating. - * - * @return bool */ public function valid(): bool { diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php index bead7edacf..efee60bd59 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php @@ -57,9 +57,8 @@ public function providerSUBTOTAL() return require 'tests/data/Calculation/MathTrig/SUBTOTAL.php'; } - protected function rowVisibility() + protected function rowVisibility($data) { - $data = [1 => false, 2 => true, 3 => false, 4 => true, 5 => false, 6 => false, 7 => false, 8 => true, 9 => false, 10 => true, 11 => true, 12 => false]; foreach ($data as $row => $visibility) { yield $row => $visibility; } @@ -69,10 +68,11 @@ protected function rowVisibility() * @dataProvider providerHiddenSUBTOTAL * * @param mixed $expectedResult + * @param mixed $hiddenRows */ - public function testHiddenSUBTOTAL($expectedResult, ...$args): void + public function testHiddenSUBTOTAL($expectedResult, $hiddenRows, ...$args): void { - $visibilityGenerator = $this->rowVisibility(); + $visibilityGenerator = $this->rowVisibility($hiddenRows); $rowDimension = $this->getMockBuilder(RowDimension::class) ->setMethods(['getVisible']) diff --git a/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php b/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php index 001531f851..accaf03e15 100644 --- a/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php +++ b/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php @@ -15,59 +15,85 @@ 12 => ['A' => 89], ]; +$hiddenRows = [ + 1 => false, + 2 => true, + 3 => false, + 4 => true, + 5 => false, + 6 => false, + 7 => false, + 8 => true, + 9 => false, + 10 => true, + 11 => true, + 12 => false, +]; + return [ [ 21, + $hiddenRows, 101, $baseTestData, ], [ 5, + $hiddenRows, 102, $baseTestData, ], [ 5, + $hiddenRows, 103, $baseTestData, ], [ 55, + $hiddenRows, 104, $baseTestData, ], [ 1, + $hiddenRows, 105, $baseTestData, ], [ 48620, + $hiddenRows, 106, $baseTestData, ], [ 23.1840462387393, + $hiddenRows, 107, $baseTestData, ], [ 20.7364413533277, + $hiddenRows, 108, $baseTestData, ], [ 105, + $hiddenRows, 109, $baseTestData, ], [ 537.5, + $hiddenRows, 110, $baseTestData, ], [ 430, + $hiddenRows, 111, $baseTestData, ], From 0ab07173fdda9c16a91348bdc24fe144fe3a088d Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 17 Feb 2021 19:28:26 +0100 Subject: [PATCH 03/10] PHPCS Fixes --- src/PhpSpreadsheet/Style/Style.php | 8 ++++++-- .../Worksheet/ColumnCellIterator2Test.php | 8 +++++--- .../Worksheet/RowCellIterator2Test.php | 8 +++++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/PhpSpreadsheet/Style/Style.php b/src/PhpSpreadsheet/Style/Style.php index df53a16ca8..bb5ebc6ae7 100644 --- a/src/PhpSpreadsheet/Style/Style.php +++ b/src/PhpSpreadsheet/Style/Style.php @@ -359,7 +359,9 @@ public function applyFromArray(array $pStyles, $pAdvanced = true) $cellIterator = $columnIterator->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(true); foreach ($cellIterator as $columnCell) { - $columnCell->getStyle()->applyFromArray($pStyles); + if ($columnCell !== null) { + $columnCell->getStyle()->applyFromArray($pStyles); + } } } @@ -377,7 +379,9 @@ public function applyFromArray(array $pStyles, $pAdvanced = true) $cellIterator = $rowIterator->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(true); foreach ($cellIterator as $rowCell) { - $rowCell->getStyle()->applyFromArray($pStyles); + if ($rowCell !== null) { + $rowCell->getStyle()->applyFromArray($pStyles); + } } } diff --git a/tests/PhpSpreadsheetTests/Worksheet/ColumnCellIterator2Test.php b/tests/PhpSpreadsheetTests/Worksheet/ColumnCellIterator2Test.php index c542d89e3b..90280bd2a7 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/ColumnCellIterator2Test.php +++ b/tests/PhpSpreadsheetTests/Worksheet/ColumnCellIterator2Test.php @@ -25,9 +25,11 @@ public function testEndRange(?bool $existing, string $expectedResultFirst, strin $lastCoordinate = ''; $firstCoordinate = ''; foreach ($iterator as $cell) { - $lastCoordinate = $cell->getCoordinate(); - if (!$firstCoordinate) { - $firstCoordinate = $lastCoordinate; + if ($cell !== null) { + $lastCoordinate = $cell->getCoordinate(); + if (!$firstCoordinate) { + $firstCoordinate = $lastCoordinate; + } } } self::assertEquals($expectedResultFirst, $firstCoordinate); diff --git a/tests/PhpSpreadsheetTests/Worksheet/RowCellIterator2Test.php b/tests/PhpSpreadsheetTests/Worksheet/RowCellIterator2Test.php index 20d10da988..5218316891 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/RowCellIterator2Test.php +++ b/tests/PhpSpreadsheetTests/Worksheet/RowCellIterator2Test.php @@ -25,9 +25,11 @@ public function testEndRangeTrue(?bool $existing, string $expectedResultFirst, s $lastCoordinate = ''; $firstCoordinate = ''; foreach ($iterator as $cell) { - $lastCoordinate = $cell->getCoordinate(); - if (!$firstCoordinate) { - $firstCoordinate = $lastCoordinate; + if ($cell !== null) { + $lastCoordinate = $cell->getCoordinate(); + if (!$firstCoordinate) { + $firstCoordinate = $lastCoordinate; + } } } self::assertEquals($expectedResultFirst, $firstCoordinate); From ea198d0bbd59c35964563aeb2b187fd3c4d7d91b Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 17 Feb 2021 20:34:12 +0100 Subject: [PATCH 04/10] PHPCS Fixes --- src/PhpSpreadsheet/Reader/Slk.php | 6 +++--- src/PhpSpreadsheet/Worksheet/ColumnDimension.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/PhpSpreadsheet/Reader/Slk.php b/src/PhpSpreadsheet/Reader/Slk.php index e58ff2f6c5..89d80ffa86 100644 --- a/src/PhpSpreadsheet/Reader/Slk.php +++ b/src/PhpSpreadsheet/Reader/Slk.php @@ -419,14 +419,14 @@ private function addWidth(Spreadsheet $spreadsheet, string $columnWidth, string if ($columnWidth > '') { if ($startCol == $endCol) { $startCol = Coordinate::stringFromColumnIndex((int) $startCol); - $spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth); + $spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth((float) $columnWidth); } else { $startCol = Coordinate::stringFromColumnIndex($startCol); $endCol = Coordinate::stringFromColumnIndex($endCol); $spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth((float) $columnWidth); do { - $spreadsheet->getActiveSheet()->getColumnDimension(++$startCol)->setWidth($columnWidth); - } while ($startCol != $endCol); + $spreadsheet->getActiveSheet()->getColumnDimension(++$startCol)->setWidth((float) $columnWidth); + } while ($startCol !== $endCol); } } } diff --git a/src/PhpSpreadsheet/Worksheet/ColumnDimension.php b/src/PhpSpreadsheet/Worksheet/ColumnDimension.php index cb80cdee07..c29dd9bdc3 100644 --- a/src/PhpSpreadsheet/Worksheet/ColumnDimension.php +++ b/src/PhpSpreadsheet/Worksheet/ColumnDimension.php @@ -66,7 +66,7 @@ public function setColumnIndex(string $index) * * @return float */ - public function getWidth() + public function getWidth(): float { return $this->width; } @@ -78,7 +78,7 @@ public function getWidth() * * @return $this */ - public function setWidth($width) + public function setWidth(float $width) { $this->width = $width; From 8eb2e2b9f66053ec48656b45d0c738412b4f8e20 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 17 Feb 2021 20:45:41 +0100 Subject: [PATCH 05/10] PHPCS Fixes --- src/PhpSpreadsheet/Worksheet/ColumnDimension.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/PhpSpreadsheet/Worksheet/ColumnDimension.php b/src/PhpSpreadsheet/Worksheet/ColumnDimension.php index c29dd9bdc3..12b1efdf29 100644 --- a/src/PhpSpreadsheet/Worksheet/ColumnDimension.php +++ b/src/PhpSpreadsheet/Worksheet/ColumnDimension.php @@ -63,8 +63,6 @@ public function setColumnIndex(string $index) /** * Get Width. - * - * @return float */ public function getWidth(): float { @@ -74,8 +72,6 @@ public function getWidth(): float /** * Set Width. * - * @param float $width - * * @return $this */ public function setWidth(float $width) From 4b3209f1b8d02158b02b0acfe1a428d85ff2f51c Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 17 Feb 2021 22:33:06 +0100 Subject: [PATCH 06/10] Additional rector recommendations --- src/PhpSpreadsheet/Calculation/Database.php | 2 +- src/PhpSpreadsheet/Calculation/Financial.php | 12 ++++++------ src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php | 2 +- src/PhpSpreadsheet/Worksheet/ColumnIterator.php | 2 +- src/PhpSpreadsheet/Worksheet/RowCellIterator.php | 2 +- src/PhpSpreadsheet/Worksheet/RowIterator.php | 2 +- src/PhpSpreadsheet/Writer/Xls/Worksheet.php | 4 +--- 7 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/Database.php b/src/PhpSpreadsheet/Calculation/Database.php index 2ba4af2dc4..5250e30693 100644 --- a/src/PhpSpreadsheet/Calculation/Database.php +++ b/src/PhpSpreadsheet/Calculation/Database.php @@ -33,7 +33,7 @@ private static function fieldExtract($database, $field) } $key = array_search($field, $fieldNames); - return ($key) ? $key : null; + return $key ?: null; } /** diff --git a/src/PhpSpreadsheet/Calculation/Financial.php b/src/PhpSpreadsheet/Calculation/Financial.php index 3c24eb3b8f..f0b5ab05cc 100644 --- a/src/PhpSpreadsheet/Calculation/Financial.php +++ b/src/PhpSpreadsheet/Calculation/Financial.php @@ -2270,7 +2270,7 @@ public static function XIRR($values, $dates, $guess = 0.1) // create an initial range, with a root somewhere between 0 and guess $guess = Functions::flattenSingleValue($guess); $x1 = 0.0; - $x2 = $guess ? $guess : 0.1; + $x2 = $guess ?: 0.1; $f1 = self::xnpvOrdered($x1, $values, $dates, false); $f2 = self::xnpvOrdered($x2, $values, $dates, false); $found = false; @@ -2306,12 +2306,12 @@ public static function XIRR($values, $dates, $guess = 0.1) * * @param float $rate the discount rate to apply to the cash flows * @param float[] $values A series of cash flows that corresponds to a schedule of payments in dates. - * The first payment is optional and corresponds to a cost or payment that occurs at the beginning of the investment. - * If the first value is a cost or payment, it must be a negative value. All succeeding payments are discounted based on a 365-day year. - * The series of values must contain at least one positive value and one negative value. + * The first payment is optional and corresponds to a cost or payment that occurs at the beginning of the investment. + * If the first value is a cost or payment, it must be a negative value. All succeeding payments are discounted based on a 365-day year. + * The series of values must contain at least one positive value and one negative value. * @param mixed[] $dates A schedule of payment dates that corresponds to the cash flow payments. - * The first payment date indicates the beginning of the schedule of payments. - * All other dates must be later than this date, but they may occur in any order. + * The first payment date indicates the beginning of the schedule of payments. + * All other dates must be later than this date, but they may occur in any order. * * @return float|mixed|string */ diff --git a/src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php b/src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php index f406f8adc2..1390214e2c 100644 --- a/src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php +++ b/src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php @@ -78,7 +78,7 @@ public function resetStart(int $startRow = 1) */ public function resetEnd($endRow = null) { - $this->endRow = ($endRow) ? $endRow : $this->worksheet->getHighestRow(); + $this->endRow = $endRow ?: $this->worksheet->getHighestRow(); $this->adjustForExistingOnlyRange(); return $this; diff --git a/src/PhpSpreadsheet/Worksheet/ColumnIterator.php b/src/PhpSpreadsheet/Worksheet/ColumnIterator.php index c0200b69de..a7466881bf 100644 --- a/src/PhpSpreadsheet/Worksheet/ColumnIterator.php +++ b/src/PhpSpreadsheet/Worksheet/ColumnIterator.php @@ -94,7 +94,7 @@ public function resetStart(string $startColumn = 'A') */ public function resetEnd($endColumn = null) { - $endColumn = $endColumn ? $endColumn : $this->worksheet->getHighestColumn(); + $endColumn = $endColumn ?: $this->worksheet->getHighestColumn(); $this->endColumnIndex = Coordinate::columnIndexFromString($endColumn); return $this; diff --git a/src/PhpSpreadsheet/Worksheet/RowCellIterator.php b/src/PhpSpreadsheet/Worksheet/RowCellIterator.php index f5f97d956c..6a96a826a5 100644 --- a/src/PhpSpreadsheet/Worksheet/RowCellIterator.php +++ b/src/PhpSpreadsheet/Worksheet/RowCellIterator.php @@ -78,7 +78,7 @@ public function resetStart(string $startColumn = 'A') */ public function resetEnd($endColumn = null) { - $endColumn = $endColumn ? $endColumn : $this->worksheet->getHighestColumn(); + $endColumn = $endColumn ?: $this->worksheet->getHighestColumn(); $this->endColumnIndex = Coordinate::columnIndexFromString($endColumn); $this->adjustForExistingOnlyRange(); diff --git a/src/PhpSpreadsheet/Worksheet/RowIterator.php b/src/PhpSpreadsheet/Worksheet/RowIterator.php index e0328bb439..469c284ce6 100644 --- a/src/PhpSpreadsheet/Worksheet/RowIterator.php +++ b/src/PhpSpreadsheet/Worksheet/RowIterator.php @@ -91,7 +91,7 @@ public function resetStart(int $startRow = 1) */ public function resetEnd($endRow = null) { - $this->endRow = ($endRow) ? $endRow : $this->subject->getHighestRow(); + $this->endRow = $endRow ?: $this->subject->getHighestRow(); return $this; } diff --git a/src/PhpSpreadsheet/Writer/Xls/Worksheet.php b/src/PhpSpreadsheet/Writer/Xls/Worksheet.php index a793128ae0..d2784d6d06 100644 --- a/src/PhpSpreadsheet/Writer/Xls/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xls/Worksheet.php @@ -1715,9 +1715,7 @@ private function writeSetup(): void $length = 0x0022; // Number of bytes to follow $iPaperSize = $this->phpSheet->getPageSetup()->getPaperSize(); // Paper size - - $iScale = $this->phpSheet->getPageSetup()->getScale() ? - $this->phpSheet->getPageSetup()->getScale() : 100; // Print scaling factor + $iScale = $this->phpSheet->getPageSetup()->getScale() ?: 100; // Print scaling factor $iPageStart = 0x01; // Starting page number $iFitWidth = (int) $this->phpSheet->getPageSetup()->getFitToWidth(); // Fit to number of pages wide From 90551fc5fce6a5d870da3e5e06b5f56ad527c529 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 17 Feb 2021 22:54:43 +0100 Subject: [PATCH 07/10] Additional unit tests --- .../PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php | 9 +++++++++ tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php b/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php index de985ceecd..34f0ced814 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php @@ -81,4 +81,13 @@ public function testPrevOutOfRange(): void $iterator->prev(); self::assertFalse($iterator->valid()); } + + + public function testResetStartOutOfRange(): void + { + $this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class); + + $iterator = new ColumnIterator($this->mockWorksheet, 'B', 'D'); + $iterator->resetStart('H'); + } } diff --git a/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php b/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php index 919da9efad..71333c0115 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php @@ -79,4 +79,12 @@ public function testPrevOutOfRange(): void $iterator->prev(); self::assertFalse($iterator->valid()); } + + public function testResetStartOutOfRange(): void + { + $this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class); + + $iterator = new RowIterator($this->mockWorksheet, 2, 4); + $iterator->resetStart(10); + } } From c367174e47294973d6fdc389b37486e6497d55d9 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Thu, 18 Feb 2021 08:34:32 +0100 Subject: [PATCH 08/10] Additional unit tests and assertions --- .../Worksheet/ColumnIteratorTest.php | 16 ++++++++++++++++ .../Worksheet/RowIteratorTest.php | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php b/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php index 34f0ced814..5ccc94fe23 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php @@ -67,6 +67,22 @@ public function testIteratorSeekAndPrev(): void } } + public function testIteratorResetStart(): void + { + $iterator = new ColumnIterator($this->mockWorksheet, 'B', 'D'); + $iterator->resetStart('E'); + + $key = $iterator->key(); + self::assertSame('E', $key); + + $lastColumn = $iterator->key(); + while ($iterator->valid() !== false) { + $iterator->next(); + $lastColumn = $iterator->key(); + }; + self::assertSame('F', $lastColumn); + } + public function testSeekOutOfRange(): void { $this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class); diff --git a/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php b/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php index 71333c0115..49e8508b77 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php @@ -65,6 +65,22 @@ public function testIteratorSeekAndPrev(): void } } + public function testIteratorResetStart(): void + { + $iterator = new RowIterator($this->mockWorksheet, 2, 4); + $iterator->resetStart(5); + + $key = $iterator->key(); + self::assertSame(5, $key); + + $lastRow = $iterator->key(); + while ($iterator->valid() !== false) { + $iterator->next(); + $lastRow = $iterator->key(); + }; + self::assertSame(6, $lastRow); + } + public function testSeekOutOfRange(): void { $this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class); From 833fdcdb56eb825f8853838002046b342bec06fd Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Thu, 18 Feb 2021 08:54:07 +0100 Subject: [PATCH 09/10] phpcs fixes --- tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php | 2 +- tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php b/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php index 5ccc94fe23..80cf7ff970 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php @@ -79,7 +79,7 @@ public function testIteratorResetStart(): void while ($iterator->valid() !== false) { $iterator->next(); $lastColumn = $iterator->key(); - }; + } self::assertSame('F', $lastColumn); } diff --git a/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php b/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php index 49e8508b77..c527f43416 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php @@ -77,7 +77,7 @@ public function testIteratorResetStart(): void while ($iterator->valid() !== false) { $iterator->next(); $lastRow = $iterator->key(); - }; + } self::assertSame(6, $lastRow); } From 7b133d16f5fd41f381623102c4279e29b4d10c1f Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Thu, 18 Feb 2021 12:01:07 +0100 Subject: [PATCH 10/10] More rectorisations --- src/PhpSpreadsheet/Calculation/MathTrig.php | 4 ++-- src/PhpSpreadsheet/Chart/Axis.php | 12 ++++++------ src/PhpSpreadsheet/Chart/GridLines.php | 4 ++-- src/PhpSpreadsheet/Helper/Html.php | 2 +- src/PhpSpreadsheet/Reader/Gnumeric.php | 4 ++-- src/PhpSpreadsheet/Reader/Xls.php | 3 ++- src/PhpSpreadsheet/Reader/Xlsx.php | 2 +- src/PhpSpreadsheet/Reader/Xml.php | 4 ++-- src/PhpSpreadsheet/Style/Style.php | 4 ++-- .../Worksheet/ColumnIteratorTest.php | 1 - 10 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 0ba109f3f3..e62e949765 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -146,8 +146,8 @@ public static function ATAN2($xCoordinate = null, $yCoordinate = null) $xCoordinate = Functions::flattenSingleValue($xCoordinate); $yCoordinate = Functions::flattenSingleValue($yCoordinate); - $xCoordinate = ($xCoordinate !== null) ? $xCoordinate : 0.0; - $yCoordinate = ($yCoordinate !== null) ? $yCoordinate : 0.0; + $xCoordinate = $xCoordinate ?? 0.0; + $yCoordinate = $yCoordinate ?? 0.0; if ( ((is_numeric($xCoordinate)) || (is_bool($xCoordinate))) && diff --git a/src/PhpSpreadsheet/Chart/Axis.php b/src/PhpSpreadsheet/Chart/Axis.php index 7995c3b3fe..455a5faaa6 100644 --- a/src/PhpSpreadsheet/Chart/Axis.php +++ b/src/PhpSpreadsheet/Chart/Axis.php @@ -340,9 +340,9 @@ public function setShadowProperties($sh_presets, $sh_color_value = null, $sh_col { $this->setShadowPresetsProperties((int) $sh_presets) ->setShadowColor( - $sh_color_value === null ? $this->shadowProperties['color']['value'] : $sh_color_value, - $sh_color_alpha === null ? (int) $this->shadowProperties['color']['alpha'] : $sh_color_alpha, - $sh_color_type === null ? $this->shadowProperties['color']['type'] : $sh_color_type + $sh_color_value ?? $this->shadowProperties['color']['value'], + $sh_color_alpha ?? (int) $this->shadowProperties['color']['alpha'], + $sh_color_type ?? $this->shadowProperties['color']['type'] ) ->setShadowBlur($sh_blur) ->setShadowAngle($sh_angle) @@ -482,9 +482,9 @@ public function setGlowProperties($size, $color_value = null, $color_alpha = nul { $this->setGlowSize($size) ->setGlowColor( - $color_value === null ? $this->glowProperties['color']['value'] : $color_value, - $color_alpha === null ? (int) $this->glowProperties['color']['alpha'] : $color_alpha, - $color_type === null ? $this->glowProperties['color']['type'] : $color_type + $color_value ?? $this->glowProperties['color']['value'], + $color_alpha ?? (int) $this->glowProperties['color']['alpha'], + $color_type ?? $this->glowProperties['color']['type'] ); } diff --git a/src/PhpSpreadsheet/Chart/GridLines.php b/src/PhpSpreadsheet/Chart/GridLines.php index 2e424bc246..385b278bfc 100644 --- a/src/PhpSpreadsheet/Chart/GridLines.php +++ b/src/PhpSpreadsheet/Chart/GridLines.php @@ -291,9 +291,9 @@ public function setShadowProperties($sh_presets, $sh_color_value = null, $sh_col $this->activateObject() ->setShadowPresetsProperties((int) $sh_presets) ->setShadowColor( - $sh_color_value === null ? $this->shadowProperties['color']['value'] : $sh_color_value, + $sh_color_value ?? $this->shadowProperties['color']['value'], $sh_color_alpha === null ? (int) $this->shadowProperties['color']['alpha'] : $this->getTrueAlpha($sh_color_alpha), - $sh_color_type === null ? $this->shadowProperties['color']['type'] : $sh_color_type + $sh_color_type ?? $this->shadowProperties['color']['type'] ) ->setShadowBlur($sh_blur) ->setShadowAngle($sh_angle) diff --git a/src/PhpSpreadsheet/Helper/Html.php b/src/PhpSpreadsheet/Helper/Html.php index 6c4cbf9bbb..f07bc961c5 100644 --- a/src/PhpSpreadsheet/Helper/Html.php +++ b/src/PhpSpreadsheet/Helper/Html.php @@ -711,7 +711,7 @@ protected function startFontTag($tag): void } elseif (strpos(trim($attributeValue), '#') === 0) { $this->$attributeName = ltrim($attributeValue, '#'); } else { - $this->$attributeName = $this->colourNameLookup($attributeValue); + $this->$attributeName = static::colourNameLookup($attributeValue); } } else { $this->$attributeName = $attributeValue; diff --git a/src/PhpSpreadsheet/Reader/Gnumeric.php b/src/PhpSpreadsheet/Reader/Gnumeric.php index dc921c1e44..2bec2a13e3 100644 --- a/src/PhpSpreadsheet/Reader/Gnumeric.php +++ b/src/PhpSpreadsheet/Reader/Gnumeric.php @@ -657,7 +657,7 @@ private function processColumnLoop(int $c, int $maxCol, SimpleXMLElement $column $column = $columnAttributes['No']; $columnWidth = ((float) $columnAttributes['Unit']) / 5.4; $hidden = (isset($columnAttributes['Hidden'])) && ((string) $columnAttributes['Hidden'] == '1'); - $columnCount = (isset($columnAttributes['Count'])) ? $columnAttributes['Count'] : 1; + $columnCount = $columnAttributes['Count'] ?? 1; while ($c < $column) { $this->spreadsheet->getActiveSheet()->getColumnDimension(Coordinate::stringFromColumnIndex($c + 1))->setWidth($defaultWidth); ++$c; @@ -696,7 +696,7 @@ private function processRowLoop(int $r, int $maxRow, SimpleXMLElement $rowOverri $row = $rowAttributes['No']; $rowHeight = (float) $rowAttributes['Unit']; $hidden = (isset($rowAttributes['Hidden'])) && ((string) $rowAttributes['Hidden'] == '1'); - $rowCount = (isset($rowAttributes['Count'])) ? $rowAttributes['Count'] : 1; + $rowCount = $rowAttributes['Count'] ?? 1; while ($r < $row) { ++$r; $this->spreadsheet->getActiveSheet()->getRowDimension($r)->setRowHeight($defaultHeight); diff --git a/src/PhpSpreadsheet/Reader/Xls.php b/src/PhpSpreadsheet/Reader/Xls.php index 5656a0ff7b..6d6b87fdc8 100644 --- a/src/PhpSpreadsheet/Reader/Xls.php +++ b/src/PhpSpreadsheet/Reader/Xls.php @@ -1703,7 +1703,8 @@ private function readNote(): void // max 2048 bytes will probably throw a wobbly. $row = self::getUInt2d($recordData, 0); $extension = true; - $cellAddress = array_pop(array_keys($this->phpSheet->getComments())); + $arrayKeys = array_keys($this->phpSheet->getComments()); + $cellAddress = array_pop($arrayKeys); } $cellAddress = str_replace('$', '', $cellAddress); diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index 2b37896d56..bf7545916f 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -516,7 +516,7 @@ public function load($pFilename) } $style = (object) [ - 'numFmt' => $numFmt === null ? NumberFormat::FORMAT_GENERAL : $numFmt, + 'numFmt' => $numFmt ?? NumberFormat::FORMAT_GENERAL, 'font' => $xmlStyles->fonts->font[(int) ($xf['fontId'])], 'fill' => $xmlStyles->fills->fill[(int) ($xf['fillId'])], 'border' => $xmlStyles->borders->border[(int) ($xf['borderId'])], diff --git a/src/PhpSpreadsheet/Reader/Xml.php b/src/PhpSpreadsheet/Reader/Xml.php index 9de882337f..f38a951562 100644 --- a/src/PhpSpreadsheet/Reader/Xml.php +++ b/src/PhpSpreadsheet/Reader/Xml.php @@ -421,7 +421,7 @@ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet) $xml_ss = $xml->children($namespaces['ss']); foreach ($xml_ss->Worksheet as $worksheetx) { - $worksheet = ($worksheetx === null) ? new SimpleXMLElement('') : $worksheetx; + $worksheet = $worksheetx ?? new SimpleXMLElement(''); $worksheet_ss = self::getAttributes($worksheet, $namespaces['ss']); if ( @@ -665,7 +665,7 @@ private function parseStyles(SimpleXMLElement $xml, array $namespaces): void foreach ($xml->Styles[0] as $style) { $style_ss = self::getAttributes($style, $namespaces['ss']); $styleID = (string) $style_ss['ID']; - $this->styles[$styleID] = (isset($this->styles['Default'])) ? $this->styles['Default'] : []; + $this->styles[$styleID] = $this->styles['Default'] ?? []; foreach ($style as $styleType => $styleDatax) { $styleData = $styleDatax ?? new SimpleXMLElement(''); $styleAttributes = $styleData->attributes($namespaces['ss']); diff --git a/src/PhpSpreadsheet/Style/Style.php b/src/PhpSpreadsheet/Style/Style.php index bb5ebc6ae7..7fec9a00f6 100644 --- a/src/PhpSpreadsheet/Style/Style.php +++ b/src/PhpSpreadsheet/Style/Style.php @@ -427,8 +427,8 @@ public function applyFromArray(array $pStyles, $pAdvanced = true) case 'ROW': for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) { $rowDimension = $this->getActiveSheet()->getRowDimension($row); - $oldXfIndex = $rowDimension->getXfIndex() === null ? - 0 : $rowDimension->getXfIndex(); // row without explicit style should be formatted based on default style + // row without explicit style should be formatted based on default style + $oldXfIndex = $rowDimension->getXfIndex() ?? 0; $rowDimension->setXfIndex($newXfIndexes[$oldXfIndex]); } diff --git a/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php b/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php index 80cf7ff970..71a05d4846 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php @@ -98,7 +98,6 @@ public function testPrevOutOfRange(): void self::assertFalse($iterator->valid()); } - public function testResetStartOutOfRange(): void { $this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);