Skip to content

Commit cb0e930

Browse files
bug #46008 [Workflow] Catch error when trying to get an uninitialized marking (lyrixx)
This PR was merged into the 4.4 branch. Discussion ---------- [Workflow] Catch error when trying to get an uninitialized marking | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #44213 | License | MIT | Doc PR | Commits ------- 141320de52 [Workflow] Catch error when trying to get an uninitialized marking
2 parents 3edf208 + d1d6ada commit cb0e930

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

MarkingStore/MethodMarkingStore.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@ 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+
$unInitializedPropertyMassage = sprintf('Typed property %s::$%s must not be accessed before initialization', get_debug_type($subject), $this->property);
62+
if ($e->getMessage() !== $unInitializedPropertyMassage) {
63+
throw $e;
64+
}
65+
}
5866

5967
if (null === $marking) {
6068
return new Marking();

Tests/MarkingStore/MethodMarkingStoreTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,36 @@ public function testGetMarkingWithValueObject()
8787
$this->assertSame('first_place', (string) $subject->getMarking());
8888
}
8989

90+
/**
91+
* @requires PHP 7.4
92+
*/
93+
public function testGetMarkingWithUninitializedProperty()
94+
{
95+
$subject = new SubjectWithType();
96+
97+
$markingStore = new MethodMarkingStore(true);
98+
99+
$marking = $markingStore->getMarking($subject);
100+
101+
$this->assertInstanceOf(Marking::class, $marking);
102+
$this->assertCount(0, $marking->getPlaces());
103+
}
104+
105+
/**
106+
* @requires PHP 7.4
107+
*/
108+
public function testGetMarkingWithUninitializedProperty2()
109+
{
110+
$subject = new SubjectWithType();
111+
112+
$markingStore = new MethodMarkingStore(true, 'marking2');
113+
114+
$this->expectException(\Error::class);
115+
$this->expectExceptionMessage('Typed property Symfony\Component\Workflow\Tests\MarkingStore\SubjectWithType::$marking must not be accessed before initialization');
116+
117+
$markingStore->getMarking($subject);
118+
}
119+
90120
private function createValueObject(string $markingValue)
91121
{
92122
return new class($markingValue) {

0 commit comments

Comments
 (0)