From 1a036dc9ffb8dc5a7c27c023a1ca3ec4f772dd9c Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 7 Aug 2019 18:46:18 +0200 Subject: [PATCH] [VarExporter] fix support for PHP 7.4 --- .../VarExporter/Internal/Exporter.php | 4 +- .../VarExporter/Internal/Registry.php | 4 +- .../Tests/Fixtures/array-iterator-legacy.php | 22 +++++++++++ .../Tests/Fixtures/array-iterator.php | 23 +++++------- .../Fixtures/array-object-custom-legacy.php | 22 +++++++++++ .../Tests/Fixtures/array-object-custom.php | 21 +++++------ .../Tests/Fixtures/array-object-legacy.php | 29 +++++++++++++++ .../Tests/Fixtures/array-object.php | 37 +++++++++---------- .../Fixtures/final-array-iterator-legacy.php | 11 ++++++ .../Tests/Fixtures/final-array-iterator.php | 14 +++++-- .../Tests/Fixtures/final-error-legacy.php | 27 ++++++++++++++ .../Tests/Fixtures/final-error.php | 11 ++++-- .../Fixtures/spl-object-storage-legacy.php | 21 +++++++++++ .../Tests/Fixtures/spl-object-storage.php | 21 +++++------ .../VarExporter/Tests/VarExporterTest.php | 12 +++--- 15 files changed, 208 insertions(+), 71 deletions(-) create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/array-iterator-legacy.php create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/array-object-custom-legacy.php create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/array-object-legacy.php create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/final-array-iterator-legacy.php create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/final-error-legacy.php create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/spl-object-storage-legacy.php diff --git a/src/Symfony/Component/VarExporter/Internal/Exporter.php b/src/Symfony/Component/VarExporter/Internal/Exporter.php index 87b3156d41..c0b7fa17f4 100644 --- a/src/Symfony/Component/VarExporter/Internal/Exporter.php +++ b/src/Symfony/Component/VarExporter/Internal/Exporter.php @@ -155,7 +155,7 @@ class Exporter } $sleep[$n] = false; } - if (!\array_key_exists($name, $proto) || $proto[$name] !== $v) { + if (!\array_key_exists($name, $proto) || $proto[$name] !== $v || "\x00Error\x00trace" === $name || "\x00Exception\x00trace" === $name) { $properties[$c][$n] = $v; } } @@ -291,7 +291,7 @@ class Exporter continue; } if (!Registry::$instantiableWithoutConstructor[$class]) { - if (is_subclass_of($class, 'Serializable')) { + if (is_subclass_of($class, 'Serializable') && !method_exists($class, '__unserialize')) { $serializables[$k] = 'C:'.\strlen($class).':"'.$class.'":0:{}'; } else { $serializables[$k] = 'O:'.\strlen($class).':"'.$class.'":0:{}'; diff --git a/src/Symfony/Component/VarExporter/Internal/Registry.php b/src/Symfony/Component/VarExporter/Internal/Registry.php index dd2792133e..19d91c9304 100644 --- a/src/Symfony/Component/VarExporter/Internal/Registry.php +++ b/src/Symfony/Component/VarExporter/Internal/Registry.php @@ -75,7 +75,7 @@ class Registry } elseif (!$isClass || $reflector->isAbstract()) { throw new NotInstantiableTypeException($class); } elseif ($reflector->name !== $class) { - $reflector = self::$reflectors[$name = $reflector->name] ?? self::getClassReflector($name, $instantiableWithoutConstructor, $cloneable); + $reflector = self::$reflectors[$name = $reflector->name] ?? self::getClassReflector($name, false, $cloneable); self::$cloneable[$class] = self::$cloneable[$name]; self::$instantiableWithoutConstructor[$class] = self::$instantiableWithoutConstructor[$name]; self::$prototypes[$class] = self::$prototypes[$name]; @@ -86,7 +86,7 @@ class Registry $proto = $reflector->newInstanceWithoutConstructor(); $instantiableWithoutConstructor = true; } catch (\ReflectionException $e) { - $proto = $reflector->implementsInterface('Serializable') && (\PHP_VERSION_ID < 70400 || !method_exists($class, '__unserialize')) ? 'C:' : 'O:'; + $proto = $reflector->implementsInterface('Serializable') && !method_exists($class, '__unserialize') ? 'C:' : 'O:'; if ('C:' === $proto && !$reflector->getMethod('unserialize')->isInternal()) { $proto = null; } elseif (false === $proto = @unserialize($proto.\strlen($class).':"'.$class.'":0:{}')) { diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/array-iterator-legacy.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/array-iterator-legacy.php new file mode 100644 index 0000000000..c59573315d --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/array-iterator-legacy.php @@ -0,0 +1,22 @@ + [ + "\0" => [ + [ + [ + 123, + ], + 1, + ], + ], + ], + ], + $o[0], + [] +); diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/array-iterator.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/array-iterator.php index c59573315d..ed4df00c99 100644 --- a/src/Symfony/Component/VarExporter/Tests/Fixtures/array-iterator.php +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/array-iterator.php @@ -5,18 +5,15 @@ return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate( clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['ArrayIterator'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('ArrayIterator')), ], null, - [ - 'ArrayIterator' => [ - "\0" => [ - [ - [ - 123, - ], - 1, - ], - ], - ], - ], + [], $o[0], - [] + [ + [ + 1, + [ + 123, + ], + [], + ], + ] ); diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object-custom-legacy.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object-custom-legacy.php new file mode 100644 index 0000000000..35303f8222 --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object-custom-legacy.php @@ -0,0 +1,22 @@ + [ + "\0" => [ + [ + [ + 234, + ], + 1, + ], + ], + ], + ], + $o[0], + [] +); diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object-custom.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object-custom.php index 35303f8222..530f0d1026 100644 --- a/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object-custom.php +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object-custom.php @@ -5,18 +5,17 @@ return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate( clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\MyArrayObject'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\MyArrayObject')), ], null, + [], + $o[0], [ - 'ArrayObject' => [ - "\0" => [ - [ - [ - 234, - ], - 1, - ], + [ + 1, + [ + 234, + ], + [ + "\0".'Symfony\\Component\\VarExporter\\Tests\\MyArrayObject'."\0".'unused' => 123, ], ], - ], - $o[0], - [] + ] ); diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object-legacy.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object-legacy.php new file mode 100644 index 0000000000..a461c6ed97 --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object-legacy.php @@ -0,0 +1,29 @@ + [ + "\0" => [ + [ + [ + 1, + $o[0], + ], + 0, + ], + ], + ], + 'stdClass' => [ + 'foo' => [ + $o[1], + ], + ], + ], + $o[0], + [] +); diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object.php index a461c6ed97..e2f349e647 100644 --- a/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object.php +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/array-object.php @@ -6,24 +6,23 @@ return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate( clone $p['ArrayObject'], ], null, - [ - 'ArrayObject' => [ - "\0" => [ - [ - [ - 1, - $o[0], - ], - 0, - ], - ], - ], - 'stdClass' => [ - 'foo' => [ - $o[1], - ], - ], - ], + [], $o[0], - [] + [ + [ + 0, + [ + 1, + $o[0], + ], + [ + 'foo' => $o[1], + ], + ], + -1 => [ + 0, + [], + [], + ], + ] ); diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/final-array-iterator-legacy.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/final-array-iterator-legacy.php new file mode 100644 index 0000000000..9bdb2b3662 --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/final-array-iterator-legacy.php @@ -0,0 +1,11 @@ + [ + 'file' => [ + \dirname(__DIR__).\DIRECTORY_SEPARATOR.'VarExporterTest.php', + ], + 'line' => [ + 123, + ], + ], + 'Error' => [ + 'trace' => [ + [], + ], + ], + ], + $o[0], + [ + 1 => 0, + ] +); diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/final-error.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/final-error.php index dc260dc024..b2e729937b 100644 --- a/src/Symfony/Component/VarExporter/Tests/Fixtures/final-error.php +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/final-error.php @@ -1,9 +1,9 @@ [ @@ -14,6 +14,11 @@ return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate( 123, ], ], + 'Error' => [ + 'trace' => [ + [], + ], + ], ], $o[0], [ diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/spl-object-storage-legacy.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/spl-object-storage-legacy.php new file mode 100644 index 0000000000..5e854a4959 --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/spl-object-storage-legacy.php @@ -0,0 +1,21 @@ + [ + "\0" => [ + [ + $o[1], + 345, + ], + ], + ], + ], + $o[0], + [] +); diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/spl-object-storage.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/spl-object-storage.php index 5e854a4959..023a75fdcd 100644 --- a/src/Symfony/Component/VarExporter/Tests/Fixtures/spl-object-storage.php +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/spl-object-storage.php @@ -6,16 +6,15 @@ return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate( clone ($p['stdClass'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('stdClass')), ], null, - [ - 'SplObjectStorage' => [ - "\0" => [ - [ - $o[1], - 345, - ], - ], - ], - ], + [], $o[0], - [] + [ + [ + [ + $o[1], + 345, + ], + [], + ], + ] ); diff --git a/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php b/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php index 8a70d02be5..d80c2858ee 100644 --- a/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php +++ b/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php @@ -12,7 +12,6 @@ namespace Symfony\Component\VarExporter\Tests; use PHPUnit\Framework\TestCase; -use PHPUnit\Framework\Warning; use Symfony\Component\VarDumper\Test\VarDumperTestTrait; use Symfony\Component\VarExporter\Internal\Registry; use Symfony\Component\VarExporter\VarExporter; @@ -76,10 +75,6 @@ class VarExporterTest extends TestCase */ public function testExport(string $testName, $value, bool $staticValueExpected = false) { - if (\PHP_VERSION_ID >= 70400 && \in_array($testName, ['spl-object-storage', 'array-object-custom', 'array-iterator', 'array-object', 'final-array-iterator'])) { - throw new Warning('PHP 7.4 breaks this test.'); - } - $dumpedValue = $this->getDump($value); $isStaticValue = true; $marshalledValue = VarExporter::export($value, $isStaticValue); @@ -91,7 +86,12 @@ class VarExporterTest extends TestCase $dump = "assertStringEqualsFile($fixtureFile, $dump); if ('incomplete-class' === $testName || 'external-references' === $testName) {