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()`.
This commit is contained in:
Adrien Crivelli 2021-04-25 15:31:56 +09:00
parent 1e8ff9f852
commit 2e39ece373
No known key found for this signature in database
GPG Key ID: 16D79B903B4B5874
2 changed files with 7 additions and 31 deletions

View File

@ -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

View File

@ -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);
}
/**