BREAKING `Worksheet::getRowDimension()` and `Worksheet::getColumnDimension()` cannot return null anymore

Both methods used to optionally return null if passed a
second argument. This second argument was removed entirely and the
method always returns a RowDimension or ColumnDimension respectively
(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::getRowDimensions()` or `Worksheet::getColumnDimensions()` and
check for existence yourself before calling the getters.
This commit is contained in:
Adrien Crivelli 2021-04-25 16:10:09 +09:00
parent 2e39ece373
commit 4e2259c135
No known key found for this signature in database
GPG Key ID: 16D79B903B4B5874
6 changed files with 24 additions and 317 deletions

View File

@ -2590,31 +2590,11 @@ parameters:
count: 2
path: src/PhpSpreadsheet/Reader/Gnumeric.php
-
message: "#^Cannot call method setWidth\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 3
path: src/PhpSpreadsheet/Reader/Gnumeric.php
-
message: "#^Cannot call method setVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Gnumeric.php
-
message: "#^Offset 'DefaultSizePts' does not exist on SimpleXMLElement\\|null\\.$#"
count: 2
path: src/PhpSpreadsheet/Reader/Gnumeric.php
-
message: "#^Cannot call method setRowHeight\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 2
path: src/PhpSpreadsheet/Reader/Gnumeric.php
-
message: "#^Cannot call method setVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Gnumeric.php
-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Gnumeric\\:\\:parseBorderAttributes\\(\\) has no return typehint specified\\.$#"
count: 1
@ -2760,16 +2740,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Reader/Html.php
-
message: "#^Cannot call method setRowHeight\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 4
path: src/PhpSpreadsheet/Reader/Html.php
-
message: "#^Cannot call method setWidth\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 3
path: src/PhpSpreadsheet/Reader/Html.php
-
message: "#^Parameter \\#2 \\$styleValue of method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Html\\:\\:setBorderStyle\\(\\) expects string, string\\|null given\\.$#"
count: 5
@ -3020,11 +2990,6 @@ parameters:
count: 3
path: src/PhpSpreadsheet/Reader/Slk.php
-
message: "#^Cannot call method setWidth\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 3
path: src/PhpSpreadsheet/Reader/Slk.php
-
message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Escher\\|PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Escher\\\\DgContainer\\|PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Escher\\\\DgContainer\\\\SpgrContainer\\|PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Escher\\\\DgContainer\\\\SpgrContainer\\\\SpContainer\\|PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Escher\\\\DggContainer\\|PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Escher\\\\DggContainer\\\\BstoreContainer\\|PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Escher\\\\DggContainer\\\\BstoreContainer\\\\BSE\\:\\:getDgContainer\\(\\)\\.$#"
count: 1
@ -3225,56 +3190,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Reader/Xls.php
-
message: "#^Cannot call method setWidth\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls.php
-
message: "#^Cannot call method setVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls.php
-
message: "#^Cannot call method setOutlineLevel\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls.php
-
message: "#^Cannot call method setCollapsed\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls.php
-
message: "#^Cannot call method setXfIndex\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls.php
-
message: "#^Cannot call method setRowHeight\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls.php
-
message: "#^Cannot call method setOutlineLevel\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls.php
-
message: "#^Cannot call method setCollapsed\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls.php
-
message: "#^Cannot call method setVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls.php
-
message: "#^Cannot call method setXfIndex\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls.php
-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\:\\:includeCellRangeFiltered\\(\\) has no return typehint specified\\.$#"
count: 1
@ -4450,16 +4365,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Reader/Xml.php
-
message: "#^Cannot call method setWidth\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xml.php
-
message: "#^Cannot call method setRowHeight\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xml.php
-
message: "#^Parameter \\#2 \\$cmp_function of function uksort expects callable\\(mixed, mixed\\)\\: int, array\\('self', 'cellReverseSort'\\) given\\.$#"
count: 4
@ -4475,46 +4380,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/ReferenceHelper.php
-
message: "#^Cannot call method getRowHeight\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/ReferenceHelper.php
-
message: "#^Cannot call method setRowHeight\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/ReferenceHelper.php
-
message: "#^Cannot call method getVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/ReferenceHelper.php
-
message: "#^Cannot call method setVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/ReferenceHelper.php
-
message: "#^Cannot call method getOutlineLevel\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/ReferenceHelper.php
-
message: "#^Cannot call method setOutlineLevel\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/ReferenceHelper.php
-
message: "#^Cannot call method getCollapsed\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/ReferenceHelper.php
-
message: "#^Cannot call method setCollapsed\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|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
@ -5930,26 +5795,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Style/Style.php
-
message: "#^Cannot call method getXfIndex\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 2
path: src/PhpSpreadsheet/Style/Style.php
-
message: "#^Cannot call method setXfIndex\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Style/Style.php
-
message: "#^Cannot call method getXfIndex\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 3
path: src/PhpSpreadsheet/Style/Style.php
-
message: "#^Cannot call method setXfIndex\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Style/Style.php
-
message: "#^Result of && is always true\\.$#"
count: 1
@ -6040,11 +5885,6 @@ parameters:
count: 2
path: src/PhpSpreadsheet/Worksheet/AutoFilter.php
-
message: "#^Cannot call method setVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 2
path: src/PhpSpreadsheet/Worksheet/AutoFilter.php
-
message: "#^Cannot assign offset 'year' to array\\<string\\>\\|string\\.$#"
count: 1
@ -6320,11 +6160,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Worksheet/Worksheet.php
-
message: "#^Cannot call method setWidth\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Worksheet/Worksheet.php
-
message: "#^If condition is always true\\.$#"
count: 2
@ -6345,11 +6180,6 @@ parameters:
count: 2
path: src/PhpSpreadsheet/Worksheet/Worksheet.php
-
message: "#^Parameter \\#1 \\$pRow of method PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\Worksheet\\:\\:getRowDimension\\(\\) expects int, string given\\.$#"
count: 1
path: src/PhpSpreadsheet/Worksheet/Worksheet.php
-
message: "#^Result of && is always true\\.$#"
count: 1
@ -7760,31 +7590,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
-
message: "#^Cannot call method getCollapsed\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 2
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
-
message: "#^Cannot call method getOutlineLevel\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 3
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
-
message: "#^Cannot call method getRowHeight\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 3
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
-
message: "#^Cannot call method getVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 2
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
-
message: "#^Cannot call method getXfIndex\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 3
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
-
message: "#^Parameter \\#2 \\$content of method XMLWriter\\:\\:writeElement\\(\\) expects string\\|null, int\\|string given\\.$#"
count: 1
@ -7805,11 +7610,6 @@ parameters:
count: 1
path: tests/PhpSpreadsheetTests/Calculation/Engine/RangeTest.php
-
message: "#^Cannot call method setAutoSize\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: tests/PhpSpreadsheetTests/Calculation/XlfnFunctionsTest.php
-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertSame\\(\\) with arguments PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Cell, null and 'should get exact…' will always evaluate to false\\.$#"
count: 1
@ -7835,16 +7635,6 @@ parameters:
count: 1
path: tests/PhpSpreadsheetTests/Functional/ColumnWidthTest.php
-
message: "#^Cannot call method setWidth\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: tests/PhpSpreadsheetTests/Functional/ColumnWidthTest.php
-
message: "#^Cannot call method getWidth\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: tests/PhpSpreadsheetTests/Functional/ColumnWidthTest.php
-
message: "#^Method PhpOffice\\\\PhpSpreadsheetTests\\\\Functional\\\\CommentsTest\\:\\:testComments\\(\\) has parameter \\$format with no typehint specified\\.$#"
count: 1
@ -7855,11 +7645,6 @@ parameters:
count: 2
path: tests/PhpSpreadsheetTests/Functional/ConditionalStopIfTrueTest.php
-
message: "#^Cannot call method setAutoSize\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: tests/PhpSpreadsheetTests/Functional/ConditionalTextTest.php
-
message: "#^Parameter \\#1 \\$im of function imagecolorallocate expects resource, resource\\|false given\\.$#"
count: 1
@ -7955,26 +7740,6 @@ parameters:
count: 1
path: tests/PhpSpreadsheetTests/Reader/CsvTest.php
-
message: "#^Cannot call method getVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: tests/PhpSpreadsheetTests/Reader/Gnumeric/GnumericLoadTest.php
-
message: "#^Cannot call method getRowHeight\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 2
path: tests/PhpSpreadsheetTests/Reader/Html/HtmlTagsTest.php
-
message: "#^Cannot call method getWidth\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 2
path: tests/PhpSpreadsheetTests/Reader/Html/HtmlTest.php
-
message: "#^Cannot call method getRowHeight\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 2
path: tests/PhpSpreadsheetTests/Reader/Html/HtmlTest.php
-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
@ -8065,26 +7830,6 @@ parameters:
count: 1
path: tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalFormattingDataBarXlsxTest.php
-
message: "#^Cannot call method getRowHeight\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: tests/PhpSpreadsheetTests/Reader/XlsxTest.php
-
message: "#^Cannot call method getVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: tests/PhpSpreadsheetTests/Reader/XlsxTest.php
-
message: "#^Cannot call method getWidth\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: tests/PhpSpreadsheetTests/Reader/XlsxTest.php
-
message: "#^Cannot call method getVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1
path: tests/PhpSpreadsheetTests/Reader/XlsxTest.php
-
message: "#^Method PhpOffice\\\\PhpSpreadsheetTests\\\\Reader\\\\XlsxTest\\:\\:testStripsWhiteSpaceFromStyleString\\(\\) has parameter \\$string with no typehint specified\\.$#"
count: 1
@ -8240,21 +7985,6 @@ parameters:
count: 1
path: tests/PhpSpreadsheetTests/Writer/Html/ImagesRootTest.php
-
message: "#^Cannot call method setVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 2
path: tests/PhpSpreadsheetTests/Writer/Html/VisibilityTest.php
-
message: "#^Cannot call method setVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 3
path: tests/PhpSpreadsheetTests/Writer/Html/VisibilityTest.php
-
message: "#^Cannot call method setRowHeight\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#"
count: 1
path: tests/PhpSpreadsheetTests/Writer/Html/VisibilityTest.php
-
message: "#^Parameter \\#1 \\$options of static method PhpOffice\\\\PhpSpreadsheet\\\\Settings\\:\\:setLibXmlLoaderOptions\\(\\) expects int, null given\\.$#"
count: 1

View File

@ -220,13 +220,13 @@ class Html extends BaseReader
/**
* Set input encoding.
*
* @deprecated no use is made of this property
*
* @param string $pValue Input encoding, eg: 'ANSI'
*
* @return $this
*
* @codeCoverageIgnore
*
* @deprecated no use is made of this property
*/
public function setInputEncoding($pValue)
{
@ -238,11 +238,11 @@ class Html extends BaseReader
/**
* Get input encoding.
*
* @deprecated no use is made of this property
*
* @return string
*
* @codeCoverageIgnore
*
* @deprecated no use is made of this property
*/
public function getInputEncoding()
{
@ -620,7 +620,7 @@ class Html extends BaseReader
$cellContent .= $domText;
}
// but if we have a rich text run instead, we need to append it correctly
// TODO
// TODO
} elseif ($child instanceof DOMElement) {
$this->processDomElementBody($sheet, $row, $column, $cellContent, $child);
}
@ -878,14 +878,14 @@ class Html extends BaseReader
case 'width':
$sheet->getColumnDimension($column)->setWidth(
str_replace('px', '', $styleValue)
(float) str_replace('px', '', $styleValue)
);
break;
case 'height':
$sheet->getRowDimension($row)->setRowHeight(
str_replace('px', '', $styleValue)
(float) str_replace('px', '', $styleValue)
);
break;
@ -922,8 +922,8 @@ class Html extends BaseReader
}
/**
* @param string $column
* @param int $row
* @param string $column
* @param int $row
*/
private function insertImage(Worksheet $sheet, $column, $row, array $attributes): void
{
@ -990,7 +990,7 @@ class Html extends BaseReader
/**
* Map html border style to PhpSpreadsheet border style.
*
* @param string $style
* @param string $style
*
* @return null|string
*/

View File

@ -3634,7 +3634,7 @@ class Xls extends BaseReader
$level = (0x0700 & self::getUInt2d($recordData, 8)) >> 8;
// bit: 12; mask: 0x1000; 1 = collapsed
$isCollapsed = (0x1000 & self::getUInt2d($recordData, 8)) >> 12;
$isCollapsed = (bool) ((0x1000 & self::getUInt2d($recordData, 8)) >> 12);
// offset: 10; size: 2; not used
@ -3704,7 +3704,7 @@ class Xls extends BaseReader
$this->phpSheet->getRowDimension($r + 1)->setOutlineLevel($level);
// bit: 4; mask: 0x00000010; 1 = outline group start or ends here... and is collapsed
$isCollapsed = (0x00000010 & self::getInt4d($recordData, 12)) >> 4;
$isCollapsed = (bool) ((0x00000010 & self::getInt4d($recordData, 12)) >> 4);
$this->phpSheet->getRowDimension($r + 1)->setCollapsed($isCollapsed);
// bit: 5; mask: 0x00000020; 1 = row is hidden

View File

@ -1296,7 +1296,7 @@ class Worksheet implements IComparable
$this->cellCollectionIsSorted = false;
// Coordinates
$aCoordinates = Coordinate::coordinateFromString($pCoordinate);
[$column, $row] = Coordinate::coordinateFromString($pCoordinate);
$aIndexes = Coordinate::indexesFromString($pCoordinate);
if ($this->cachedHighestColumn < $aIndexes[0]) {
$this->cachedHighestColumn = $aIndexes[0];
@ -1307,8 +1307,8 @@ class Worksheet implements IComparable
// Cell needs appropriate xfIndex from dimensions records
// but don't create dimension records if they don't already exist
$rowDimension = $this->getRowDimension($aCoordinates[1], false);
$columnDimension = $this->getColumnDimension($aCoordinates[0], false);
$rowDimension = $this->rowDimensions[$row] ?? null;
$columnDimension = $this->columnDimensions[$column] ?? null;
if ($rowDimension !== null && $rowDimension->getXfIndex() > 0) {
// then there is a row dimension with explicit style, assign it to the cell
@ -1353,20 +1353,11 @@ class Worksheet implements IComparable
* Get row dimension at a specific row.
*
* @param int $pRow Numeric index of the row
* @param bool $create
*
* @return null|RowDimension
*/
public function getRowDimension($pRow, $create = true)
public function getRowDimension(int $pRow): RowDimension
{
// Found
$found = null;
// Get row dimension
if (!isset($this->rowDimensions[$pRow])) {
if (!$create) {
return null;
}
$this->rowDimensions[$pRow] = new RowDimension($pRow);
$this->cachedHighestRow = max($this->cachedHighestRow, $pRow);
@ -1379,20 +1370,14 @@ class Worksheet implements IComparable
* Get column dimension at a specific column.
*
* @param string $pColumn String index of the column eg: 'A'
* @param bool $create
*
* @return null|ColumnDimension
*/
public function getColumnDimension($pColumn, $create = true)
public function getColumnDimension(string $pColumn): ColumnDimension
{
// Uppercase coordinate
$pColumn = strtoupper($pColumn);
// Fetch dimensions
if (!isset($this->columnDimensions[$pColumn])) {
if (!$create) {
return null;
}
$this->columnDimensions[$pColumn] = new ColumnDimension($pColumn);
$columnIndex = Coordinate::columnIndexFromString($pColumn);
@ -1408,10 +1393,8 @@ class Worksheet implements IComparable
* Get column dimension at a specific column by using numeric cell coordinates.
*
* @param int $columnIndex Numeric column coordinate of the cell
*
* @return null|ColumnDimension
*/
public function getColumnDimensionByColumn($columnIndex)
public function getColumnDimensionByColumn(int $columnIndex): ColumnDimension
{
return $this->getColumnDimension(Coordinate::stringFromColumnIndex($columnIndex));
}

View File

@ -56,12 +56,8 @@ class SubTotalTest extends AllSetupTeardown
'L' => false,
];
foreach ($hiddenColumns as $col => $hidden) {
$colDim = $sheet->getColumnDimension($col);
if ($colDim === null) {
self::fail('Unexpected null column dimension');
} else {
$colDim->setVisible($hidden);
}
$columnDimension = $sheet->getColumnDimension($col);
$columnDimension->setVisible($hidden);
}
$sheet->getCell('D2')->setValue("=SUBTOTAL($type, A1:$maxCol$maxRow)");
$result = $sheet->getCell('D2')->getCalculatedValue();
@ -96,12 +92,8 @@ class SubTotalTest extends AllSetupTeardown
'12' => false,
];
foreach ($visibleRows as $row => $visible) {
$rowDim = $sheet->getRowDimension($row);
if ($rowDim === null) {
self::fail('Unexpected null row dimension');
} else {
$rowDim->setVisible($visible);
}
$rowDimension = $sheet->getRowDimension($row);
$rowDimension->setVisible($visible);
}
$sheet->getCell('D2')->setValue("=SUBTOTAL($type, A1:$maxCol$maxRow)");
$result = $sheet->getCell('D2')->getCalculatedValue();

View File

@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Functional;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension;
class ColumnWidthTest extends AbstractFunctional
{
@ -36,6 +37,7 @@ class ColumnWidthTest extends AbstractFunctional
self::assertArrayHasKey('A', $columnDimensions);
$column = array_shift($columnDimensions);
self::assertInstanceOf(ColumnDimension::class, $column);
self::assertEquals(20, $column->getWidth());
}
}