From 46e2ecd5c85d48594f8c9ba3a0ac87832cc15df7 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 13 Nov 2018 22:29:46 +0100 Subject: [PATCH] [VarExporter] fix handling of __sleep() --- .../VarExporter/Internal/Exporter.php | 23 +++++++++---------- .../Tests/Fixtures/wakeup-refl.php | 13 +++++++++++ .../VarExporter/Tests/Fixtures/wakeup.php | 3 --- .../VarExporter/Tests/VarExporterTest.php | 8 +++++++ 4 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/wakeup-refl.php diff --git a/src/Symfony/Component/VarExporter/Internal/Exporter.php b/src/Symfony/Component/VarExporter/Internal/Exporter.php index ee721d31b6aed..bbb409e194c25 100644 --- a/src/Symfony/Component/VarExporter/Internal/Exporter.php +++ b/src/Symfony/Component/VarExporter/Internal/Exporter.php @@ -119,11 +119,10 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount $proto = (array) $proto; foreach ($arrayValue as $name => $v) { + $i = 0; $n = (string) $name; if ('' === $n || "\0" !== $n[0]) { $c = 'stdClass'; - $properties[$c][$n] = $v; - unset($sleep[$n]); } elseif ('*' === $n[1]) { $n = substr($n, 3); $c = $reflector->getProperty($n)->class; @@ -132,26 +131,26 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount } elseif ('Exception' === $c) { $c = 'ErrorException'; } - $properties[$c][$n] = $v; - unset($sleep[$n]); } else { $i = strpos($n, "\0", 2); $c = substr($n, 1, $i - 1); $n = substr($n, 1 + $i); - if (null === $sleep) { - $properties[$c][$n] = $v; - } elseif (isset($sleep[$n]) && $c === $class) { - $properties[$c][$n] = $v; - unset($sleep[$n]); + } + if (null !== $sleep) { + if (!isset($sleep[$n]) || ($i && $c !== $class)) { + continue; } + $sleep[$n] = false; } - if (\array_key_exists($name, $proto) && $proto[$name] === $v) { - unset($properties[$c][$n]); + if (!\array_key_exists($name, $proto) || $proto[$name] !== $v) { + $properties[$c][$n] = $v; } } if ($sleep) { foreach ($sleep as $n => $v) { - trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), E_USER_NOTICE); + if (false !== $v) { + trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), E_USER_NOTICE); + } } } diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/wakeup-refl.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/wakeup-refl.php new file mode 100644 index 0000000000000..1fee610a49de5 --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/wakeup-refl.php @@ -0,0 +1,13 @@ + 0, + ] +); diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/wakeup.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/wakeup.php index 0fe504ef8d6e3..89d4cebb5a8c8 100644 --- a/src/Symfony/Component/VarExporter/Tests/Fixtures/wakeup.php +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/wakeup.php @@ -12,9 +12,6 @@ $o[1], 123, ], - 'bis' => [ - 1 => 123, - ], 'baz' => [ 1 => 123, ], diff --git a/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php b/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php index 2a2eac982b30f..f5096176868ad 100644 --- a/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php +++ b/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php @@ -97,6 +97,9 @@ public function testExport(string $testName, $value, bool $staticValueExpected = $marshalledValue = include $fixtureFile; if (!$isStaticValue) { + if ($value instanceof MyWakeup) { + $value->bis = null; + } $this->assertDumpEquals($value, $marshalledValue); } else { $this->assertSame($value, $marshalledValue); @@ -184,6 +187,11 @@ public function provideExport() yield array('final-array-iterator', new FinalArrayIterator()); yield array('final-stdclass', new FinalStdClass()); + + $value = new MyWakeup(); + $value->bis = new \ReflectionClass($value); + + yield array('wakeup-refl', $value); } }