Skip to content

Commit 0019110

Browse files
committed
Deprecate Worksheet::getHashInt and Spreadsheet::getId
Php8.5 *may* deprecate the use of `__wakeup` (it is planned, but not yet implemented, and it is convtroversial). We use it only twice. In the first instance, it just throws an exception to prevent a security exploit. This can be trivially replaced with `__unserialize`. The other instance is merely to initialize a Worksheet instance variable. Converting this use to `__unserialize` is *not* trivial (one of the reasons for the controversy). However, I see no useful purpose for that variable. Since it has no use, there is no real need for `__wakeup`, so we will just remove the routine altogether, and deprecate the variable's getter (there is no setter). A similar instance variable in Spreadsheet also serves no useful purpose, so we will deprecate its getter as well (again no setter).
1 parent 889d26a commit 0019110

File tree

8 files changed

+22
-24
lines changed

8 files changed

+22
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ and this project adheres to [Semantic Versioning](https://semver.org). Thia is a
2525

2626
### Deprecated
2727

28-
- Nothing yet.
28+
- Worksheet::getHashInt serves no useful purpose. No replacement.
29+
- Spreadsheet::getId serves no useful purpose. No replacement.
2930

3031
### Fixed
3132

3233
- Php8.5 deprecates use of null as array index. [PR #4634](https://github.com/PHPOffice/PhpSpreadsheet/pull/4634)
34+
- For Php8.5, replace one of our two uses of `__wakeup` with `__unserialize`, and eliminate the other. [PR #4639](https://github.com/PHPOffice/PhpSpreadsheet/pull/4639)
3335

3436
## 2025-09-03 - 5.1.0
3537

src/PhpSpreadsheet/ReferenceHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ private function updateNamedRange(DefinedName $definedName, Worksheet $worksheet
10451045
{
10461046
$cellAddress = $definedName->getValue();
10471047
$asFormula = ($cellAddress[0] === '=');
1048-
if ($definedName->getWorksheet() !== null && $definedName->getWorksheet()->getHashInt() === $worksheet->getHashInt()) {
1048+
if ($definedName->getWorksheet() === $worksheet) {
10491049
/**
10501050
* If we delete the entire range that is referenced by a Named Range, MS Excel sets the value to #REF!
10511051
* PhpSpreadsheet still only does a basic adjustment, so the Named Range will still reference Cells.
@@ -1064,7 +1064,7 @@ private function updateNamedRange(DefinedName $definedName, Worksheet $worksheet
10641064

10651065
private function updateNamedFormula(DefinedName $definedName, Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns, int $numberOfRows): void
10661066
{
1067-
if ($definedName->getWorksheet() !== null && $definedName->getWorksheet()->getHashInt() === $worksheet->getHashInt()) {
1067+
if ($definedName->getWorksheet() === $worksheet) {
10681068
/**
10691069
* If we delete the entire range that is referenced by a Named Formula, MS Excel sets the value to #REF!
10701070
* PhpSpreadsheet still only does a basic adjustment, so the Named Formula will still reference Cells.

src/PhpSpreadsheet/Shared/XMLWriter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public function __destruct()
6060
}
6161
}
6262

63-
public function __wakeup(): void
63+
/** @param mixed[] $data */
64+
public function __unserialize(array $data): void
6465
{
6566
$this->tempFileName = '';
6667

src/PhpSpreadsheet/Spreadsheet.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,8 @@ public function getSheetByNameOrThrow(string $worksheetName): Worksheet
735735
*/
736736
public function getIndex(Worksheet $worksheet, bool $noThrow = false): int
737737
{
738-
$wsHash = $worksheet->getHashInt();
739738
foreach ($this->workSheetCollection as $key => $value) {
740-
if ($value->getHashInt() === $wsHash) {
739+
if ($value === $worksheet) {
741740
return $key;
742741
}
743742
}
@@ -1468,6 +1467,10 @@ public function garbageCollect(): void
14681467

14691468
/**
14701469
* Return the unique ID value assigned to this spreadsheet workbook.
1470+
*
1471+
* @deprecated 5.2.0 Serves no useful purpose. No replacement.
1472+
*
1473+
* @codeCoverageIgnore
14711474
*/
14721475
public function getID(): string
14731476
{

src/PhpSpreadsheet/Worksheet/BaseDrawing.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public function getHashCode(): string
422422
return md5(
423423
$this->name
424424
. $this->description
425-
. (($this->worksheet === null) ? '' : (string) $this->worksheet->getHashInt())
425+
. (($this->worksheet === null) ? '' : (string) spl_object_id($this->worksheet))
426426
. $this->coordinates
427427
. $this->offsetX
428428
. $this->offsetY

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,6 @@ class Worksheet
306306
*/
307307
private ?Color $tabColor = null;
308308

309-
/**
310-
* Hash.
311-
*/
312-
private int $hash;
313-
314309
/**
315310
* CodeName.
316311
*/
@@ -323,7 +318,6 @@ public function __construct(?Spreadsheet $parent = null, string $title = 'Worksh
323318
{
324319
// Set parent and title
325320
$this->parent = $parent;
326-
$this->hash = spl_object_id($this);
327321
$this->setTitle($title, false);
328322
// setTitle can change $pTitle
329323
$this->setCodeName($this->getTitle());
@@ -380,11 +374,6 @@ public function __destruct()
380374
unset($this->rowDimensions, $this->columnDimensions, $this->tableCollection, $this->drawingCollection, $this->chartCollection, $this->autoFilter);
381375
}
382376

383-
public function __wakeup(): void
384-
{
385-
$this->hash = spl_object_id($this);
386-
}
387-
388377
/**
389378
* Return the cell collection.
390379
*/
@@ -3189,7 +3178,7 @@ private function validateNamedRange(string $definedName, bool $returnNullIfInval
31893178

31903179
if ($namedRange->getLocalOnly()) {
31913180
$worksheet = $namedRange->getWorksheet();
3192-
if ($worksheet === null || $this->hash !== $worksheet->getHashInt()) {
3181+
if ($worksheet === null || $this !== $worksheet) {
31933182
if ($returnNullIfInvalid) {
31943183
return null;
31953184
}
@@ -3333,9 +3322,14 @@ public function garbageCollect(): static
33333322
return $this;
33343323
}
33353324

3325+
/**
3326+
* @deprecated 5.2.0 Serves no useful purpose. No replacement.
3327+
*
3328+
* @codeCoverageIgnore
3329+
*/
33363330
public function getHashInt(): int
33373331
{
3338-
return $this->hash;
3332+
return spl_object_id($this);
33393333
}
33403334

33413335
/**
@@ -3722,7 +3716,6 @@ public function __clone()
37223716
}
37233717
}
37243718
}
3725-
$this->hash = spl_object_id($this);
37263719
}
37273720

37283721
/**

tests/PhpSpreadsheetTests/Reader/Xml/XmlTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public function testLoadCorruptedFile(): void
5757

5858
$xmlReader = new Xml();
5959
$spreadsheet = @$xmlReader->load('tests/data/Reader/Xml/CorruptedXmlFile.xml');
60-
self::assertNotSame('', $spreadsheet->getID());
6160
}
6261

6362
public function testListWorksheetNamesCorruptedFile(): void

tests/PhpSpreadsheetTests/Worksheet/CloneTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testSerialize1(): void
5151
$newSheet = unserialize($serialized);
5252
self::assertInstanceOf(Worksheet::class, $newSheet);
5353
self::assertSame(10, $newSheet->getCell('A1')->getValue());
54-
self::assertNotEquals($newSheet->getHashInt(), $sheet1->getHashInt());
54+
self::assertNotSame($newSheet, $sheet1);
5555
self::assertNotNull($newSheet->getParent());
5656
self::assertNotSame($newSheet->getParent(), $sheet1->getParent());
5757
$newSheet->getParent()->disconnectWorksheets();
@@ -66,6 +66,6 @@ public function testSerialize2(): void
6666
$newSheet = unserialize($serialized);
6767
self::assertInstanceOf(Worksheet::class, $newSheet);
6868
self::assertSame(10, $newSheet->getCell('A1')->getValue());
69-
self::assertNotEquals($newSheet->getHashInt(), $sheet1->getHashInt());
69+
self::assertNotSame($newSheet, $sheet1);
7070
}
7171
}

0 commit comments

Comments
 (0)