Skip to content

Commit 7d3723b

Browse files
Merge branch '7.4' into 8.0
* 7.4: Replace __sleep/wakeup() by __(un)serialize() for throwing and internal usages [TypeInfo] simplify identifier comparison in isIdentifiedBy method
2 parents b71c7d3 + 03a60f1 commit 7d3723b

File tree

10 files changed

+169
-101
lines changed

10 files changed

+169
-101
lines changed

Tests/Fixtures/GoodNight.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarExporter\Tests\Fixtures;
13+
14+
class GoodNight
15+
{
16+
public $good;
17+
protected $foo;
18+
private $bar;
19+
20+
public function __construct()
21+
{
22+
unset($this->good);
23+
$this->foo = 'afternoon';
24+
$this->bar = 'morning';
25+
}
26+
27+
public function __sleep(): array
28+
{
29+
$this->good = 'night';
30+
31+
return ['good', 'foo', "\0*\0foo", "\0".__CLASS__."\0bar"];
32+
}
33+
}

Tests/Fixtures/LazyProxy/TestWakeupClass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class TestWakeupClass extends TestClass
1515
{
16-
public function __wakeup()
16+
public function __wakeup(): void
1717
{
1818
$this->dep->wokeUp = true;
1919
}

Tests/Fixtures/MyWakeup.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarExporter\Tests\Fixtures;
13+
14+
class MyWakeup
15+
{
16+
public $sub;
17+
public $bis;
18+
public $baz;
19+
public $def = 234;
20+
21+
public function __sleep(): array
22+
{
23+
return ['sub', 'baz'];
24+
}
25+
26+
public function __wakeup(): void
27+
{
28+
if (123 === $this->sub) {
29+
$this->bis = 123;
30+
$this->baz = 123;
31+
}
32+
}
33+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarExporter\Tests\Fixtures;
13+
14+
class Php74Serializable implements \Serializable
15+
{
16+
public $foo;
17+
18+
public function __serialize(): array
19+
{
20+
return [$this->foo = new \stdClass()];
21+
}
22+
23+
public function __unserialize(array $data): void
24+
{
25+
[$this->foo] = $data;
26+
}
27+
28+
public function __sleep(): array
29+
{
30+
throw new \BadMethodCallException();
31+
}
32+
33+
public function __wakeup(): void
34+
{
35+
throw new \BadMethodCallException();
36+
}
37+
38+
public function serialize(): string
39+
{
40+
throw new \BadMethodCallException();
41+
}
42+
43+
public function unserialize($ser)
44+
{
45+
throw new \BadMethodCallException();
46+
}
47+
}

Tests/Fixtures/php74-serializable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
44
$o = [
5-
clone (($p = &\Symfony\Component\VarExporter\Internal\Registry::$prototypes)['Symfony\\Component\\VarExporter\\Tests\\Php74Serializable'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\Php74Serializable')),
5+
clone (($p = &\Symfony\Component\VarExporter\Internal\Registry::$prototypes)['Symfony\\Component\\VarExporter\\Tests\\Fixtures\\Php74Serializable'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\Fixtures\\Php74Serializable')),
66
clone ($p['stdClass'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('stdClass')),
77
],
88
null,

Tests/Fixtures/var-on-sleep.php

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

33
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
44
$o = [
5-
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\GoodNight'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\GoodNight')),
5+
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\Fixtures\\GoodNight'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\Fixtures\\GoodNight')),
66
],
77
null,
88
[
@@ -11,7 +11,7 @@
1111
'night',
1212
],
1313
],
14-
'Symfony\\Component\\VarExporter\\Tests\\GoodNight' => [
14+
'Symfony\\Component\\VarExporter\\Tests\\Fixtures\\GoodNight' => [
1515
'foo' => [
1616
'afternoon',
1717
],

Tests/Fixtures/wakeup-refl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
44
$o = [
5-
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\MyWakeup'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\MyWakeup')),
5+
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\Fixtures\\MyWakeup'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\Fixtures\\MyWakeup')),
66
],
77
null,
88
[],

Tests/Fixtures/wakeup.php

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

33
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
44
$o = [
5-
clone (($p = &\Symfony\Component\VarExporter\Internal\Registry::$prototypes)['Symfony\\Component\\VarExporter\\Tests\\MyWakeup'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\MyWakeup')),
6-
clone $p['Symfony\\Component\\VarExporter\\Tests\\MyWakeup'],
5+
clone (($p = &\Symfony\Component\VarExporter\Internal\Registry::$prototypes)['Symfony\\Component\\VarExporter\\Tests\\Fixtures\\MyWakeup'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\Fixtures\\MyWakeup')),
6+
clone $p['Symfony\\Component\\VarExporter\\Tests\\Fixtures\\MyWakeup'],
77
],
88
null,
99
[

Tests/LazyProxyTraitTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@
3131
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestWakeupClass;
3232
use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject;
3333

34+
$errorHandler = set_error_handler(static function (int $errno, string $errstr) use (&$errorHandler) {
35+
if (\E_DEPRECATED === $errno && str_contains($errstr, 'serialize()')) {
36+
// We're testing if the component handles deprecated Serializable and __sleep/wakeup implementations well.
37+
// This kind of implementation triggers a deprecation warning that we explicitly want to ignore here.
38+
return true;
39+
}
40+
41+
return $errorHandler ? $errorHandler(...\func_get_args()) : false;
42+
});
43+
44+
try {
45+
foreach ([
46+
TestWakeupClass::class,
47+
] as $class) {
48+
class_exists($class);
49+
}
50+
} finally {
51+
restore_error_handler();
52+
}
53+
3454
class LazyProxyTraitTest extends TestCase
3555
{
3656
public function testGetter()

Tests/VarExporterTest.php

Lines changed: 29 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,37 @@
2121
use Symfony\Component\VarExporter\Tests\Fixtures\FooReadonly;
2222
use Symfony\Component\VarExporter\Tests\Fixtures\FooSerializable;
2323
use Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum;
24+
use Symfony\Component\VarExporter\Tests\Fixtures\GoodNight;
2425
use Symfony\Component\VarExporter\Tests\Fixtures\MySerializable;
26+
use Symfony\Component\VarExporter\Tests\Fixtures\MyWakeup;
27+
use Symfony\Component\VarExporter\Tests\Fixtures\Php74Serializable;
2528
use Symfony\Component\VarExporter\Tests\Fixtures\PrivateFCC;
2629
use Symfony\Component\VarExporter\VarExporter;
2730

31+
$errorHandler = set_error_handler(static function (int $errno, string $errstr) use (&$errorHandler) {
32+
if (\E_DEPRECATED === $errno && str_contains($errstr, 'serialize()')) {
33+
// We're testing if the component handles deprecated Serializable and __sleep/wakeup implementations well.
34+
// This kind of implementation triggers a deprecation warning that we explicitly want to ignore here.
35+
return true;
36+
}
37+
38+
return $errorHandler ? $errorHandler(...\func_get_args()) : false;
39+
});
40+
41+
try {
42+
foreach ([
43+
MySerializable::class,
44+
FooSerializable::class,
45+
GoodNight::class,
46+
Php74Serializable::class,
47+
MyWakeup::class,
48+
] as $class) {
49+
class_exists($class);
50+
}
51+
} finally {
52+
restore_error_handler();
53+
}
54+
2855
class VarExporterTest extends TestCase
2956
{
3057
use VarDumperTestTrait;
@@ -136,23 +163,8 @@ public static function provideExport()
136163
yield ['array-iterator', new \ArrayIterator([123], 1)];
137164
yield ['array-object-custom', new MyArrayObject([234])];
138165

139-
$errorHandler = set_error_handler(static function (int $errno, string $errstr) use (&$errorHandler) {
140-
if (\E_DEPRECATED === $errno && str_contains($errstr, 'implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead')) {
141-
// We're testing if the component handles deprecated Serializable implementations well.
142-
// This kind of implementation triggers a deprecation warning since PHP 8.1 that we explicitly want to
143-
// ignore here. We probably need to reevaluate this piece of code for PHP 9.
144-
return true;
145-
}
146-
147-
return $errorHandler ? $errorHandler(...\func_get_args()) : false;
148-
});
149-
150-
try {
151-
$mySerializable = new MySerializable();
152-
$fooSerializable = new FooSerializable('bar');
153-
} finally {
154-
restore_error_handler();
155-
}
166+
$mySerializable = new MySerializable();
167+
$fooSerializable = new FooSerializable('bar');
156168

157169
yield ['serializable', [$mySerializable, $mySerializable]];
158170
yield ['foo-serializable', $fooSerializable];
@@ -253,27 +265,6 @@ public function testUnicodeDirectionality()
253265
}
254266
}
255267

256-
class MyWakeup
257-
{
258-
public $sub;
259-
public $bis;
260-
public $baz;
261-
public $def = 234;
262-
263-
public function __sleep(): array
264-
{
265-
return ['sub', 'baz'];
266-
}
267-
268-
public function __wakeup(): void
269-
{
270-
if (123 === $this->sub) {
271-
$this->bis = 123;
272-
$this->baz = 123;
273-
}
274-
}
275-
}
276-
277268
class MyCloneable
278269
{
279270
public function __clone()
@@ -349,27 +340,6 @@ public function setFlags($flags): void
349340
}
350341
}
351342

352-
class GoodNight
353-
{
354-
public $good;
355-
protected $foo;
356-
private $bar;
357-
358-
public function __construct()
359-
{
360-
unset($this->good);
361-
$this->foo = 'afternoon';
362-
$this->bar = 'morning';
363-
}
364-
365-
public function __sleep(): array
366-
{
367-
$this->good = 'night';
368-
369-
return ['good', 'foo', "\0*\0foo", "\0".__CLASS__."\0bar"];
370-
}
371-
}
372-
373343
final class FinalError extends \Error
374344
{
375345
public function __construct(bool $throw = true)
@@ -425,41 +395,6 @@ public function __construct()
425395
}
426396
}
427397

428-
class Php74Serializable implements \Serializable
429-
{
430-
public $foo;
431-
432-
public function __serialize(): array
433-
{
434-
return [$this->foo = new \stdClass()];
435-
}
436-
437-
public function __unserialize(array $data): void
438-
{
439-
[$this->foo] = $data;
440-
}
441-
442-
public function __sleep(): array
443-
{
444-
throw new \BadMethodCallException();
445-
}
446-
447-
public function __wakeup(): void
448-
{
449-
throw new \BadMethodCallException();
450-
}
451-
452-
public function serialize(): string
453-
{
454-
throw new \BadMethodCallException();
455-
}
456-
457-
public function unserialize($ser)
458-
{
459-
throw new \BadMethodCallException();
460-
}
461-
}
462-
463398
#[\AllowDynamicProperties]
464399
class ArrayObject extends \ArrayObject
465400
{

0 commit comments

Comments
 (0)