From 2e39ece3739fad95a0fda0dbf9dfc6c757a585bb Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Sun, 25 Apr 2021 15:31:56 +0900 Subject: [PATCH] BREAKING `Worksheet::getCellByColumnAndRow()` cannot return null anymore `Worksheet::getCellByColumnAndRow()` used to optionnaly return null if passed a second argument. This second argument was removed entirely and the method always returns a Cell (possibly creating it if needed). This make the API more predictable and easier to do static analysis with tools such as PHPStan. If you relied on that second parameter, you should instead use the `Worksheet::cellExistsByColumnAndRow()` before calling `getCellByColumnAndRow()`. --- phpstan-baseline.neon | 25 ---------------------- src/PhpSpreadsheet/Worksheet/Worksheet.php | 13 +++++------ 2 files changed, 7 insertions(+), 31 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 84fb7fd8..a606d636 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -4515,11 +4515,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/ReferenceHelper.php - - - message: "#^Cannot call method setXfIndex\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Cell\\|null\\.$#" - count: 1 - path: src/PhpSpreadsheet/ReferenceHelper.php - - message: "#^Parameter \\#1 \\$columnIndex of static method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Coordinate\\:\\:stringFromColumnIndex\\(\\) expects int, float\\|int given\\.$#" count: 1 @@ -5955,16 +5950,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Style/Style.php - - - message: "#^Cannot call method getXfIndex\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Cell\\|null\\.$#" - count: 2 - path: src/PhpSpreadsheet/Style/Style.php - - - - message: "#^Cannot call method setXfIndex\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Cell\\|null\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Style.php - - message: "#^Result of && is always true\\.$#" count: 1 @@ -6355,16 +6340,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Worksheet/Worksheet.php - - - message: "#^Cannot call method setValue\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Cell\\|null\\.$#" - count: 1 - path: src/PhpSpreadsheet/Worksheet/Worksheet.php - - - - message: "#^Cannot call method setValueExplicit\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Cell\\|null\\.$#" - count: 1 - path: src/PhpSpreadsheet/Worksheet/Worksheet.php - - message: "#^Parameter \\#2 \\$start of function substr expects int, int\\<0, max\\>\\|false given\\.$#" count: 2 diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index 9e1f0a19..63166980 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -1263,22 +1263,23 @@ class Worksheet implements IComparable * * @param int $columnIndex Numeric column coordinate of the cell * @param int $row Numeric row coordinate of the cell - * @param bool $createIfNotExists Flag indicating whether a new cell should be created if it doesn't - * already exist, or a null should be returned instead * - * @return null|Cell Cell that was found/created or null + * @return Cell Cell that was found/created or null */ - public function getCellByColumnAndRow($columnIndex, $row, $createIfNotExists = true) + public function getCellByColumnAndRow($columnIndex, $row): Cell { $columnLetter = Coordinate::stringFromColumnIndex($columnIndex); $coordinate = $columnLetter . $row; if ($this->cellCollection->has($coordinate)) { - return $this->cellCollection->get($coordinate); + /** @var Cell $cell */ + $cell = $this->cellCollection->get($coordinate); + + return $cell; } // Create new cell object, if required - return $createIfNotExists ? $this->createNewCell($coordinate) : null; + return $this->createNewCell($coordinate); } /**