diff --git a/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php b/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php index feb012913cf1..f541d1bf81a7 100644 --- a/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php +++ b/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php @@ -54,7 +54,15 @@ public function getMarking($subject): Marking throw new LogicException(sprintf('The method "%s::%s()" does not exist.', \get_class($subject), $method)); } - $marking = $subject->{$method}(); + $marking = null; + try { + $marking = $subject->{$method}(); + } catch (\Error $e) { + $unInitializedPropertyMassage = sprintf('Typed property %s::$%s must not be accessed before initialization', get_debug_type($subject), $this->property); + if ($e->getMessage() !== $unInitializedPropertyMassage) { + throw $e; + } + } if (null === $marking) { return new Marking(); diff --git a/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php b/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php index 9587f925978c..0ba2254384b0 100644 --- a/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php +++ b/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php @@ -87,6 +87,36 @@ public function testGetMarkingWithValueObject() $this->assertSame('first_place', (string) $subject->getMarking()); } + /** + * @requires PHP 7.4 + */ + public function testGetMarkingWithUninitializedProperty() + { + $subject = new SubjectWithType(); + + $markingStore = new MethodMarkingStore(true); + + $marking = $markingStore->getMarking($subject); + + $this->assertInstanceOf(Marking::class, $marking); + $this->assertCount(0, $marking->getPlaces()); + } + + /** + * @requires PHP 7.4 + */ + public function testGetMarkingWithUninitializedProperty2() + { + $subject = new SubjectWithType(); + + $markingStore = new MethodMarkingStore(true, 'marking2'); + + $this->expectException(\Error::class); + $this->expectExceptionMessage('Typed property Symfony\Component\Workflow\Tests\MarkingStore\SubjectWithType::$marking must not be accessed before initialization'); + + $markingStore->getMarking($subject); + } + private function createValueObject(string $markingValue) { return new class($markingValue) {