Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 014c7ec

Browse files
committed
[Workflow] Catch error when trying to get an uninitialized marking
1 parent 2607b66 commit 014c7ec

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ public function getMarking($subject): Marking
5454
throw new LogicException(sprintf('The method "%s::%s()" does not exist.', \get_class($subject), $method));
5555
}
5656

57-
$marking = $subject->{$method}();
57+
$marking = null;
58+
try {
59+
$marking = $subject->{$method}();
60+
} catch (\Error $e) {
61+
if (!preg_match('/^Typed property ([\w\\\\@]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage())) {
62+
throw $e;
63+
}
64+
}
5865

5966
if (null === $marking) {
6067
return new Marking();

src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ public function testGetMarkingWithValueObject()
7878
$this->assertSame('first_place', (string) $subject->getMarking());
7979
}
8080

81+
public function testGetMarkingWithUninitializedProperty()
82+
{
83+
$subject = new class() {
84+
private string $marking;
85+
86+
public function getMarking(): string
87+
{
88+
return $this->marking;
89+
}
90+
};
91+
92+
$markingStore = new MethodMarkingStore(true);
93+
94+
$marking = $markingStore->getMarking($subject);
95+
96+
$this->assertInstanceOf(Marking::class, $marking);
97+
$this->assertCount(0, $marking->getPlaces());
98+
}
99+
81100
private function createValueObject(string $markingValue)
82101
{
83102
return new class($markingValue) {

0 commit comments

Comments
 (0)