From 141320de529ddf2dff761b0f36f3d7939713cac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Tue, 12 Apr 2022 10:02:23 +0200 Subject: [PATCH] [Workflow] Catch error when trying to get an uninitialized marking --- .../MarkingStore/MethodMarkingStore.php | 10 +++++- .../MarkingStore/MethodMarkingStoreTest.php | 30 +++++++++++++++++ .../Tests/MarkingStore/SubjectWithType.php | 33 +++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/Workflow/Tests/MarkingStore/SubjectWithType.php diff --git a/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php b/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php index feb012913cf1..f541d1bf81a7 100644 --- a/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php +++ b/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php @@ -54,7 +54,15 @@ public function getMarking($subject): Marking throw new LogicException(sprintf('The method "%s::%s()" does not exist.', \get_class($subject), $method)); } - $marking = $subject->{$method}(); + $marking = null; + try { + $marking = $subject->{$method}(); + } catch (\Error $e) { + $unInitializedPropertyMassage = sprintf('Typed property %s::$%s must not be accessed before initialization', get_debug_type($subject), $this->property); + if ($e->getMessage() !== $unInitializedPropertyMassage) { + throw $e; + } + } if (null === $marking) { return new Marking(); diff --git a/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php b/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php index 155f285a4a97..adbdb6f10621 100644 --- a/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php +++ b/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php @@ -78,6 +78,36 @@ public function testGetMarkingWithValueObject() $this->assertSame('first_place', (string) $subject->getMarking()); } + /** + * @requires PHP 7.4 + */ + public function testGetMarkingWithUninitializedProperty() + { + $subject = new SubjectWithType(); + + $markingStore = new MethodMarkingStore(true); + + $marking = $markingStore->getMarking($subject); + + $this->assertInstanceOf(Marking::class, $marking); + $this->assertCount(0, $marking->getPlaces()); + } + + /** + * @requires PHP 7.4 + */ + public function testGetMarkingWithUninitializedProperty2() + { + $subject = new SubjectWithType(); + + $markingStore = new MethodMarkingStore(true, 'marking2'); + + $this->expectException(\Error::class); + $this->expectExceptionMessage('Typed property Symfony\Component\Workflow\Tests\MarkingStore\SubjectWithType::$marking must not be accessed before initialization'); + + $markingStore->getMarking($subject); + } + private function createValueObject(string $markingValue) { return new class($markingValue) { diff --git a/src/Symfony/Component/Workflow/Tests/MarkingStore/SubjectWithType.php b/src/Symfony/Component/Workflow/Tests/MarkingStore/SubjectWithType.php new file mode 100644 index 000000000000..1040dabe0dfb --- /dev/null +++ b/src/Symfony/Component/Workflow/Tests/MarkingStore/SubjectWithType.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Workflow\Tests\MarkingStore; + +class SubjectWithType +{ + private string $marking; + + public function getMarking(): string + { + return $this->marking; + } + + public function setMarking(string $type): void + { + $this->marking = $type; + } + + public function getMarking2(): string + { + // Typo made on purpose! + return $this->marking; + } +}