Skip to content

[Workflow] Added parameter type hinting where possible. #35434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
5.1.0
-----

* Added parameter type hinting where possible.
* Added context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()`
* Added `Registry::has()` to check if a workflow exists
* Added support for `$context[Workflow::DISABLE_ANNOUNCE_EVENT] = true` when calling `workflow->apply()` to not fire the announce event
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Workflow/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function getMetadataStore(): MetadataStoreInterface
return $this->metadataStore;
}

private function setInitialPlaces($places = null)
private function setInitialPlaces($places = null): void
{
if (!$places) {
return;
Expand All @@ -93,7 +93,7 @@ private function setInitialPlaces($places = null)
$this->initialPlaces = $places;
}

private function addPlace(string $place)
private function addPlace(string $place): void
{
if (!\count($this->places)) {
$this->initialPlaces = [$place];
Expand All @@ -102,7 +102,7 @@ private function addPlace(string $place)
$this->places[$place] = $place;
}

private function addTransition(Transition $transition)
private function addTransition(Transition $transition): void
{
$name = $transition->getName();

Expand Down
19 changes: 8 additions & 11 deletions src/Symfony/Component/Workflow/DefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public function __construct(array $places = [], array $transitions = [])
$this->addTransitions($transitions);
}

/**
* @return Definition
*/
public function build()
public function build(): Definition
{
return new Definition($this->places, $this->transitions, $this->initialPlaces, $this->metadataStore);
}
Expand All @@ -50,7 +47,7 @@ public function build()
*
* @return $this
*/
public function clear()
public function clear(): self
{
$this->places = [];
$this->transitions = [];
Expand All @@ -65,7 +62,7 @@ public function clear()
*
* @return $this
*/
public function setInitialPlaces($initialPlaces)
public function setInitialPlaces($initialPlaces): self
{
$this->initialPlaces = $initialPlaces;

Expand All @@ -75,7 +72,7 @@ public function setInitialPlaces($initialPlaces)
/**
* @return $this
*/
public function addPlace(string $place)
public function addPlace(string $place): self
{
if (!$this->places) {
$this->initialPlaces = $place;
Expand All @@ -91,7 +88,7 @@ public function addPlace(string $place)
*
* @return $this
*/
public function addPlaces(array $places)
public function addPlaces(array $places): self
{
foreach ($places as $place) {
$this->addPlace($place);
Expand All @@ -105,7 +102,7 @@ public function addPlaces(array $places)
*
* @return $this
*/
public function addTransitions(array $transitions)
public function addTransitions(array $transitions): self
{
foreach ($transitions as $transition) {
$this->addTransition($transition);
Expand All @@ -117,7 +114,7 @@ public function addTransitions(array $transitions)
/**
* @return $this
*/
public function addTransition(Transition $transition)
public function addTransition(Transition $transition): self
{
$this->transitions[] = $transition;

Expand All @@ -127,7 +124,7 @@ public function addTransition(Transition $transition)
/**
* @return $this
*/
public function setMetadataStore(MetadataStoreInterface $metadataStore)
public function setMetadataStore(MetadataStoreInterface $metadataStore): self
{
$this->metadataStore = $metadataStore;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ public function __construct(LoggerInterface $logger)
$this->logger = $logger;
}

public function onLeave(Event $event)
public function onLeave(Event $event): void
{
foreach ($event->getTransition()->getFroms() as $place) {
$this->logger->info(sprintf('Leaving "%s" for subject of class "%s" in workflow "%s".', $place, \get_class($event->getSubject()), $event->getWorkflowName()));
}
}

public function onTransition(Event $event)
public function onTransition(Event $event): void
{
$this->logger->info(sprintf('Transition "%s" for subject of class "%s" in workflow "%s".', $event->getTransition()->getName(), \get_class($event->getSubject()), $event->getWorkflowName()));
}

public function onEnter(Event $event)
public function onEnter(Event $event): void
{
foreach ($event->getTransition()->getTos() as $place) {
$this->logger->info(sprintf('Entering "%s" for subject of class "%s" in workflow "%s".', $place, \get_class($event->getSubject()), $event->getWorkflowName()));
}
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): iterable
{
return [
'workflow.leave' => ['onLeave'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function __construct(Transition $transition, string $expression)
$this->expression = $expression;
}

public function getTransition()
public function getTransition(): Transition
{
return $this->transition;
}

public function getExpression()
public function getExpression(): string
{
return $this->expression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(array $configuration, ExpressionLanguage $expression
$this->validator = $validator;
}

public function onTransition(GuardEvent $event, string $eventName)
public function onTransition(GuardEvent $event, string $eventName): void
{
if (!isset($this->configuration[$eventName])) {
return;
Expand All @@ -63,7 +63,7 @@ public function onTransition(GuardEvent $event, string $eventName)
}
}

private function validateGuardExpression(GuardEvent $event, string $expression)
private function validateGuardExpression(GuardEvent $event, string $expression): void
{
if (!$this->expressionLanguage->evaluate($expression, $this->getVariables($event))) {
$blocker = TransitionBlocker::createBlockedByExpressionGuardListener($expression);
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Workflow/Marking.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ public function __construct(array $representation = [])
}
}

public function mark(string $place)
public function mark(string $place): void
{
$this->places[$place] = 1;
}

public function unmark(string $place)
public function unmark(string $place): void
{
unset($this->places[$place]);
}

public function has(string $place)
public function has(string $place): bool
{
return isset($this->places[$place]);
}

public function getPlaces()
public function getPlaces(): array
{
return $this->places;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Workflow/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public function __construct(string $name, $froms, $tos)
$this->tos = (array) $tos;
}

public function getName()
public function getName(): string
{
return $this->name;
}

public function getFroms()
public function getFroms(): array
{
return $this->froms;
}

public function getTos()
public function getTos(): array
{
return $this->tos;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class StateMachineValidator implements DefinitionValidatorInterface
{
public function validate(Definition $definition, string $name)
public function validate(Definition $definition, string $name): void
{
$transitionFromNames = [];
foreach ($definition->getTransitions() as $transition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(bool $singlePlace = false)
$this->singlePlace = $singlePlace;
}

public function validate(Definition $definition, string $name)
public function validate(Definition $definition, string $name): void
{
// Make sure all transitions for one place has unique name.
$places = array_fill_keys($definition->getPlaces(), []);
Expand Down
14 changes: 7 additions & 7 deletions src/Symfony/Component/Workflow/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(Definition $definition, MarkingStoreInterface $marki
/**
* {@inheritdoc}
*/
public function getMarking(object $subject)
public function getMarking(object $subject): Marking
{
$marking = $this->markingStore->getMarking($subject);

Expand Down Expand Up @@ -93,7 +93,7 @@ public function getMarking(object $subject)
/**
* {@inheritdoc}
*/
public function can(object $subject, string $transitionName)
public function can(object $subject, string $transitionName): bool
{
$transitions = $this->definition->getTransitions();
$marking = $this->getMarking($subject);
Expand Down Expand Up @@ -152,7 +152,7 @@ public function buildTransitionBlockerList(object $subject, string $transitionNa
/**
* {@inheritdoc}
*/
public function apply(object $subject, string $transitionName, array $context = [])
public function apply(object $subject, string $transitionName, array $context = []): Marking
{
$marking = $this->getMarking($subject);

Expand Down Expand Up @@ -220,7 +220,7 @@ public function apply(object $subject, string $transitionName, array $context =
/**
* {@inheritdoc}
*/
public function getEnabledTransitions(object $subject)
public function getEnabledTransitions(object $subject): iterable
{
$enabledTransitions = [];
$marking = $this->getMarking($subject);
Expand All @@ -238,23 +238,23 @@ public function getEnabledTransitions(object $subject)
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return $this->name;
}

/**
* {@inheritdoc}
*/
public function getDefinition()
public function getDefinition(): Definition
{
return $this->definition;
}

/**
* {@inheritdoc}
*/
public function getMarkingStore()
public function getMarkingStore(): MarkingStoreInterface
{
return $this->markingStore;
}
Expand Down
23 changes: 7 additions & 16 deletions src/Symfony/Component/Workflow/WorkflowInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ interface WorkflowInterface
*
* @throws LogicException
*/
public function getMarking(object $subject);
public function getMarking(object $subject): Marking;

/**
* Returns true if the transition is enabled.
*
* @return bool true if the transition is enabled
*/
public function can(object $subject, string $transitionName);
public function can(object $subject, string $transitionName): bool;

/**
* Builds a TransitionBlockerList to know why a transition is blocked.
Expand All @@ -48,29 +48,20 @@ public function buildTransitionBlockerList(object $subject, string $transitionNa
*
* @throws LogicException If the transition is not applicable
*/
public function apply(object $subject, string $transitionName, array $context = []);
public function apply(object $subject, string $transitionName, array $context = []): Marking;

/**
* Returns all enabled transitions.
*
* @return Transition[] All enabled transitions
*/
public function getEnabledTransitions(object $subject);
public function getEnabledTransitions(object $subject): iterable;

/**
* @return string
*/
public function getName();
public function getName(): string;

/**
* @return Definition
*/
public function getDefinition();
public function getDefinition(): Definition;

/**
* @return MarkingStoreInterface
*/
public function getMarkingStore();
public function getMarkingStore(): MarkingStoreInterface;

public function getMetadataStore(): MetadataStoreInterface;
}