From fa3a112861e52f34716316f63904b00ad1a3a841 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Sat, 10 Feb 2024 10:50:56 +0700 Subject: [PATCH] [VarExporter] Uniform unitialized property error message under ghost and non-ghost objects --- .../Component/VarExporter/LazyGhostTrait.php | 4 ++++ .../ClassWithUninitializedObjectProperty.php | 17 ++++++++++++++ .../VarExporter/Tests/LazyGhostTraitTest.php | 23 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/LazyGhost/ClassWithUninitializedObjectProperty.php diff --git a/src/Symfony/Component/VarExporter/LazyGhostTrait.php b/src/Symfony/Component/VarExporter/LazyGhostTrait.php index 30787dd5abe19..ab8a8e87b47f8 100644 --- a/src/Symfony/Component/VarExporter/LazyGhostTrait.php +++ b/src/Symfony/Component/VarExporter/LazyGhostTrait.php @@ -223,6 +223,10 @@ public function &__get($name): mixed return $accessor['get']($this, $name, null !== $readonlyScope); } catch (\Error) { + if (preg_match('/^Cannot access uninitialized non-nullable property ([^ ]++) by reference$/', $e->getMessage(), $matches)) { + throw new \Error('Typed property '.$matches[1].' must not be accessed before initialization', $e->getCode(), $e->getPrevious()); + } + throw $e; } } diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/LazyGhost/ClassWithUninitializedObjectProperty.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/LazyGhost/ClassWithUninitializedObjectProperty.php new file mode 100644 index 0000000000000..352810c34ba1e --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/LazyGhost/ClassWithUninitializedObjectProperty.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost; + +class ClassWithUninitializedObjectProperty +{ + public \DateTimeInterface $property; +} diff --git a/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php b/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php index 83257bdfdeeac..be554b953287e 100644 --- a/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php +++ b/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php @@ -17,6 +17,7 @@ use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildMagicClass; use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildStdClass; use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildTestClass; +use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ClassWithUninitializedObjectProperty; use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\LazyClass; use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\MagicClass; use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ReadOnlyClass; @@ -438,6 +439,28 @@ public function testReadOnlyClass() $this->assertSame(123, $proxy->foo); } + public function testAccessingUninializedPropertyWithoutLazyGhost() + { + $object = new ClassWithUninitializedObjectProperty(); + + $this->expectException(\Error::class); + $this->expectExceptionCode(0); + $this->expectExceptionMessage('Typed property Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ClassWithUninitializedObjectProperty::$property must not be accessed before initialization'); + + $object->property; + } + + public function testAccessingUninializedPropertyWithLazyGhost() + { + $object = $this->createLazyGhost(ClassWithUninitializedObjectProperty::class, function ($instance) {}); + + $this->expectException(\Error::class); + $this->expectExceptionCode(0); + $this->expectExceptionMessage('Typed property Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ClassWithUninitializedObjectProperty::$property must not be accessed before initialization'); + + $object->property; + } + /** * @template T *