|null $transitionsMetadata
*/
- public function __construct(array $workflowMetadata = [], array $placesMetadata = [], ?\SplObjectStorage $transitionsMetadata = null)
- {
- $this->workflowMetadata = $workflowMetadata;
- $this->placesMetadata = $placesMetadata;
+ public function __construct(
+ private array $workflowMetadata = [],
+ private array $placesMetadata = [],
+ ?\SplObjectStorage $transitionsMetadata = null,
+ ) {
$this->transitionsMetadata = $transitionsMetadata ?? new \SplObjectStorage();
}
diff --git a/Registry.php b/Registry.php
index 787bc21..08017a3 100644
--- a/Registry.php
+++ b/Registry.php
@@ -49,13 +49,13 @@ public function get(object $subject, ?string $workflowName = null): WorkflowInte
}
if (!$matched) {
- throw new InvalidArgumentException(sprintf('Unable to find a workflow for class "%s".', get_debug_type($subject)));
+ throw new InvalidArgumentException(\sprintf('Unable to find a workflow for class "%s".', get_debug_type($subject)));
}
if (2 <= \count($matched)) {
$names = array_map(static fn (WorkflowInterface $workflow): string => $workflow->getName(), $matched);
- throw new InvalidArgumentException(sprintf('Too many workflows (%s) match this subject (%s); set a different name on each and use the second (name) argument of this method.', implode(', ', $names), get_debug_type($subject)));
+ throw new InvalidArgumentException(\sprintf('Too many workflows (%s) match this subject (%s); set a different name on each and use the second (name) argument of this method.', implode(', ', $names), get_debug_type($subject)));
}
return $matched[0];
diff --git a/SupportStrategy/InstanceOfSupportStrategy.php b/SupportStrategy/InstanceOfSupportStrategy.php
index 86bd107..8d8a4b5 100644
--- a/SupportStrategy/InstanceOfSupportStrategy.php
+++ b/SupportStrategy/InstanceOfSupportStrategy.php
@@ -19,11 +19,9 @@
*/
final class InstanceOfSupportStrategy implements WorkflowSupportStrategyInterface
{
- private string $className;
-
- public function __construct(string $className)
- {
- $this->className = $className;
+ public function __construct(
+ private string $className,
+ ) {
}
public function supports(WorkflowInterface $workflow, object $subject): bool
diff --git a/Tests/Attribute/AsListenerTest.php b/Tests/Attribute/AsListenerTest.php
index a858626..0a8c232 100644
--- a/Tests/Attribute/AsListenerTest.php
+++ b/Tests/Attribute/AsListenerTest.php
@@ -64,7 +64,7 @@ public static function provideOkTests(): iterable
public function testTransitionThrowException(string $class)
{
$this->expectException(LogicException::class);
- $this->expectExceptionMessage(sprintf('The "transition" argument of "%s" cannot be used without a "workflow" argument.', $class));
+ $this->expectExceptionMessage(\sprintf('The "transition" argument of "%s" cannot be used without a "workflow" argument.', $class));
new $class(transition: 'some');
}
@@ -83,7 +83,7 @@ public static function provideTransitionThrowException(): iterable
public function testPlaceThrowException(string $class)
{
$this->expectException(LogicException::class);
- $this->expectExceptionMessage(sprintf('The "place" argument of "%s" cannot be used without a "workflow" argument.', $class));
+ $this->expectExceptionMessage(\sprintf('The "place" argument of "%s" cannot be used without a "workflow" argument.', $class));
new $class(place: 'some');
}
diff --git a/Tests/StateMachineTest.php b/Tests/StateMachineTest.php
index e991707..5d10fde 100644
--- a/Tests/StateMachineTest.php
+++ b/Tests/StateMachineTest.php
@@ -88,7 +88,7 @@ public function testBuildTransitionBlockerListReturnsExpectedReasonOnBranchMerge
$net = new StateMachine($definition, null, $dispatcher);
$dispatcher->addListener('workflow.guard', function (GuardEvent $event) {
- $event->addTransitionBlocker(new TransitionBlocker(sprintf('Transition blocker of place %s', $event->getTransition()->getFroms()[0]), 'blocker'));
+ $event->addTransitionBlocker(new TransitionBlocker(\sprintf('Transition blocker of place %s', $event->getTransition()->getFroms()[0]), 'blocker'));
});
$subject = new Subject();
@@ -124,7 +124,7 @@ public function testApplyReturnsExpectedReasonOnBranchMerge()
$net = new StateMachine($definition, null, $dispatcher);
$dispatcher->addListener('workflow.guard', function (GuardEvent $event) {
- $event->addTransitionBlocker(new TransitionBlocker(sprintf('Transition blocker of place %s', $event->getTransition()->getFroms()[0]), 'blocker'));
+ $event->addTransitionBlocker(new TransitionBlocker(\sprintf('Transition blocker of place %s', $event->getTransition()->getFroms()[0]), 'blocker'));
});
$subject = new Subject();
diff --git a/Tests/WorkflowTest.php b/Tests/WorkflowTest.php
index 83af790..e78530a 100644
--- a/Tests/WorkflowTest.php
+++ b/Tests/WorkflowTest.php
@@ -286,7 +286,7 @@ public function testApplyWithNotEnabledTransition()
$this->fail('Should throw an exception');
} catch (NotEnabledTransitionException $e) {
- $this->assertSame('Transition "t2" is not enabled for workflow "unnamed".', $e->getMessage());
+ $this->assertSame('Cannot apply transition "t2" on workflow "unnamed".', $e->getMessage());
$this->assertCount(1, $e->getTransitionBlockerList());
$list = iterator_to_array($e->getTransitionBlockerList());
$this->assertSame('The marking does not enable the transition.', $list[0]->getMessage());
diff --git a/Transition.php b/Transition.php
index 50d834b..05fe267 100644
--- a/Transition.php
+++ b/Transition.php
@@ -17,7 +17,6 @@
*/
class Transition
{
- private string $name;
private array $froms;
private array $tos;
@@ -25,9 +24,11 @@ class Transition
* @param string|string[] $froms
* @param string|string[] $tos
*/
- public function __construct(string $name, string|array $froms, string|array $tos)
- {
- $this->name = $name;
+ public function __construct(
+ private string $name,
+ string|array $froms,
+ string|array $tos,
+ ) {
$this->froms = (array) $froms;
$this->tos = (array) $tos;
}
diff --git a/TransitionBlocker.php b/TransitionBlocker.php
index 4864598..6a745a2 100644
--- a/TransitionBlocker.php
+++ b/TransitionBlocker.php
@@ -20,21 +20,17 @@ final class TransitionBlocker
public const BLOCKED_BY_EXPRESSION_GUARD_LISTENER = '326a1e9c-0c12-11e8-ba89-0ed5f89f718b';
public const UNKNOWN = 'e8b5bbb9-5913-4b98-bfa6-65dbd228a82a';
- private string $message;
- private string $code;
- private array $parameters;
-
/**
* @param string $code Code is a machine-readable string, usually an UUID
* @param array $parameters This is useful if you would like to pass around the condition values, that
* blocked the transition. E.g. for a condition "distance must be larger than
* 5 miles", you might want to pass around the value of 5.
*/
- public function __construct(string $message, string $code, array $parameters = [])
- {
- $this->message = $message;
- $this->code = $code;
- $this->parameters = $parameters;
+ public function __construct(
+ private string $message,
+ private string $code,
+ private array $parameters = [],
+ ) {
}
/**
diff --git a/Validator/StateMachineValidator.php b/Validator/StateMachineValidator.php
index 521fc88..626a20e 100644
--- a/Validator/StateMachineValidator.php
+++ b/Validator/StateMachineValidator.php
@@ -25,19 +25,19 @@ public function validate(Definition $definition, string $name): void
foreach ($definition->getTransitions() as $transition) {
// Make sure that each transition has exactly one TO
if (1 !== \count($transition->getTos())) {
- throw new InvalidDefinitionException(sprintf('A transition in StateMachine can only have one output. But the transition "%s" in StateMachine "%s" has %d outputs.', $transition->getName(), $name, \count($transition->getTos())));
+ throw new InvalidDefinitionException(\sprintf('A transition in StateMachine can only have one output. But the transition "%s" in StateMachine "%s" has %d outputs.', $transition->getName(), $name, \count($transition->getTos())));
}
// Make sure that each transition has exactly one FROM
$froms = $transition->getFroms();
if (1 !== \count($froms)) {
- throw new InvalidDefinitionException(sprintf('A transition in StateMachine can only have one input. But the transition "%s" in StateMachine "%s" has %d inputs.', $transition->getName(), $name, \count($froms)));
+ throw new InvalidDefinitionException(\sprintf('A transition in StateMachine can only have one input. But the transition "%s" in StateMachine "%s" has %d inputs.', $transition->getName(), $name, \count($froms)));
}
// Enforcing uniqueness of the names of transitions starting at each node
$from = reset($froms);
if (isset($transitionFromNames[$from][$transition->getName()])) {
- throw new InvalidDefinitionException(sprintf('A transition from a place/state must have an unique name. Multiple transitions named "%s" from place/state "%s" were found on StateMachine "%s".', $transition->getName(), $from, $name));
+ throw new InvalidDefinitionException(\sprintf('A transition from a place/state must have an unique name. Multiple transitions named "%s" from place/state "%s" were found on StateMachine "%s".', $transition->getName(), $from, $name));
}
$transitionFromNames[$from][$transition->getName()] = true;
@@ -45,7 +45,7 @@ public function validate(Definition $definition, string $name): void
$initialPlaces = $definition->getInitialPlaces();
if (2 <= \count($initialPlaces)) {
- throw new InvalidDefinitionException(sprintf('The state machine "%s" cannot store many places. But the definition has %d initial places. Only one is supported.', $name, \count($initialPlaces)));
+ throw new InvalidDefinitionException(\sprintf('The state machine "%s" cannot store many places. But the definition has %d initial places. Only one is supported.', $name, \count($initialPlaces)));
}
}
}
diff --git a/Validator/WorkflowValidator.php b/Validator/WorkflowValidator.php
index 2afefb7..f4eb292 100644
--- a/Validator/WorkflowValidator.php
+++ b/Validator/WorkflowValidator.php
@@ -20,11 +20,9 @@
*/
class WorkflowValidator implements DefinitionValidatorInterface
{
- private bool $singlePlace;
-
- public function __construct(bool $singlePlace = false)
- {
- $this->singlePlace = $singlePlace;
+ public function __construct(
+ private bool $singlePlace = false,
+ ) {
}
public function validate(Definition $definition, string $name): void
@@ -34,7 +32,7 @@ public function validate(Definition $definition, string $name): void
foreach ($definition->getTransitions() as $transition) {
foreach ($transition->getFroms() as $from) {
if (\in_array($transition->getName(), $places[$from], true)) {
- throw new InvalidDefinitionException(sprintf('All transitions for a place must have an unique name. Multiple transitions named "%s" where found for place "%s" in workflow "%s".', $transition->getName(), $from, $name));
+ throw new InvalidDefinitionException(\sprintf('All transitions for a place must have an unique name. Multiple transitions named "%s" where found for place "%s" in workflow "%s".', $transition->getName(), $from, $name));
}
$places[$from][] = $transition->getName();
}
@@ -46,13 +44,13 @@ public function validate(Definition $definition, string $name): void
foreach ($definition->getTransitions() as $transition) {
if (1 < \count($transition->getTos())) {
- throw new InvalidDefinitionException(sprintf('The marking store of workflow "%s" cannot store many places. But the transition "%s" has too many output (%d). Only one is accepted.', $name, $transition->getName(), \count($transition->getTos())));
+ throw new InvalidDefinitionException(\sprintf('The marking store of workflow "%s" cannot store many places. But the transition "%s" has too many output (%d). Only one is accepted.', $name, $transition->getName(), \count($transition->getTos())));
}
}
$initialPlaces = $definition->getInitialPlaces();
if (2 <= \count($initialPlaces)) {
- throw new InvalidDefinitionException(sprintf('The marking store of workflow "%s" cannot store many places. But the definition has %d initial places. Only one is supported.', $name, \count($initialPlaces)));
+ throw new InvalidDefinitionException(\sprintf('The marking store of workflow "%s" cannot store many places. But the definition has %d initial places. Only one is supported.', $name, \count($initialPlaces)));
}
}
}
diff --git a/Workflow.php b/Workflow.php
index cfad75f..04b0084 100644
--- a/Workflow.php
+++ b/Workflow.php
@@ -52,28 +52,22 @@ class Workflow implements WorkflowInterface
WorkflowEvents::ANNOUNCE => self::DISABLE_ANNOUNCE_EVENT,
];
- private Definition $definition;
private MarkingStoreInterface $markingStore;
- private ?EventDispatcherInterface $dispatcher;
- private string $name;
/**
- * When `null` fire all events (the default behaviour).
- * Setting this to an empty array `[]` means no events are dispatched (except the {@see GuardEvent}).
- * Passing an array with WorkflowEvents will allow only those events to be dispatched plus
- * the {@see GuardEvent}.
- *
- * @var array|string[]|null
+ * @param array|string[]|null $eventsToDispatch When `null` fire all events (the default behaviour).
+ * Setting this to an empty array `[]` means no events are dispatched (except the {@see GuardEvent}).
+ * Passing an array with WorkflowEvents will allow only those events to be dispatched plus
+ * the {@see GuardEvent}.
*/
- private ?array $eventsToDispatch = null;
-
- public function __construct(Definition $definition, ?MarkingStoreInterface $markingStore = null, ?EventDispatcherInterface $dispatcher = null, string $name = 'unnamed', ?array $eventsToDispatch = null)
- {
- $this->definition = $definition;
+ public function __construct(
+ private Definition $definition,
+ ?MarkingStoreInterface $markingStore = null,
+ private ?EventDispatcherInterface $dispatcher = null,
+ private string $name = 'unnamed',
+ private ?array $eventsToDispatch = null,
+ ) {
$this->markingStore = $markingStore ?? new MethodMarkingStore();
- $this->dispatcher = $dispatcher;
- $this->name = $name;
- $this->eventsToDispatch = $eventsToDispatch;
}
public function getMarking(object $subject, array $context = []): Marking
@@ -83,7 +77,7 @@ public function getMarking(object $subject, array $context = []): Marking
// check if the subject is already in the workflow
if (!$marking->getPlaces()) {
if (!$this->definition->getInitialPlaces()) {
- throw new LogicException(sprintf('The Marking is empty and there is no initial place for workflow "%s".', $this->name));
+ throw new LogicException(\sprintf('The Marking is empty and there is no initial place for workflow "%s".', $this->name));
}
foreach ($this->definition->getInitialPlaces() as $place) {
$marking->mark($place);
@@ -103,7 +97,7 @@ public function getMarking(object $subject, array $context = []): Marking
$places = $this->definition->getPlaces();
foreach ($marking->getPlaces() as $placeName => $nbToken) {
if (!isset($places[$placeName])) {
- $message = sprintf('Place "%s" is not valid for workflow "%s".', $placeName, $this->name);
+ $message = \sprintf('Place "%s" is not valid for workflow "%s".', $placeName, $this->name);
if (!$places) {
$message .= ' It seems you forgot to add places to the current workflow.';
}
@@ -319,8 +313,8 @@ private function guardTransition(object $subject, Marking $marking, Transition $
$event = new GuardEvent($subject, $marking, $transition, $this);
$this->dispatcher->dispatch($event, WorkflowEvents::GUARD);
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.guard', $this->name));
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.guard.%s', $this->name, $transition->getName()));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.guard', $this->name));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.guard.%s', $this->name, $transition->getName()));
return $event;
}
@@ -333,10 +327,10 @@ private function leave(object $subject, Transition $transition, Marking $marking
$event = new LeaveEvent($subject, $marking, $transition, $this, $context);
$this->dispatcher->dispatch($event, WorkflowEvents::LEAVE);
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.leave', $this->name));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.leave', $this->name));
foreach ($places as $place) {
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.leave.%s', $this->name, $place));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.leave.%s', $this->name, $place));
}
}
@@ -354,8 +348,8 @@ private function transition(object $subject, Transition $transition, Marking $ma
$event = new TransitionEvent($subject, $marking, $transition, $this, $context);
$this->dispatcher->dispatch($event, WorkflowEvents::TRANSITION);
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.transition', $this->name));
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.transition.%s', $this->name, $transition->getName()));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.transition', $this->name));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.transition.%s', $this->name, $transition->getName()));
return $event->getContext();
}
@@ -368,10 +362,10 @@ private function enter(object $subject, Transition $transition, Marking $marking
$event = new EnterEvent($subject, $marking, $transition, $this, $context);
$this->dispatcher->dispatch($event, WorkflowEvents::ENTER);
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.enter', $this->name));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.enter', $this->name));
foreach ($places as $place) {
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.enter.%s', $this->name, $place));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.enter.%s', $this->name, $place));
}
}
@@ -389,10 +383,10 @@ private function entered(object $subject, ?Transition $transition, Marking $mark
$event = new EnteredEvent($subject, $marking, $transition, $this, $context);
$this->dispatcher->dispatch($event, WorkflowEvents::ENTERED);
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.entered', $this->name));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.entered', $this->name));
foreach ($marking->getPlaces() as $placeName => $nbToken) {
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.entered.%s', $this->name, $placeName));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.entered.%s', $this->name, $placeName));
}
}
@@ -405,8 +399,8 @@ private function completed(object $subject, Transition $transition, Marking $mar
$event = new CompletedEvent($subject, $marking, $transition, $this, $context);
$this->dispatcher->dispatch($event, WorkflowEvents::COMPLETED);
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.completed', $this->name));
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.completed.%s', $this->name, $transition->getName()));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.completed', $this->name));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.completed.%s', $this->name, $transition->getName()));
}
private function announce(object $subject, Transition $initialTransition, Marking $marking, array $context): void
@@ -418,10 +412,10 @@ private function announce(object $subject, Transition $initialTransition, Markin
$event = new AnnounceEvent($subject, $marking, $initialTransition, $this, $context);
$this->dispatcher->dispatch($event, WorkflowEvents::ANNOUNCE);
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.announce', $this->name));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.announce', $this->name));
foreach ($this->getEnabledTransitions($subject) as $transition) {
- $this->dispatcher->dispatch($event, sprintf('workflow.%s.announce.%s', $this->name, $transition->getName()));
+ $this->dispatcher->dispatch($event, \sprintf('workflow.%s.announce.%s', $this->name, $transition->getName()));
}
}
diff --git a/WorkflowInterface.php b/WorkflowInterface.php
index 8e0faef..6f5bff2 100644
--- a/WorkflowInterface.php
+++ b/WorkflowInterface.php
@@ -12,6 +12,7 @@
namespace Symfony\Component\Workflow;
use Symfony\Component\Workflow\Exception\LogicException;
+use Symfony\Component\Workflow\Exception\UndefinedTransitionException;
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
use Symfony\Component\Workflow\Metadata\MetadataStoreInterface;
@@ -38,6 +39,8 @@ public function can(object $subject, string $transitionName): bool;
/**
* Builds a TransitionBlockerList to know why a transition is blocked.
+ *
+ * @throws UndefinedTransitionException If the transition is not defined
*/
public function buildTransitionBlockerList(object $subject, string $transitionName): TransitionBlockerList;
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy