Skip to content

Commit 3dce900

Browse files
committed
Typed coordinate and range parameters
1 parent ad9fe0a commit 3dce900

File tree

5 files changed

+36
-34
lines changed

5 files changed

+36
-34
lines changed

src/PhpSpreadsheet/Worksheet/AutoFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function setEvaluated(bool $value): void
5353
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
5454
* or an AddressRange object.
5555
*/
56-
public function __construct($range = '', ?Worksheet $worksheet = null)
56+
public function __construct(AddressRange|string|array $range = '', ?Worksheet $worksheet = null)
5757
{
5858
if ($range !== '') {
5959
[, $range] = Worksheet::extractSheetTitle(Validations::validateCellRange($range), true);
@@ -105,7 +105,7 @@ public function getRange(): string
105105
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
106106
* or an AddressRange object.
107107
*/
108-
public function setRange($range = ''): self
108+
public function setRange(AddressRange|string|array $range = ''): self
109109
{
110110
$this->evaluated = false;
111111
// extract coordinate

src/PhpSpreadsheet/Worksheet/PageBreak.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ class PageBreak
1414

1515
private int $maxColOrRow;
1616

17-
/** @param array|CellAddress|string $coordinate */
18-
public function __construct(int $breakType, $coordinate, int $maxColOrRow = -1)
17+
/**
18+
* @param array{0: int, 1: int}|CellAddress|string $coordinate
19+
*/
20+
public function __construct(int $breakType, CellAddress|string|array $coordinate, int $maxColOrRow = -1)
1921
{
2022
$coordinate = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate));
2123
$this->breakType = $breakType;

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
* or an AddressRange object.
7070
* @param string $name (e.g. Table1)
7171
*/
72-
public function __construct($range = '', string $name = '')
72+
public function __construct(AddressRange|string|array $range = '', string $name = '')
7373
{
7474
$this->style = new TableStyle();
7575
$this->autoFilter = new AutoFilter($range);
@@ -272,7 +272,7 @@ public function getRange(): string
272272
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
273273
* or an AddressRange object.
274274
*/
275-
public function setRange($range = ''): self
275+
public function setRange(AddressRange|string|array $range = ''): self
276276
{
277277
// extract coordinate
278278
if ($range !== '') {

src/PhpSpreadsheet/Worksheet/Validations.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class Validations
1212
/**
1313
* Validate a cell address.
1414
*
15-
* @param null|array<int>|CellAddress|string $cellAddress Coordinate of the cell as a string, eg: 'C5';
15+
* @param null|array{0: int, 1: int}|CellAddress|string $cellAddress Coordinate of the cell as a string, eg: 'C5';
1616
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
1717
*/
18-
public static function validateCellAddress($cellAddress): string
18+
public static function validateCellAddress(null|CellAddress|string|array $cellAddress): string
1919
{
2020
if (is_string($cellAddress)) {
2121
[$worksheet, $address] = Worksheet::extractSheetTitle($cellAddress, true);
@@ -40,7 +40,7 @@ public static function validateCellAddress($cellAddress): string
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
*/
43-
public static function validateCellOrCellRange($cellRange): string
43+
public static function validateCellOrCellRange(AddressRange|CellAddress|int|string|array $cellRange): string
4444
{
4545
if (is_string($cellRange) || is_numeric($cellRange)) {
4646
// Convert a single column reference like 'A' to 'A:A',
@@ -63,7 +63,7 @@ public static function validateCellOrCellRange($cellRange): string
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(AddressRange|array|string $cellRange): string
66+
public static function validateCellRange(AddressRange|string|array $cellRange): string
6767
{
6868
if (is_string($cellRange)) {
6969
[$worksheet, $addressRange] = Worksheet::extractSheetTitle($cellRange, true);

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,14 +1103,14 @@ public function getHighestRowAndColumn(): array
11031103
/**
11041104
* Set a cell value.
11051105
*
1106-
* @param array<int>|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
1106+
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
11071107
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
11081108
* @param mixed $value Value for the cell
11091109
* @param null|IValueBinder $binder Value Binder to override the currently set Value Binder
11101110
*
11111111
* @return $this
11121112
*/
1113-
public function setCellValue($coordinate, mixed $value, ?IValueBinder $binder = null): static
1113+
public function setCellValue(CellAddress|string|array $coordinate, mixed $value, ?IValueBinder $binder = null): static
11141114
{
11151115
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate));
11161116
$this->getCell($cellAddress)->setValue($value, $binder);
@@ -1121,7 +1121,7 @@ public function setCellValue($coordinate, mixed $value, ?IValueBinder $binder =
11211121
/**
11221122
* Set a cell value.
11231123
*
1124-
* @param array<int>|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
1124+
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
11251125
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
11261126
* @param mixed $value Value of the cell
11271127
* @param string $dataType Explicit data type, see DataType::TYPE_*
@@ -1135,7 +1135,7 @@ public function setCellValue($coordinate, mixed $value, ?IValueBinder $binder =
11351135
*
11361136
* @return $this
11371137
*/
1138-
public function setCellValueExplicit($coordinate, mixed $value, string $dataType): static
1138+
public function setCellValueExplicit(CellAddress|string|array $coordinate, mixed $value, string $dataType): static
11391139
{
11401140
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate));
11411141
$this->getCell($cellAddress)->setValueExplicit($value, $dataType);
@@ -1146,7 +1146,7 @@ public function setCellValueExplicit($coordinate, mixed $value, string $dataType
11461146
/**
11471147
* Get cell at a specific coordinate.
11481148
*
1149-
* @param array<int>|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
1149+
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
11501150
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
11511151
*
11521152
* @return Cell Cell that was found or created
@@ -1156,7 +1156,7 @@ public function setCellValueExplicit($coordinate, mixed $value, string $dataType
11561156
* the "active" cell, and any previous assignment becomes a disconnected reference because
11571157
* the active cell has changed.
11581158
*/
1159-
public function getCell($coordinate): Cell
1159+
public function getCell(CellAddress|string|array $coordinate): Cell
11601160
{
11611161
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate));
11621162

@@ -1299,10 +1299,10 @@ public function createNewCell(string $coordinate): Cell
12991299
/**
13001300
* Does the cell at a specific coordinate exist?
13011301
*
1302-
* @param array<int>|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
1302+
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
13031303
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
13041304
*/
1305-
public function cellExists($coordinate): bool
1305+
public function cellExists(CellAddress|string|array $coordinate): bool
13061306
{
13071307
$cellAddress = Validations::validateCellAddress($coordinate);
13081308
[$sheet, $finalCoordinate] = $this->getWorksheetAndCoordinate($cellAddress);
@@ -1388,7 +1388,7 @@ public function getStyles(): array
13881388
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
13891389
* or a CellAddress or AddressRange object.
13901390
*/
1391-
public function getStyle($cellCoordinate): Style
1391+
public function getStyle(AddressRange|CellAddress|int|string|array $cellCoordinate): Style
13921392
{
13931393
$cellCoordinate = Validations::validateCellOrCellRange($cellCoordinate);
13941394

@@ -1589,13 +1589,13 @@ public function duplicateConditionalStyle(array $styles, string $range = ''): st
15891589
/**
15901590
* Set break on a cell.
15911591
*
1592-
* @param array<int>|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
1592+
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
15931593
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
15941594
* @param int $break Break type (type of Worksheet::BREAK_*)
15951595
*
15961596
* @return $this
15971597
*/
1598-
public function setBreak($coordinate, int $break, int $max = -1): static
1598+
public function setBreak(CellAddress|string|array $coordinate, int $break, int $max = -1): static
15991599
{
16001600
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate));
16011601

@@ -1692,7 +1692,7 @@ public function getColumnBreaks(): array
16921692
*
16931693
* @return $this
16941694
*/
1695-
public function mergeCells($range, string $behaviour = self::MERGE_CELL_CONTENT_EMPTY): static
1695+
public function mergeCells(AddressRange|string|array $range, string $behaviour = self::MERGE_CELL_CONTENT_EMPTY): static
16961696
{
16971697
$range = Functions::trimSheetFromCellReference(Validations::validateCellRange($range));
16981698

@@ -1812,7 +1812,7 @@ public function mergeCellBehaviour(Cell $cell, string $upperLeft, string $behavi
18121812
*
18131813
* @return $this
18141814
*/
1815-
public function unmergeCells($range): static
1815+
public function unmergeCells(AddressRange|string|array $range): static
18161816
{
18171817
$range = Functions::trimSheetFromCellReference(Validations::validateCellRange($range));
18181818

@@ -1865,7 +1865,7 @@ public function setMergeCells(array $mergeCells): static
18651865
*
18661866
* @return $this
18671867
*/
1868-
public function protectCells($range, string $password, bool $alreadyHashed = false): static
1868+
public function protectCells(AddressRange|CellAddress|int|string|array $range, string $password, bool $alreadyHashed = false): static
18691869
{
18701870
$range = Functions::trimSheetFromCellReference(Validations::validateCellOrCellRange($range));
18711871

@@ -1886,7 +1886,7 @@ public function protectCells($range, string $password, bool $alreadyHashed = fal
18861886
*
18871887
* @return $this
18881888
*/
1889-
public function unprotectCells($range): static
1889+
public function unprotectCells(AddressRange|CellAddress|int|string|array $range): static
18901890
{
18911891
$range = Functions::trimSheetFromCellReference(Validations::validateCellOrCellRange($range));
18921892

@@ -1927,7 +1927,7 @@ public function getAutoFilter(): AutoFilter
19271927
*
19281928
* @return $this
19291929
*/
1930-
public function setAutoFilter($autoFilterOrRange): static
1930+
public function setAutoFilter(AddressRange|string|array|AutoFilter $autoFilterOrRange): static
19311931
{
19321932
if (is_object($autoFilterOrRange) && ($autoFilterOrRange instanceof AutoFilter)) {
19331933
$this->autoFilter = $autoFilterOrRange;
@@ -2063,16 +2063,16 @@ public function getFreezePane(): ?string
20632063
* - B1 will freeze the columns to the left of cell B1 (i.e column A)
20642064
* - B2 will freeze the rows above and to the left of cell B2 (i.e row 1 and column A)
20652065
*
2066-
* @param null|array<int>|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
2066+
* @param null|array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
20672067
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
20682068
* Passing a null value for this argument will clear any existing freeze pane for this worksheet.
2069-
* @param null|array<int>|CellAddress|string $topLeftCell default position of the right bottom pane
2069+
* @param null|array{0: int, 1: int}|CellAddress|string $topLeftCell default position of the right bottom pane
20702070
* Coordinate of the cell as a string, eg: 'C5'; or as an array of [$columnIndex, $row] (e.g. [3, 5]),
20712071
* or a CellAddress object.
20722072
*
20732073
* @return $this
20742074
*/
2075-
public function freezePane($coordinate, $topLeftCell = null, bool $frozenSplit = false): static
2075+
public function freezePane(null|CellAddress|string|array $coordinate, null|CellAddress|string|array $topLeftCell = null, bool $frozenSplit = false): static
20762076
{
20772077
$this->panes = [
20782078
'bottomRight' => null,
@@ -2577,12 +2577,12 @@ public function setComments(array $comments): self
25772577
/**
25782578
* Remove comment from cell.
25792579
*
2580-
* @param array<int>|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
2580+
* @param array{0: int, 1: int}|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
25812581
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
25822582
*
25832583
* @return $this
25842584
*/
2585-
public function removeComment($cellCoordinate): self
2585+
public function removeComment(CellAddress|string|array $cellCoordinate): self
25862586
{
25872587
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate));
25882588

@@ -2604,10 +2604,10 @@ public function removeComment($cellCoordinate): self
26042604
/**
26052605
* Get comment for cell.
26062606
*
2607-
* @param array<int>|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
2607+
* @param array{0: int, 1: int}|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
26082608
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
26092609
*/
2610-
public function getComment($cellCoordinate): Comment
2610+
public function getComment(CellAddress|string|array $cellCoordinate): Comment
26112611
{
26122612
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate));
26132613

@@ -2670,7 +2670,7 @@ public function setSelectedCell(string $coordinate): static
26702670
*
26712671
* @return $this
26722672
*/
2673-
public function setSelectedCells($coordinate): static
2673+
public function setSelectedCells(AddressRange|CellAddress|int|string|array $coordinate): static
26742674
{
26752675
if (is_string($coordinate)) {
26762676
$coordinate = Validations::definedNameToCoordinate($coordinate, $this);

0 commit comments

Comments
 (0)