diff --git a/src/PhpSpreadsheet/Calculation/Database.php b/src/PhpSpreadsheet/Calculation/Database.php index 2ba4af2d..5250e306 100644 --- a/src/PhpSpreadsheet/Calculation/Database.php +++ b/src/PhpSpreadsheet/Calculation/Database.php @@ -33,7 +33,7 @@ class Database } $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 3c24eb3b..f0b5ab05 100644 --- a/src/PhpSpreadsheet/Calculation/Financial.php +++ b/src/PhpSpreadsheet/Calculation/Financial.php @@ -2270,7 +2270,7 @@ class Financial // 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 @@ class Financial * * @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/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 0ba109f3..e62e9497 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -146,8 +146,8 @@ class MathTrig $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 7995c3b3..455a5faa 100644 --- a/src/PhpSpreadsheet/Chart/Axis.php +++ b/src/PhpSpreadsheet/Chart/Axis.php @@ -340,9 +340,9 @@ class Axis extends Properties { $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 @@ class Axis extends Properties { $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 2e424bc2..385b278b 100644 --- a/src/PhpSpreadsheet/Chart/GridLines.php +++ b/src/PhpSpreadsheet/Chart/GridLines.php @@ -291,9 +291,9 @@ class GridLines extends Properties $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 6c4cbf9b..f07bc961 100644 --- a/src/PhpSpreadsheet/Helper/Html.php +++ b/src/PhpSpreadsheet/Helper/Html.php @@ -711,7 +711,7 @@ class Html } 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 dc921c1e..2bec2a13 100644 --- a/src/PhpSpreadsheet/Reader/Gnumeric.php +++ b/src/PhpSpreadsheet/Reader/Gnumeric.php @@ -657,7 +657,7 @@ class Gnumeric extends BaseReader $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 @@ class Gnumeric extends BaseReader $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/Slk.php b/src/PhpSpreadsheet/Reader/Slk.php index e58ff2f6..89d80ffa 100644 --- a/src/PhpSpreadsheet/Reader/Slk.php +++ b/src/PhpSpreadsheet/Reader/Slk.php @@ -419,14 +419,14 @@ class Slk extends BaseReader 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/Reader/Xls.php b/src/PhpSpreadsheet/Reader/Xls.php index 5656a0ff..6d6b87fd 100644 --- a/src/PhpSpreadsheet/Reader/Xls.php +++ b/src/PhpSpreadsheet/Reader/Xls.php @@ -1703,7 +1703,8 @@ class Xls extends BaseReader // 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 2b37896d..bf754591 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -516,7 +516,7 @@ class Xlsx extends BaseReader } $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 9de88233..f38a9515 100644 --- a/src/PhpSpreadsheet/Reader/Xml.php +++ b/src/PhpSpreadsheet/Reader/Xml.php @@ -421,7 +421,7 @@ class Xml extends BaseReader $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 @@ class Xml extends BaseReader 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 f7c1be23..7fec9a00 100644 --- a/src/PhpSpreadsheet/Style/Style.php +++ b/src/PhpSpreadsheet/Style/Style.php @@ -359,7 +359,9 @@ class Style extends Supervisor $cellIterator = $columnIterator->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(true); foreach ($cellIterator as $columnCell) { - $columnCell->getStyle()->applyFromArray($pStyles); + if ($columnCell !== null) { + $columnCell->getStyle()->applyFromArray($pStyles); + } } } @@ -367,7 +369,7 @@ class Style extends Supervisor 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; @@ -377,7 +379,9 @@ class Style extends Supervisor $cellIterator = $rowIterator->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(true); foreach ($cellIterator as $rowCell) { - $rowCell->getStyle()->applyFromArray($pStyles); + if ($rowCell !== null) { + $rowCell->getStyle()->applyFromArray($pStyles); + } } } @@ -423,8 +427,8 @@ class Style extends Supervisor 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/src/PhpSpreadsheet/Worksheet/CellIterator.php b/src/PhpSpreadsheet/Worksheet/CellIterator.php index 45f76cab..e0f35b64 100644 --- a/src/PhpSpreadsheet/Worksheet/CellIterator.php +++ b/src/PhpSpreadsheet/Worksheet/CellIterator.php @@ -30,10 +30,8 @@ abstract class CellIterator implements Iterator /** * Get loop only existing cells. - * - * @return bool */ - public function getIterateOnlyExistingCells() + public function getIterateOnlyExistingCells(): bool { return $this->onlyExistingCells; } @@ -45,10 +43,8 @@ abstract class CellIterator implements Iterator /** * Set the iterator to loop only existing cells. - * - * @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 410e8073..763806a0 100644 --- a/src/PhpSpreadsheet/Worksheet/Column.php +++ b/src/PhpSpreadsheet/Worksheet/Column.php @@ -41,10 +41,8 @@ class Column /** * Get column index as string eg: 'A'. - * - * @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 714ee7ce..1390214e 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 @@ class ColumnCellIterator extends CellIterator * * @return $this */ - public function resetStart($startRow = 1) + public function resetStart(int $startRow = 1) { $this->startRow = $startRow; $this->adjustForExistingOnlyRange(); @@ -77,7 +78,7 @@ class ColumnCellIterator extends CellIterator */ public function resetEnd($endRow = null) { - $this->endRow = ($endRow) ? $endRow : $this->worksheet->getHighestRow(); + $this->endRow = $endRow ?: $this->worksheet->getHighestRow(); $this->adjustForExistingOnlyRange(); return $this; @@ -90,7 +91,7 @@ class ColumnCellIterator extends CellIterator * * @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'); @@ -113,20 +114,16 @@ class ColumnCellIterator extends CellIterator /** * Return the current cell in this worksheet column. - * - * @return \PhpOffice\PhpSpreadsheet\Cell\Cell */ - public function current() + public function current(): ?Cell { return $this->worksheet->getCellByColumnAndRow($this->columnIndex, $this->currentRow); } /** * Return the current iterator key. - * - * @return int */ - public function key() + public function key(): int { return $this->currentRow; } @@ -161,10 +158,8 @@ class ColumnCellIterator extends CellIterator /** * Indicate if more rows exist in the worksheet range of rows that we're iterating. - * - * @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 4e87a344..12b1efdf 100644 --- a/src/PhpSpreadsheet/Worksheet/ColumnDimension.php +++ b/src/PhpSpreadsheet/Worksheet/ColumnDimension.php @@ -43,10 +43,8 @@ class ColumnDimension extends Dimension /** * Get column index as string eg: 'A'. - * - * @return string */ - public function getColumnIndex() + public function getColumnIndex(): string { return $this->columnIndex; } @@ -54,23 +52,19 @@ class ColumnDimension extends Dimension /** * Set column index as string eg: 'A'. * - * @param string $pValue - * * @return $this */ - public function setColumnIndex($pValue) + public function setColumnIndex(string $index) { - $this->columnIndex = $pValue; + $this->columnIndex = $index; return $this; } /** * Get Width. - * - * @return float */ - public function getWidth() + public function getWidth(): float { return $this->width; } @@ -78,23 +72,19 @@ class ColumnDimension extends Dimension /** * Set Width. * - * @param float $pValue - * * @return $this */ - public function setWidth($pValue) + public function setWidth(float $width) { - $this->width = $pValue; + $this->width = $width; return $this; } /** * Get Auto Size. - * - * @return bool */ - public function getAutoSize() + public function getAutoSize(): bool { return $this->autoSize; } @@ -102,13 +92,11 @@ class ColumnDimension extends Dimension /** * Set Auto Size. * - * @param bool $pValue - * * @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 d0bb20cc..a7466881 100644 --- a/src/PhpSpreadsheet/Worksheet/ColumnIterator.php +++ b/src/PhpSpreadsheet/Worksheet/ColumnIterator.php @@ -67,11 +67,13 @@ class ColumnIterator implements Iterator * * @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; @@ -92,7 +94,7 @@ class ColumnIterator implements Iterator */ public function resetEnd($endColumn = null) { - $endColumn = $endColumn ? $endColumn : $this->worksheet->getHighestColumn(); + $endColumn = $endColumn ?: $this->worksheet->getHighestColumn(); $this->endColumnIndex = Coordinate::columnIndexFromString($endColumn); return $this; @@ -105,11 +107,13 @@ class ColumnIterator implements Iterator * * @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; @@ -136,10 +140,8 @@ class ColumnIterator implements Iterator /** * Return the current iterator key. - * - * @return string */ - public function key() + public function key(): string { return Coordinate::stringFromColumnIndex($this->currentColumnIndex); } @@ -162,10 +164,8 @@ class ColumnIterator implements Iterator /** * Indicate if more columns exist in the worksheet range of columns that we're iterating. - * - * @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 a27daf09..4b3a0da8 100644 --- a/src/PhpSpreadsheet/Worksheet/Dimension.php +++ b/src/PhpSpreadsheet/Worksheet/Dimension.php @@ -47,10 +47,8 @@ abstract class Dimension /** * Get Visible. - * - * @return bool */ - public function getVisible() + public function getVisible(): bool { return $this->visible; } @@ -58,23 +56,19 @@ abstract class Dimension /** * Set Visible. * - * @param bool $pValue - * * @return $this */ - public function setVisible($pValue) + public function setVisible(bool $visible) { - $this->visible = (bool) $pValue; + $this->visible = $visible; return $this; } /** * Get Outline Level. - * - * @return int */ - public function getOutlineLevel() + public function getOutlineLevel(): int { return $this->outlineLevel; } @@ -83,27 +77,23 @@ abstract class Dimension * Set Outline Level. * Value must be between 0 and 7. * - * @param int $pValue - * * @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; } /** * Get Collapsed. - * - * @return bool */ - public function getCollapsed() + public function getCollapsed(): bool { return $this->collapsed; } @@ -111,13 +101,11 @@ abstract class Dimension /** * Set Collapsed. * - * @param bool $pValue - * * @return $this */ - public function setCollapsed($pValue) + public function setCollapsed(bool $collapsed) { - $this->collapsed = (bool) $pValue; + $this->collapsed = $collapsed; return $this; } @@ -127,7 +115,7 @@ abstract class Dimension * * @return int */ - public function getXfIndex() + public function getXfIndex(): ?int { return $this->xfIndex; } @@ -135,11 +123,9 @@ abstract class Dimension /** * Set index to cellXf. * - * @param int $pValue - * * @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 6cfed37a..93b0bb04 100644 --- a/src/PhpSpreadsheet/Worksheet/Iterator.php +++ b/src/PhpSpreadsheet/Worksheet/Iterator.php @@ -47,20 +47,16 @@ class Iterator implements \Iterator /** * Current Worksheet. - * - * @return Worksheet */ - public function current() + public function current(): Worksheet { return $this->subject->getSheet($this->position); } /** * Current key. - * - * @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 4f48a346..cf935743 100644 --- a/src/PhpSpreadsheet/Worksheet/Row.php +++ b/src/PhpSpreadsheet/Worksheet/Row.php @@ -41,10 +41,8 @@ class Row /** * Get row index. - * - * @return int */ - public function getRowIndex() + public function getRowIndex(): int { return $this->rowIndex; } @@ -64,10 +62,8 @@ class Row /** * Returns bound worksheet. - * - * @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 9b9d54eb..6a96a826 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 @@ class RowCellIterator extends CellIterator * * @return $this */ - public function resetStart($startColumn = 'A') + public function resetStart(string $startColumn = 'A') { $this->startColumnIndex = Coordinate::columnIndexFromString($startColumn); $this->adjustForExistingOnlyRange(); @@ -77,7 +78,7 @@ class RowCellIterator extends CellIterator */ public function resetEnd($endColumn = null) { - $endColumn = $endColumn ? $endColumn : $this->worksheet->getHighestColumn(); + $endColumn = $endColumn ?: $this->worksheet->getHighestColumn(); $this->endColumnIndex = Coordinate::columnIndexFromString($endColumn); $this->adjustForExistingOnlyRange(); @@ -91,7 +92,7 @@ class RowCellIterator extends CellIterator * * @return $this */ - public function seek($column = 'A') + public function seek(string $column = 'A') { $columnx = $column; $column = Coordinate::columnIndexFromString($column); @@ -116,20 +117,16 @@ class RowCellIterator extends CellIterator /** * Return the current cell in this worksheet row. - * - * @return \PhpOffice\PhpSpreadsheet\Cell\Cell */ - public function current() + public function current(): ?Cell { return $this->worksheet->getCellByColumnAndRow($this->currentColumnIndex, $this->rowIndex); } /** * Return the current iterator key. - * - * @return string */ - public function key() + public function key(): string { return Coordinate::stringFromColumnIndex($this->currentColumnIndex); } @@ -166,10 +163,8 @@ class RowCellIterator extends CellIterator /** * Return the current iterator position. - * - * @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 c4a87bdb..d86dd80f 100644 --- a/src/PhpSpreadsheet/Worksheet/RowDimension.php +++ b/src/PhpSpreadsheet/Worksheet/RowDimension.php @@ -43,10 +43,8 @@ class RowDimension extends Dimension /** * Get Row Index. - * - * @return int */ - public function getRowIndex() + public function getRowIndex(): int { return $this->rowIndex; } @@ -54,13 +52,11 @@ class RowDimension extends Dimension /** * Set Row Index. * - * @param int $pValue - * * @return $this */ - public function setRowIndex($pValue) + public function setRowIndex(int $index) { - $this->rowIndex = $pValue; + $this->rowIndex = $index; return $this; } @@ -78,23 +74,21 @@ class RowDimension extends Dimension /** * 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; } /** * Get ZeroHeight. - * - * @return bool */ - public function getZeroHeight() + public function getZeroHeight(): bool { return $this->zeroHeight; } @@ -102,11 +96,9 @@ class RowDimension extends Dimension /** * Set ZeroHeight. * - * @param bool $pValue - * * @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 42542533..469c284c 100644 --- a/src/PhpSpreadsheet/Worksheet/RowIterator.php +++ b/src/PhpSpreadsheet/Worksheet/RowIterator.php @@ -65,10 +65,12 @@ class RowIterator implements Iterator * * @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; @@ -89,7 +91,7 @@ class RowIterator implements Iterator */ public function resetEnd($endRow = null) { - $this->endRow = ($endRow) ? $endRow : $this->subject->getHighestRow(); + $this->endRow = $endRow ?: $this->subject->getHighestRow(); return $this; } @@ -101,7 +103,7 @@ class RowIterator implements Iterator * * @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})"); @@ -131,10 +133,8 @@ class RowIterator implements Iterator /** * Return the current iterator key. - * - * @return int */ - public function key() + public function key(): int { return $this->position; } @@ -157,10 +157,8 @@ class RowIterator implements Iterator /** * Indicate if more rows exist in the worksheet range of rows that we're iterating. - * - * @return bool */ - public function valid() + public function valid(): bool { return $this->position <= $this->endRow && $this->position >= $this->startRow; } diff --git a/src/PhpSpreadsheet/Writer/Xls/Worksheet.php b/src/PhpSpreadsheet/Writer/Xls/Worksheet.php index a793128a..d2784d6d 100644 --- a/src/PhpSpreadsheet/Writer/Xls/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xls/Worksheet.php @@ -1715,9 +1715,7 @@ class Worksheet extends BIFFwriter $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 diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php index 14865673..efee60bd 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php @@ -57,11 +57,10 @@ class SubTotalTest extends TestCase 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]; - foreach ($data as $k => $v) { - yield $k => $v; + foreach ($data as $row => $visibility) { + yield $row => $visibility; } } @@ -69,10 +68,11 @@ class SubTotalTest extends TestCase * @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/PhpSpreadsheetTests/Worksheet/ColumnCellIterator2Test.php b/tests/PhpSpreadsheetTests/Worksheet/ColumnCellIterator2Test.php index c542d89e..90280bd2 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/ColumnCellIterator2Test.php +++ b/tests/PhpSpreadsheetTests/Worksheet/ColumnCellIterator2Test.php @@ -25,9 +25,11 @@ class ColumnCellIterator2Test extends TestCase $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/ColumnIteratorTest.php b/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php index de985cee..71a05d48 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/ColumnIteratorTest.php @@ -67,6 +67,22 @@ class ColumnIteratorTest extends TestCase } } + 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); @@ -81,4 +97,12 @@ class ColumnIteratorTest extends TestCase $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/RowCellIterator2Test.php b/tests/PhpSpreadsheetTests/Worksheet/RowCellIterator2Test.php index 20d10da9..52183168 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/RowCellIterator2Test.php +++ b/tests/PhpSpreadsheetTests/Worksheet/RowCellIterator2Test.php @@ -25,9 +25,11 @@ class RowCellIterator2Test extends TestCase $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/RowIteratorTest.php b/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php index 919da9ef..c527f434 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php @@ -65,6 +65,22 @@ class RowIteratorTest extends TestCase } } + 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); @@ -79,4 +95,12 @@ class RowIteratorTest extends TestCase $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); + } } diff --git a/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php b/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php index 001531f8..accaf03e 100644 --- a/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php +++ b/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php @@ -15,59 +15,85 @@ $baseTestData = [ 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, ],