Skip to content

Commit 5a107b8

Browse files
Use spl_object_id() instead of spl_object_hash() which is deprecated in PHP 8.6
1 parent 752b775 commit 5a107b8

7 files changed

Lines changed: 50 additions & 22 deletions

File tree

ChangeLog-8.5.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes of the PHPUnit 8.5 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
44

5+
## [8.5.54] - 2026-MM-DD
6+
7+
### Changed
8+
9+
* Use `spl_object_id()` instead of `spl_object_hash()` (which will be deprecated in PHP 8.6)
10+
* Use the error suppression operator (`@`) when calling `DeepCopy\DeepCopy::copy()` because it uses `spl_object_hash()` (which will be deprecated in PHP 8.6)
11+
512
## [8.5.53] - 2026-07-06
613

714
### Changed
@@ -386,6 +393,7 @@ All notable changes of the PHPUnit 8.5 release series are documented in this fil
386393
* [#3967](https://github.com/sebastianbergmann/phpunit/issues/3967): Cannot double interface that extends interface that extends `\Throwable`
387394
* [#3968](https://github.com/sebastianbergmann/phpunit/pull/3968): Test class run in a separate PHP process are passing when `exit` called inside
388395

396+
[8.5.54]: https://github.com/sebastianbergmann/phpunit/compare/8.5.53...8.5
389397
[8.5.53]: https://github.com/sebastianbergmann/phpunit/compare/8.5.52...8.5.53
390398
[8.5.52]: https://github.com/sebastianbergmann/phpunit/compare/8.5.51...8.5.52
391399
[8.5.51]: https://github.com/sebastianbergmann/phpunit/compare/8.5.50...8.5.51

src/Framework/ExceptionWrapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use function array_keys;
1313
use function get_class;
14-
use function spl_object_hash;
14+
use function spl_object_id;
1515
use PHPUnit\Util\Filter;
1616
use Throwable;
1717

@@ -109,7 +109,7 @@ private function originalException(?Throwable $exceptionToStore = null): ?Throwa
109109
{
110110
static $originalExceptions;
111111

112-
$instanceId = spl_object_hash($this);
112+
$instanceId = spl_object_id($this);
113113

114114
if ($exceptionToStore) {
115115
$originalExceptions[$instanceId] = $exceptionToStore;

src/Framework/TestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2173,7 +2173,8 @@ private function handleDependencies(): bool
21732173
$deepCopy = new DeepCopy;
21742174
$deepCopy->skipUncloneable(false);
21752175

2176-
$this->dependencyInput[$dependency] = $deepCopy->copy($passed[$dependency]['result']);
2176+
// The diagnostics are suppressed because myclabs/deep-copy uses spl_object_hash(), which is deprecated since PHP 8.6
2177+
$this->dependencyInput[$dependency] = @$deepCopy->copy($passed[$dependency]['result']);
21772178
} elseif ($shallowClone) {
21782179
$this->dependencyInput[$dependency] = clone $passed[$dependency]['result'];
21792180
} else {

src/Runner/Filter/ExcludeGroupFilterIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717
final class ExcludeGroupFilterIterator extends GroupFilterIterator
1818
{
19-
protected function doAccept(string $hash): bool
19+
protected function doAccept(int $id): bool
2020
{
21-
return !in_array($hash, $this->groupTests, true);
21+
return !in_array($id, $this->groupTests, true);
2222
}
2323
}

src/Runner/Filter/GroupFilterIterator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use function array_map;
1313
use function array_merge;
1414
use function in_array;
15-
use function spl_object_hash;
15+
use function spl_object_id;
1616
use PHPUnit\Framework\TestSuite;
1717
use RecursiveFilterIterator;
1818
use RecursiveIterator;
@@ -23,7 +23,7 @@
2323
abstract class GroupFilterIterator extends RecursiveFilterIterator
2424
{
2525
/**
26-
* @var string[]
26+
* @var int[]
2727
*/
2828
protected $groupTests = [];
2929

@@ -33,12 +33,12 @@ public function __construct(RecursiveIterator $iterator, array $groups, TestSuit
3333

3434
foreach ($suite->getGroupDetails() as $group => $tests) {
3535
if (in_array((string) $group, $groups, true)) {
36-
$testHashes = array_map(
37-
'spl_object_hash',
36+
$testIds = array_map(
37+
'spl_object_id',
3838
$tests
3939
);
4040

41-
$this->groupTests = array_merge($this->groupTests, $testHashes);
41+
$this->groupTests = array_merge($this->groupTests, $testIds);
4242
}
4343
}
4444
}
@@ -51,8 +51,8 @@ public function accept(): bool
5151
return true;
5252
}
5353

54-
return $this->doAccept(spl_object_hash($test));
54+
return $this->doAccept(spl_object_id($test));
5555
}
5656

57-
abstract protected function doAccept(string $hash);
57+
abstract protected function doAccept(int $id);
5858
}

src/Runner/Filter/IncludeGroupFilterIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717
final class IncludeGroupFilterIterator extends GroupFilterIterator
1818
{
19-
protected function doAccept(string $hash): bool
19+
protected function doAccept(int $id): bool
2020
{
21-
return in_array($hash, $this->groupTests, true);
21+
return in_array($id, $this->groupTests, true);
2222
}
2323
}

tests/unit/Framework/Constraint/IsEqualTest.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
namespace PHPUnit\Framework\Constraint;
1111

1212
use function preg_replace;
13-
use function spl_object_hash;
13+
use function spl_object_id;
1414
use DateTime;
1515
use DateTimeZone;
1616
use DOMDocument;
1717
use PHPUnit\Framework\ExpectationFailedException;
1818
use PHPUnit\Framework\TestFailure;
19+
use SebastianBergmann\RecursionContext\Context;
1920
use SplObjectStorage;
2021
use stdClass;
2122

@@ -84,8 +85,10 @@ public function isEqualProvider(): array
8485
$a = new stdClass;
8586
$a->foo = 'bar';
8687
$b = new stdClass;
87-
$ahash = spl_object_hash($a);
88-
$bhash = spl_object_hash($b);
88+
$aid = spl_object_id($a);
89+
$bid = spl_object_id($b);
90+
$ahash = $this->objectIdentifier($a);
91+
$bhash = $this->objectIdentifier($b);
8992

9093
$c = new stdClass;
9194
$c->foo = 'bar';
@@ -109,8 +112,8 @@ public function isEqualProvider(): array
109112
$storage1->offsetSet($b);
110113
$storage2 = new SplObjectStorage;
111114
$storage2->offsetSet($b);
112-
$storage1hash = spl_object_hash($storage1);
113-
$storage2hash = spl_object_hash($storage2);
115+
$storage1hash = $this->objectIdentifier($storage1);
116+
$storage2hash = $this->objectIdentifier($storage2);
114117

115118
$dom1 = new DOMDocument;
116119
$dom1->preserveWhiteSpace = false;
@@ -293,15 +296,15 @@ public function isEqualProvider(): array
293296
+++ Actual
294297
@@ @@
295298
-SplObjectStorage Object &{$storage1hash} (
296-
- '{$ahash}' => Array &0 (
299+
- {$aid} => Array &0 (
297300
- 'obj' => stdClass Object &{$ahash} (
298301
- 'foo' => 'bar'
299302
- )
300303
- 'inf' => null
301304
- )
302-
- '{$bhash}' => Array &1 (
305+
- {$bid} => Array &1 (
303306
+SplObjectStorage Object &{$storage2hash} (
304-
+ '{$bhash}' => Array &0 (
307+
+ {$bid} => Array &0 (
305308
'obj' => stdClass Object &{$bhash} ()
306309
'inf' => null
307310
)
@@ -323,4 +326,20 @@ private function trimnl($string)
323326
{
324327
return preg_replace('/[ ]*\n/', "\n", $string);
325328
}
329+
330+
/**
331+
* Returns the identifier that sebastian/exporter uses for an object.
332+
*
333+
* That identifier is generated by sebastian/recursion-context and is asked
334+
* for here instead of being reproduced so that this test does not depend on
335+
* how it is generated.
336+
*
337+
* @param object $object
338+
*
339+
* @return int|string
340+
*/
341+
private function objectIdentifier($object)
342+
{
343+
return (new Context)->add($object);
344+
}
326345
}

0 commit comments

Comments
 (0)