Skip to content

Commit 983e903

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

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ public function testGetMarkingWithValueObject()
7878
$this->assertSame('first_place', (string) $subject->getMarking());
7979
}
8080

81+
/**
82+
* @requires PHP 7.4
83+
*/
84+
public function testGetMarkingWithUninitializedProperty()
85+
{
86+
$subject = new SubjectWithType();
87+
88+
$markingStore = new MethodMarkingStore(true);
89+
90+
$marking = $markingStore->getMarking($subject);
91+
92+
$this->assertInstanceOf(Marking::class, $marking);
93+
$this->assertCount(0, $marking->getPlaces());
94+
}
95+
8196
private function createValueObject(string $markingValue)
8297
{
8398
return new class($markingValue) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Component\Workflow\Tests\MarkingStore;
4+
5+
class SubjectWithType
6+
{
7+
private string $marking;
8+
9+
public function getMarking(): string
10+
{
11+
return $this->marking;
12+
}
13+
14+
public function setMarking(string $type): void
15+
{
16+
$this->marking = $type;
17+
}
18+
}

0 commit comments

Comments
 (0)