Skip to content

Commit

Permalink
getStyle Accept RowRange or ColumnRange Using Phpstan
Browse files Browse the repository at this point in the history
Fix PHPOffice#4309. No executable source code is changed, just some doc blocks, and some new tests added.
  • Loading branch information
oleibman committed Jan 8, 2025
1 parent fb757cf commit 77f3f17
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Worksheet/AutoFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function setEvaluated(bool $value): void
/**
* Create a new AutoFilter.
*
* @param AddressRange<CellAddress>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|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
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Worksheet/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Table implements Stringable
/**
* Create a new Table.
*
* @param AddressRange<CellAddress>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|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 @@ -268,7 +268,7 @@ public function getRange(): string
/**
* Set Table Cell Range.
*
* @param AddressRange<CellAddress>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|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
4 changes: 2 additions & 2 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(null|CellAddress|string|array $cellAd
/**
* Validate a cell address or cell range.
*
* @param AddressRange<CellAddress>|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';
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|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,7 +59,7 @@ public static function validateCellOrCellRange(AddressRange|CellAddress|int|stri
/**
* Validate a cell range.
*
* @param AddressRange<CellAddress>|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';
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|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.
*/
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 @@ -1389,7 +1389,7 @@ public function getStyles(): array
/**
* Get style for cell.
*
* @param AddressRange<CellAddress>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $cellCoordinate
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|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 @@ -1693,7 +1693,7 @@ public function getColumnBreaks(): array
/**
* Set merge on a cell range.
*
* @param AddressRange<CellAddress>|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'
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|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 @@ -1818,7 +1818,7 @@ public function mergeCellBehaviour(Cell $cell, string $upperLeft, string $behavi
/**
* Remove merge on a cell range.
*
* @param AddressRange<CellAddress>|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'
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|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 @@ -1869,7 +1869,7 @@ public function setMergeCells(array $mergeCells): static
/**
* Set protection on a cell or cell range.
*
* @param AddressRange<CellAddress>|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'
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|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 All @@ -1892,7 +1892,7 @@ public function protectCells(AddressRange|CellAddress|int|string|array $range, s
/**
* Remove protection on a cell or cell range.
*
* @param AddressRange<CellAddress>|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'
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|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 @@ -1950,7 +1950,7 @@ public function getAutoFilter(): AutoFilter
/**
* Set AutoFilter.
*
* @param AddressRange<CellAddress>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|AutoFilter|string $autoFilterOrRange
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|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 @@ -2696,7 +2696,7 @@ public function setSelectedCell(string $coordinate): static
/**
* Select a range of cells.
*
* @param AddressRange<CellAddress>|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'
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|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
20 changes: 20 additions & 0 deletions tests/PhpSpreadsheetTests/Cell/ColumnRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function testCreateColumnRangeWithWorksheet(): void
self::assertSame('E', $columnRange->to());
self::assertSame("'Mark''s Worksheet'!C:E", (string) $columnRange);
self::assertSame("'Mark''s Worksheet'!C1:E1048576", (string) $columnRange->toCellRange());
$spreadsheet->disconnectWorksheets();
}

public function testCreateColumnRangeFromArray(): void
Expand Down Expand Up @@ -88,4 +89,23 @@ public function testColumnRangePrevious(): void
// Check that original Column Range isn't changed
self::assertSame('C:E', (string) $columnRange);
}

public function testIssue4309(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$columnRange = new ColumnRange('A', 'A');
$columnStyle = $sheet->getStyle($columnRange);
$columnStyle->applyFromArray([
'font' => ['bold' => true],
]);
$columnXf = $sheet->getColumnDimension('A')->getXfIndex();
self::assertNotNull($columnXf);
self::assertTrue(
$spreadsheet->getCellXfByIndex($columnXf)
->getFont()->getBold()
);

$spreadsheet->disconnectWorksheets();
}
}
17 changes: 17 additions & 0 deletions tests/PhpSpreadsheetTests/Cell/RowRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function testCreateRowRangeWithWorksheet(): void
self::assertSame(3, $rowRange->from());
self::assertSame(5, $rowRange->to());
self::assertSame("'Mark''s Worksheet'!3:5", (string) $rowRange);
$spreadsheet->disconnectWorksheets();
}

public function testCreateRowRangeFromArray(): void
Expand Down Expand Up @@ -74,4 +75,20 @@ public function testRowRangePrevious(): void
// Check that original Row Range isn't changed
self::assertSame('3:5', (string) $rowRange);
}

public function testIssue4309(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$rowRange = new RowRange(1, 1);
$rowStyle = $sheet->getStyle($rowRange);
$rowStyle->applyFromArray([
'font' => ['name' => 'Arial'],
]);
$rowXf = $sheet->getRowDimension(1)->getXfIndex();
self::assertNotNull($rowXf);
self::assertSame('Arial', $spreadsheet->getCellXfByIndex($rowXf)->getFont()->getName());

$spreadsheet->disconnectWorksheets();
}
}

0 comments on commit 77f3f17

Please sign in to comment.