Skip to content

Commit 23b95e2

Browse files
committed
Stronger typing for ranges
1 parent b2d572c commit 23b95e2

File tree

8 files changed

+31
-19
lines changed

8 files changed

+31
-19
lines changed

phpstan-baseline.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,13 @@ parameters:
2929
message: "#^Binary operation \"/\" between float and array\\|float\\|int\\|string results in an error\\.$#"
3030
count: 1
3131
path: src/PhpSpreadsheet/Calculation/MathTrig/Combinations.php
32+
33+
-
34+
message: "#^Offset 2 does not exist on array\\{int, int, int, int\\}\\|array\\{int, int\\}\\.$#"
35+
count: 1
36+
path: src/PhpSpreadsheet/Worksheet/Validations.php
37+
38+
-
39+
message: "#^Offset 3 does not exist on array\\{int, int, int, int\\}\\|array\\{int, int\\}\\.$#"
40+
count: 1
41+
path: src/PhpSpreadsheet/Worksheet/Validations.php

src/PhpSpreadsheet/Cell/CellAddress.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public function __destruct()
3030
$this->worksheet = null;
3131
}
3232

33+
/**
34+
* @phpstan-assert int|numeric-string $columnId
35+
* @phpstan-assert int|numeric-string $rowId
36+
*/
3337
private static function validateColumnAndRow(mixed $columnId, mixed $rowId): void
3438
{
3539
if (!is_numeric($columnId) || $columnId <= 0 || !is_numeric($rowId) || $rowId <= 0) {

src/PhpSpreadsheet/Cell/Coordinate.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,9 @@ public static function columnIndexFromString(?string $columnAddress): int
318318
/**
319319
* String from column index.
320320
*
321-
* @param int $columnIndex Column index (A = 1)
322-
*
323-
* @return string
321+
* @param int|numeric-string $columnIndex Column index (A = 1)
324322
*/
325-
public static function stringFromColumnIndex($columnIndex)
323+
public static function stringFromColumnIndex(int|string $columnIndex): string
326324
{
327325
static $indexCache = [];
328326
static $lookupCache = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ';

src/PhpSpreadsheet/Worksheet/AutoFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function setEvaluated(bool $value): void
4949
/**
5050
* Create a new AutoFilter.
5151
*
52-
* @param AddressRange|array<int>|string $range
52+
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range
5353
* A simple string containing a Cell range like 'A1:E10' is permitted
5454
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
5555
* or an AddressRange object.
@@ -105,7 +105,7 @@ public function getRange()
105105
/**
106106
* Set AutoFilter Cell Range.
107107
*
108-
* @param AddressRange|array<int>|string $range
108+
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range
109109
* A simple string containing a Cell range like 'A1:E10' or a Cell address like 'A1' is permitted
110110
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
111111
* or an AddressRange object.

src/PhpSpreadsheet/Worksheet/Table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Table implements Stringable
6969
/**
7070
* Create a new Table.
7171
*
72-
* @param AddressRange|array<int>|string $range
72+
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range
7373
* A simple string containing a Cell range like 'A1:E10' is permitted
7474
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
7575
* or an AddressRange object.
@@ -273,7 +273,7 @@ public function getRange(): string
273273
/**
274274
* Set Table Cell Range.
275275
*
276-
* @param AddressRange|array<int>|string $range
276+
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range
277277
* A simple string containing a Cell range like 'A1:E10' is permitted
278278
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
279279
* or an AddressRange object.

src/PhpSpreadsheet/Worksheet/Validations.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static function validateCellAddress($cellAddress): string
3636
/**
3737
* Validate a cell address or cell range.
3838
*
39-
* @param AddressRange|array<int>|CellAddress|int|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
39+
* @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';
4040
* or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
4141
* or as a CellAddress or AddressRange object.
4242
*/
@@ -59,11 +59,11 @@ public static function validateCellOrCellRange($cellRange): string
5959
/**
6060
* Validate a cell range.
6161
*
62-
* @param AddressRange|array<int>|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
62+
* @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';
6363
* or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
6464
* or as an AddressRange object.
6565
*/
66-
public static function validateCellRange($cellRange): string
66+
public static function validateCellRange(AddressRange|array|string $cellRange): string
6767
{
6868
if (is_string($cellRange)) {
6969
[$worksheet, $addressRange] = Worksheet::extractSheetTitle($cellRange, true);

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ public function getStyles()
15261526
/**
15271527
* Get style for cell.
15281528
*
1529-
* @param AddressRange|array<int>|CellAddress|int|string $cellCoordinate
1529+
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $cellCoordinate
15301530
* A simple string containing a cell address like 'A1' or a cell range like 'A1:E10'
15311531
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
15321532
* or a CellAddress or AddressRange object.
@@ -1873,7 +1873,7 @@ public function getColumnBreaks()
18731873
/**
18741874
* Set merge on a cell range.
18751875
*
1876-
* @param AddressRange|array<int>|string $range A simple string containing a Cell range like 'A1:E10'
1876+
* @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'
18771877
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
18781878
* or an AddressRange.
18791879
* @param string $behaviour How the merged cells should behave.
@@ -2029,7 +2029,7 @@ public function mergeCellsByColumnAndRow($columnIndex1, $row1, $columnIndex2, $r
20292029
/**
20302030
* Remove merge on a cell range.
20312031
*
2032-
* @param AddressRange|array<int>|string $range A simple string containing a Cell range like 'A1:E10'
2032+
* @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'
20332033
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
20342034
* or an AddressRange.
20352035
*
@@ -2106,7 +2106,7 @@ public function setMergeCells(array $mergeCells): static
21062106
/**
21072107
* Set protection on a cell or cell range.
21082108
*
2109-
* @param AddressRange|array<int>|CellAddress|int|string $range A simple string containing a Cell range like 'A1:E10'
2109+
* @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'
21102110
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
21112111
* or a CellAddress or AddressRange object.
21122112
* @param string $password Password to unlock the protection
@@ -2157,7 +2157,7 @@ public function protectCellsByColumnAndRow($columnIndex1, $row1, $columnIndex2,
21572157
/**
21582158
* Remove protection on a cell or cell range.
21592159
*
2160-
* @param AddressRange|array<int>|CellAddress|int|string $range A simple string containing a Cell range like 'A1:E10'
2160+
* @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'
21612161
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
21622162
* or a CellAddress or AddressRange object.
21632163
*
@@ -2225,7 +2225,7 @@ public function getAutoFilter()
22252225
/**
22262226
* Set AutoFilter.
22272227
*
2228-
* @param AddressRange|array<int>|AutoFilter|string $autoFilterOrRange
2228+
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|AutoFilter|string $autoFilterOrRange
22292229
* A simple string containing a Cell range like 'A1:E10' is permitted for backward compatibility
22302230
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
22312231
* or an AddressRange.
@@ -3041,7 +3041,7 @@ public function setSelectedCell($coordinate): static
30413041
/**
30423042
* Select a range of cells.
30433043
*
3044-
* @param AddressRange|array<int>|CellAddress|int|string $coordinate A simple string containing a Cell range like 'A1:E10'
3044+
* @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'
30453045
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
30463046
* or a CellAddress or AddressRange object.
30473047
*

tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function testGetRange(): void
156156
}
157157

158158
/**
159-
* @param AddressRange|array<int>|string $fullRange
159+
* @param AddressRange|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $fullRange
160160
*/
161161
public function xtestSetRangeValidRange(string|array|AddressRange $fullRange, string $actualRange): void
162162
{

0 commit comments

Comments
 (0)