Skip to content

Commit d1d6ada

Browse files
committed
[Workflow] Catch error when trying to get an uninitialized marking
1 parent dc0e8d7 commit d1d6ada

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

MarkingStore/MethodMarkingStore.php

Lines changed: 9 additions & 1 deletion
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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,36 @@ 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+
96+
/**
97+
* @requires PHP 7.4
98+
*/
99+
public function testGetMarkingWithUninitializedProperty2()
100+
{
101+
$subject = new SubjectWithType();
102+
103+
$markingStore = new MethodMarkingStore(true, 'marking2');
104+
105+
$this->expectException(\Error::class);
106+
$this->expectExceptionMessage('Typed property Symfony\Component\Workflow\Tests\MarkingStore\SubjectWithType::$marking must not be accessed before initialization');
107+
108+
$markingStore->getMarking($subject);
109+
}
110+
81111
private function createValueObject(string $markingValue)
82112
{
83113
return new class($markingValue) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Workflow\Tests\MarkingStore;
13+
14+
class SubjectWithType
15+
{
16+
private string $marking;
17+
18+
public function getMarking(): string
19+
{
20+
return $this->marking;
21+
}
22+
23+
public function setMarking(string $type): void
24+
{
25+
$this->marking = $type;
26+
}
27+
28+
public function getMarking2(): string
29+
{
30+
// Typo made on purpose!
31+
return $this->marking;
32+
}
33+
}

0 commit comments

Comments
 (0)