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

Skip to content

Commit 3098340

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

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

src/Symfony/Component/Workflow/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();

src/Symfony/Component/Workflow/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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
19+
public function getMarking2(): string
20+
{
21+
// Typo made on purpose!
22+
return $this->marking;
23+
}
24+
}

0 commit comments

Comments
 (0)