From dd8fee52ac7d01206bb40e0329f2fc9f7aaec6d3 Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Mon, 27 Jan 2025 21:50:05 -0800 Subject: [PATCH] Remove Styles Property From Worksheet Styles are a property of Spreadsheet, not Worksheet. There is a Styles property in Worksheet, initially set to an empty array and *never* updated. This PR removes the property and the getStyles method of Worksheet. This is, technically, a breaking change, so it will be installed with PhpSpreadsheet V4 (PR #4240). --- src/PhpSpreadsheet/Worksheet/Worksheet.php | 25 +++---------------- .../Worksheet/Worksheet2Test.php | 13 ++++++++-- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index 81f637e718..ff299508d8 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -63,7 +63,7 @@ class Worksheet /** * Invalid characters in sheet title. */ - private static array $invalidCharacters = ['*', ':', '/', '\\', '?', '[', ']']; + private const INVALID_CHARACTERS = ['*', ':', '/', '\\', '?', '[', ']']; /** * Parent spreadsheet. @@ -157,13 +157,6 @@ class Worksheet */ private Protection $protection; - /** - * Collection of styles. - * - * @var Style[] - */ - private array $styles = []; - /** * Conditional styles. Indexed by cell coordinate, e.g. 'A1'. */ @@ -399,7 +392,7 @@ public function getCellCollection(): Cells */ public static function getInvalidCharacters(): array { - return self::$invalidCharacters; + return self::INVALID_CHARACTERS; } /** @@ -417,7 +410,7 @@ private static function checkSheetCodeName(string $sheetCodeName): string } // Some of the printable ASCII characters are invalid: * : / \ ? [ ] and first and last characters cannot be a "'" if ( - (str_replace(self::$invalidCharacters, '', $sheetCodeName) !== $sheetCodeName) + (str_replace(self::INVALID_CHARACTERS, '', $sheetCodeName) !== $sheetCodeName) || (Shared\StringHelper::substring($sheetCodeName, -1, 1) == '\'') || (Shared\StringHelper::substring($sheetCodeName, 0, 1) == '\'') ) { @@ -442,7 +435,7 @@ private static function checkSheetCodeName(string $sheetCodeName): string private static function checkSheetTitle(string $sheetTitle): string { // Some of the printable ASCII characters are invalid: * : / \ ? [ ] - if (str_replace(self::$invalidCharacters, '', $sheetTitle) !== $sheetTitle) { + if (str_replace(self::INVALID_CHARACTERS, '', $sheetTitle) !== $sheetTitle) { throw new Exception('Invalid character found in sheet title'); } @@ -1392,16 +1385,6 @@ public function getColumnStyle(string $column): ?Style ); } - /** - * Get styles. - * - * @return Style[] - */ - public function getStyles(): array - { - return $this->styles; - } - /** * Get style for cell. * diff --git a/tests/PhpSpreadsheetTests/Worksheet/Worksheet2Test.php b/tests/PhpSpreadsheetTests/Worksheet/Worksheet2Test.php index 4247bd8b8e..e6768dc9e2 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/Worksheet2Test.php +++ b/tests/PhpSpreadsheetTests/Worksheet/Worksheet2Test.php @@ -16,9 +16,18 @@ public function testMiscellaneous(): void $invalid = Worksheet::getInvalidCharacters(); self::assertSame(['*', ':', '/', '\\', '?', '[', ']'], $invalid); $worksheet = new Worksheet(); - self::assertEmpty($worksheet->getStyles()); + $coord1 = $worksheet->getCoordinates(); + self::assertSame([], $coord1); + $worksheet->getCell('B3')->setValue(1); + $worksheet->getCell('G2')->setValue(2); + $coord2 = $worksheet->getCoordinates(false); // in order added + self::assertSame(['B3', 'G2'], $coord2); + $coord3 = $worksheet->getCoordinates(); // sorted by row then column + self::assertSame(['G2', 'B3'], $coord3); + $worksheet = new Worksheet(); $worksheet->disconnectCells(); - self::assertSame([], $worksheet->getCoordinates()); + $coord4 = $worksheet->getCoordinates(); + self::assertSame([], $coord4); } public function testHighestColumn(): void