8000 [VarExporter] Uniform unitialized property error message under ghost … · priyadi/symfony@fa3a112 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa3a112

Browse files
priyadinicolas-grekas
authored andcommitted
[VarExporter] Uniform unitialized property error message under ghost and non-ghost objects
1 parent cff8011 commit fa3a112

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/Symfony/Component/VarExporter/LazyGhostTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ public function &__get($name): mixed
223223

224224
return $accessor['get']($this, $name, null !== $readonlyScope);
225225
} catch (\Error) {
226+
if (preg_match('/^Cannot access uninitialized non-nullable property ([^ ]++) by reference$/', $e->getMessage(), $matches)) {
227+
throw new \Error('Typed property '.$matches[1].' must not be accessed before initialization', $e->getCode(), $e->getPrevious());
228+
}
229+
226230
throw $e;
227231
}
228232
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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\LazyGhost;
13+
14+
class ClassWithUninitializedObjectProperty
15+
{
16+
public \DateTimeInterface $property;
17+
}

src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildMagicClass;
1818
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildStdClass;
1919
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildTestClass;
20+
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ClassWithUninitializedObjectProperty;
2021
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\LazyClass;
2122
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\MagicClass;
2223
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ReadOnlyClass;
@@ -438,6 +439,28 @@ public function testReadOnlyClass()
438439
$this->assertSame(123, $proxy->foo);
439440
}
440441

442+
public function testAccessingUninializedPropertyWithoutLazyGhost()
443+
{
444+
$object = new ClassWithUninitializedObjectProperty();
445+
446+
$this->expectException(\Error::class);
447+
$this->expectExceptionCode(0);
448+
$this->expectExceptionMessage('Typed property Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ClassWithUninitializedObjectProperty::$property must not be accessed before initialization');
449+
450+
$object->property;
451+
}
452+
453+
public function testAccessingUninializedPropertyWithLazyGhost()
454+
{
455+
$object = $this->createLazyGhost(ClassWithUninitializedObjectProperty::class, function ($instance) {});
456+
457+
$this->expectException(\Error::class);
458+
$this->expectExceptionCode(0);
459+
$this->expectExceptionMessage('Typed property Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ClassWithUninitializedObjectProperty::$property must not be accessed before initialization');
460+
461+
$object->property;
462+
}
463+
441464
/**
442465
* @template T
443466
*

0 commit comments

Comments
 (0)
0