Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stronger typing for ranges #3742

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@ parameters:
message: "#^Binary operation \"/\" between float and array\\|float\\|int\\|string results in an error\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/MathTrig/Combinations.php

-
message: "#^Offset 2 does not exist on array\\{int, int, int, int\\}\\|array\\{int, int\\}\\.$#"
count: 1
path: src/PhpSpreadsheet/Worksheet/Validations.php

-
message: "#^Offset 3 does not exist on array\\{int, int, int, int\\}\\|array\\{int, int\\}\\.$#"
count: 1
path: src/PhpSpreadsheet/Worksheet/Validations.php
4 changes: 4 additions & 0 deletions src/PhpSpreadsheet/Cell/CellAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public function __destruct()
$this->worksheet = null;
}

/**
* @phpstan-assert int|numeric-string $columnId
* @phpstan-assert int|numeric-string $rowId
*/
private static function validateColumnAndRow(mixed $columnId, mixed $rowId): void
{
if (!is_numeric($columnId) || $columnId <= 0 || !is_numeric($rowId) || $rowId <= 0) {
Expand Down
6 changes: 2 additions & 4 deletions src/PhpSpreadsheet/Cell/Coordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,9 @@ public static function columnIndexFromString(?string $columnAddress): int
/**
* String from column index.
*
* @param int $columnIndex Column index (A = 1)
*
* @return string
* @param int|numeric-string $columnIndex Column index (A = 1)
*/
public static function stringFromColumnIndex($columnIndex)
public static function stringFromColumnIndex(int|string $columnIndex): string
{
static $indexCache = [];
static $lookupCache = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ';
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Worksheet/AutoFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function setEvaluated(bool $value): void
/**
* Create a new AutoFilter.
*
* @param AddressRange|array<int>|string $range
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range
* A simple string containing a Cell range like 'A1:E10' is permitted
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or an AddressRange object.
Expand Down Expand Up @@ -105,7 +105,7 @@ public function getRange()
/**
* Set AutoFilter Cell Range.
*
* @param AddressRange|array<int>|string $range
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range
* A simple string containing a Cell range like 'A1:E10' or a Cell address like 'A1' is permitted
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or an AddressRange object.
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Worksheet/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Table implements Stringable
/**
* Create a new Table.
*
* @param AddressRange|array<int>|string $range
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range
* A simple string containing a Cell range like 'A1:E10' is permitted
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or an AddressRange object.
Expand Down Expand Up @@ -273,7 +273,7 @@ public function getRange(): string
/**
* Set Table Cell Range.
*
* @param AddressRange|array<int>|string $range
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range
* A simple string containing a Cell range like 'A1:E10' is permitted
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or an AddressRange object.
Expand Down
6 changes: 3 additions & 3 deletions src/PhpSpreadsheet/Worksheet/Validations.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function validateCellAddress($cellAddress): string
/**
* Validate a cell address or cell range.
*
* @param AddressRange|array<int>|CellAddress|int|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
* or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
* or as a CellAddress or AddressRange object.
*/
Expand All @@ -59,11 +59,11 @@ public static function validateCellOrCellRange($cellRange): string
/**
* Validate a cell range.
*
* @param AddressRange|array<int>|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
* or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
* or as an AddressRange object.
*/
public static function validateCellRange($cellRange): string
public static function validateCellRange(AddressRange|array|string $cellRange): string
{
if (is_string($cellRange)) {
[$worksheet, $addressRange] = Worksheet::extractSheetTitle($cellRange, true);
Expand Down
14 changes: 7 additions & 7 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ public function getStyles()
/**
* Get style for cell.
*
* @param AddressRange|array<int>|CellAddress|int|string $cellCoordinate
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $cellCoordinate
* A simple string containing a cell address like 'A1' or a cell range like 'A1:E10'
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or a CellAddress or AddressRange object.
Expand Down Expand Up @@ -1873,7 +1873,7 @@ public function getColumnBreaks()
/**
* Set merge on a cell range.
*
* @param AddressRange|array<int>|string $range A simple string containing a Cell range like 'A1:E10'
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range A simple string containing a Cell range like 'A1:E10'
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or an AddressRange.
* @param string $behaviour How the merged cells should behave.
Expand Down Expand Up @@ -2029,7 +2029,7 @@ public function mergeCellsByColumnAndRow($columnIndex1, $row1, $columnIndex2, $r
/**
* Remove merge on a cell range.
*
* @param AddressRange|array<int>|string $range A simple string containing a Cell range like 'A1:E10'
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range A simple string containing a Cell range like 'A1:E10'
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or an AddressRange.
*
Expand Down Expand Up @@ -2106,7 +2106,7 @@ public function setMergeCells(array $mergeCells): static
/**
* Set protection on a cell or cell range.
*
* @param AddressRange|array<int>|CellAddress|int|string $range A simple string containing a Cell range like 'A1:E10'
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $range A simple string containing a Cell range like 'A1:E10'
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or a CellAddress or AddressRange object.
* @param string $password Password to unlock the protection
Expand Down Expand Up @@ -2157,7 +2157,7 @@ public function protectCellsByColumnAndRow($columnIndex1, $row1, $columnIndex2,
/**
* Remove protection on a cell or cell range.
*
* @param AddressRange|array<int>|CellAddress|int|string $range A simple string containing a Cell range like 'A1:E10'
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $range A simple string containing a Cell range like 'A1:E10'
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or a CellAddress or AddressRange object.
*
Expand Down Expand Up @@ -2225,7 +2225,7 @@ public function getAutoFilter()
/**
* Set AutoFilter.
*
* @param AddressRange|array<int>|AutoFilter|string $autoFilterOrRange
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|AutoFilter|string $autoFilterOrRange
* A simple string containing a Cell range like 'A1:E10' is permitted for backward compatibility
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or an AddressRange.
Expand Down Expand Up @@ -3041,7 +3041,7 @@ public function setSelectedCell($coordinate): static
/**
* Select a range of cells.
*
* @param AddressRange|array<int>|CellAddress|int|string $coordinate A simple string containing a Cell range like 'A1:E10'
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $coordinate A simple string containing a Cell range like 'A1:E10'
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or a CellAddress or AddressRange object.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function testGetRange(): void
}

/**
* @param AddressRange|array<int>|string $fullRange
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $fullRange
*/
public function xtestSetRangeValidRange(string|array|AddressRange $fullRange, string $actualRange): void
{
Expand Down