Skip to content

Commit 5179fff

Browse files
authored
Merge pull request #4639 from oleibman/wakeup
Deprecate Worksheet::getHashInt and Spreadsheet::getId
2 parents c07675a + 77eaac2 commit 5179fff

File tree

10 files changed

+64
-29
lines changed

10 files changed

+64
-29
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ 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
- Performance improvement when working with large amounts of cells. [Issue #4607](https://github.com/PHPOffice/PhpSpreadsheet/issues/4607) [PR #4609](https://github.com/PHPOffice/PhpSpreadsheet/pull/4609)
3334
- Minor improvements to Calculation coverage. [PR #4624](https://github.com/PHPOffice/PhpSpreadsheet/pull/4624)
3435
- Conditional formatting in extLst. [Issue #4629](https://github.com/PHPOffice/PhpSpreadsheet/issues/4629) [PR #4633](https://github.com/PHPOffice/PhpSpreadsheet/pull/4633)
3536
- Php8.5 deprecates use of null as array index. [PR #4634](https://github.com/PHPOffice/PhpSpreadsheet/pull/4634)
37+
- 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)
3638
- Use prefix _xlfn for BASE function. [Issue #4638](https://github.com/PHPOffice/PhpSpreadsheet/issues/4638) [PR #4641](https://github.com/PHPOffice/PhpSpreadsheet/pull/4641)
3739

3840
## 2025-09-03 - 5.1.0

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function __construct(int $temporaryStorage = self::STORAGE_MEMORY, ?strin
3939
if (empty($this->tempFileName) || $this->openUri($this->tempFileName) === false) {
4040
// Fallback to memory...
4141
$this->openMemory();
42+
if ($this->tempFileName != '') {
43+
@unlink($this->tempFileName);
44+
}
45+
$this->tempFileName = '';
4246
}
4347
}
4448

@@ -60,7 +64,8 @@ public function __destruct()
6064
}
6165
}
6266

63-
public function __wakeup(): void
67+
/** @param mixed[] $data */
68+
public function __unserialize(array $data): void
6469
{
6570
$this->tempFileName = '';
6671

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: 8 additions & 19 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
*/
@@ -3212,7 +3201,7 @@ private function validateNamedRange(string $definedName, bool $returnNullIfInval
32123201

32133202
if ($namedRange->getLocalOnly()) {
32143203
$worksheet = $namedRange->getWorksheet();
3215-
if ($worksheet === null || $this->hash !== $worksheet->getHashInt()) {
3204+
if ($worksheet === null || $this !== $worksheet) {
32163205
if ($returnNullIfInvalid) {
32173206
return null;
32183207
}
@@ -3344,21 +3333,22 @@ public function garbageCollect(): static
33443333
}
33453334

33463335
// Cache values
3347-
if ($highestColumn < 1) {
3348-
$this->cachedHighestColumn = 1;
3349-
} else {
3350-
$this->cachedHighestColumn = $highestColumn;
3351-
}
3336+
$this->cachedHighestColumn = max(1, $highestColumn);
33523337
/** @var int $highestRow */
33533338
$this->cachedHighestRow = $highestRow;
33543339

33553340
// Return
33563341
return $this;
33573342
}
33583343

3344+
/**
3345+
* @deprecated 5.2.0 Serves no useful purpose. No replacement.
3346+
*
3347+
* @codeCoverageIgnore
3348+
*/
33593349
public function getHashInt(): int
33603350
{
3361-
return $this->hash;
3351+
return spl_object_id($this);
33623352
}
33633353

33643354
/**
@@ -3745,7 +3735,6 @@ public function __clone()
37453735
}
37463736
}
37473737
}
3748-
$this->hash = spl_object_id($this);
37493738
}
37503739

37513740
/**

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
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Shared;
6+
7+
class XMLWriterNoUri extends \PhpOffice\PhpSpreadsheet\Shared\XMLWriter
8+
{
9+
public function openUri(string $uri): bool
10+
{
11+
return false;
12+
}
13+
}

tests/PhpSpreadsheetTests/Shared/XmlWriterTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,28 @@ public function testDiskCache(): void
8383
$expected .= '</root>' . $indentnl;
8484
self::assertSame($expected, $objWriter->getData());
8585
}
86+
87+
public function testFallbackToMemory(): void
88+
{
89+
XMLWriter::$debugEnabled = false;
90+
$indent = '';
91+
$indentnl = '';
92+
$objWriter = new XMLWriterNoUri(XMLWriter::STORAGE_DISK);
93+
$objWriter->startDocument('1.0', 'UTF-8', 'yes');
94+
$expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n";
95+
$objWriter->startElement('root');
96+
$expected .= '<root>' . $indentnl;
97+
$objWriter->startElement('node');
98+
$expected .= $indent . '<node>';
99+
$objWriter->writeRawData('xyz');
100+
$expected .= 'xyz';
101+
$objWriter->writeRawData(null);
102+
$objWriter->writeRawData(['12', '34', '5']);
103+
$expected .= "12\n34\n5";
104+
$objWriter->endElement(); // node
105+
$expected .= '</node>' . $indentnl;
106+
$objWriter->endElement(); // root
107+
$expected .= '</root>' . $indentnl;
108+
self::assertSame($expected, $objWriter->getData());
109+
}
86110
}

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)