Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Add support for `BackedEnum` in `MethodMarkingStore`

7.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@
private array $setters = [];

/**
* @param string $property Used to determine methods or property to call
* The `getMarking` method will use `$subject->getProperty()` or `$subject->property`
* The `setMarking` method will use `$subject->setProperty(string|array $places, array $context = [])` or `$subject->property = string|array $places`
* @param string $property Used to determine the accessor methods or the property to call
* `getMarking()` will use `$subject->getProperty()` or `$subject->$property`
* `setMarking()` will use `$subject->setProperty(string|array|\BackedEnum $places, array $context = [])` or `$subject->$property = string|array|\BackedEnum $places`
*/
public function __construct(
private bool $singleState = false,
private string $property = 'marking',
private readonly bool $singleState = false,
private readonly string $property = 'marking',
) {
}

public function getMarking(object $subject): Marking
{
$marking = null;
try {
$marking = ($this->getGetter($subject))();
$marking = ($this->getters[$subject::class] ??= $this->getGetter($subject))();

Check failure on line 54 in src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidPropertyAssignmentValue

src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php:54:25: InvalidPropertyAssignmentValue: $this->getters with declared type 'array<class-string, Symfony\Component\Workflow\MarkingStore\MarkingStoreMethod>' cannot be assigned type 'non-empty-array<class-string, Symfony\Component\Workflow\MarkingStore\MarkingStoreMethod|callable>' (see https://psalm.dev/145)

Check failure on line 54 in src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidPropertyAssignmentValue

src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php:54:25: InvalidPropertyAssignmentValue: $this->getters with declared type 'array<class-string, Symfony\Component\Workflow\MarkingStore\MarkingStoreMethod>' cannot be assigned type 'non-empty-array<class-string, Symfony\Component\Workflow\MarkingStore\MarkingStoreMethod|callable>' (see https://psalm.dev/145)
} catch (\Error $e) {
$unInitializedPropertyMessage = \sprintf('Typed property %s::$%s must not be accessed before initialization', get_debug_type($subject), $this->property);
if ($e->getMessage() !== $unInitializedPropertyMessage) {
Expand All @@ -64,6 +64,10 @@
}

if ($this->singleState) {
if ($marking instanceof \BackedEnum) {
$marking = $marking->value;
}

$marking = [(string) $marking => 1];
} elseif (!\is_array($marking)) {
throw new LogicException(\sprintf('The marking stored in "%s::$%s" is not an array and the Workflow\'s Marking store is instantiated with $singleState=false.', get_debug_type($subject), $this->property));
Expand All @@ -80,15 +84,15 @@
$marking = key($marking);
}

($this->getSetter($subject))($marking, $context);
($this->setters[$subject::class] ??= $this->getSetter($subject))($marking, $context);

Check failure on line 87 in src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidPropertyAssignmentValue

src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php:87:10: InvalidPropertyAssignmentValue: $this->setters with declared type 'array<class-string, Symfony\Component\Workflow\MarkingStore\MarkingStoreMethod>' cannot be assigned type 'non-empty-array<class-string, Symfony\Component\Workflow\MarkingStore\MarkingStoreMethod|callable>' (see https://psalm.dev/145)

Check failure on line 87 in src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidPropertyAssignmentValue

src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php:87:10: InvalidPropertyAssignmentValue: $this->setters with declared type 'array<class-string, Symfony\Component\Workflow\MarkingStore\MarkingStoreMethod>' cannot be assigned type 'non-empty-array<class-string, Symfony\Component\Workflow\MarkingStore\MarkingStoreMethod|callable>' (see https://psalm.dev/145)
}

private function getGetter(object $subject): callable
{
$property = $this->property;
$method = 'get'.ucfirst($property);

return match ($this->getters[$subject::class] ??= self::getType($subject, $property, $method)) {
return match (self::getType($subject, $property, $method)) {
MarkingStoreMethod::METHOD => $subject->{$method}(...),
MarkingStoreMethod::PROPERTY => static fn () => $subject->{$property},
};
Expand All @@ -99,26 +103,34 @@
$property = $this->property;
$method = 'set'.ucfirst($property);

return match ($this->setters[$subject::class] ??= self::getType($subject, $property, $method)) {
MarkingStoreMethod::METHOD => $subject->{$method}(...),
MarkingStoreMethod::PROPERTY => static fn ($marking) => $subject->{$property} = $marking,
return match (self::getType($subject, $property, $method, $type)) {
MarkingStoreMethod::METHOD => $type ? static fn ($marking, $context) => $subject->{$method}($type::from($marking), $context) : $subject->{$method}(...),
MarkingStoreMethod::PROPERTY => $type ? static fn ($marking) => $subject->{$property} = $type::from($marking) : static fn ($marking) => $subject->{$property} = $marking,
};
}

private static function getType(object $subject, string $property, string $method): MarkingStoreMethod
private static function getType(object $subject, string $property, string $method, ?string &$type = null): MarkingStoreMethod
{
if (method_exists($subject, $method) && (new \ReflectionMethod($subject, $method))->isPublic()) {
return MarkingStoreMethod::METHOD;
}

try {
if ((new \ReflectionProperty($subject, $property))->isPublic()) {
return MarkingStoreMethod::PROPERTY;
if (method_exists($subject, $method) && ($r = new \ReflectionMethod($subject, $method))->isPublic()) {
$type = 0 < $r->getNumberOfRequiredParameters() ? $r->getParameters()[0]->getType() : null;

return MarkingStoreMethod::METHOD;
}
} catch (\ReflectionException) {
}

throw new LogicException(\sprintf('Cannot store marking: class "%s" should have either a public method named "%s()" or a public property named "$%s"; none found.', get_debug_type($subject), $method, $property));
try {
if (($r = new \ReflectionProperty($subject, $property))->isPublic()) {
$type = $r->getType();

return MarkingStoreMethod::PROPERTY;
}
} catch (\ReflectionException) {
}

throw new LogicException(\sprintf('Cannot store marking: class "%s" should have either a public method named "%s()" or a public property named "$%s"; none found.', get_debug_type($subject), $method, $property));
} finally {
$type = $type instanceof \ReflectionNamedType && is_subclass_of($type->getName(), \BackedEnum::class) ? $type->getName() : null;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ public function testGetMarkingWithValueObject()
$this->assertSame('first_place', (string) $subject->getMarking());
}

public function testGetMarkingWithBackedEnum()
{
$subject = new SubjectWithEnum(TestEnum::Foo);

$markingStore = new MethodMarkingStore(true);

$marking = $markingStore->getMarking($subject);

$this->assertCount(1, $marking->getPlaces());
$this->assertSame(['foo' => 1], $marking->getPlaces());

$marking->mark('bar');
$marking->unmark('foo');
$markingStore->setMarking($subject, $marking);

$this->assertSame(TestEnum::Bar, $subject->getMarking());
}

public function testGetMarkingWithUninitializedProperty()
{
$subject = new SubjectWithType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 SubjectWithEnum
{
public function __construct(
private ?TestEnum $marking = null,
private array $context = [],
) {
}

public function getMarking(): ?TestEnum
{
return $this->marking;
}

public function setMarking(TestEnum $marking, array $context = []): void
{
$this->marking = $marking;
$this->context = $context;
}

public function getContext(): array
{
return $this->context;
}
}
18 changes: 18 additions & 0 deletions src/Symfony/Component/Workflow/Tests/MarkingStore/TestEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;

enum TestEnum: string
{
case Foo = 'foo';
case Bar = 'bar';
}
Loading