Skip to content

Commit

Permalink
minor #41433 [Workflow] add types to all arguments (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.0 branch.

Discussion
----------

[Workflow] add types to all arguments

| Q             | A
| ------------- | ---
| Branch?       | 6.0
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Extracted from #41424

Commits
-------

6a94b77 [Workflow] add types to all arguments
  • Loading branch information
nicolas-grekas committed May 28, 2021
2 parents 6cbca1e + 6a94b77 commit f7eee41
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/Workflow/Definition.php
Expand Up @@ -32,7 +32,7 @@ final class Definition
* @param Transition[] $transitions
* @param string|string[]|null $initialPlaces
*/
public function __construct(array $places, array $transitions, $initialPlaces = null, MetadataStoreInterface $metadataStore = null)
public function __construct(array $places, array $transitions, string|array|null $initialPlaces = null, MetadataStoreInterface $metadataStore = null)
{
foreach ($places as $place) {
$this->addPlace($place);
Expand Down Expand Up @@ -76,7 +76,7 @@ public function getMetadataStore(): MetadataStoreInterface
return $this->metadataStore;
}

private function setInitialPlaces($places = null)
private function setInitialPlaces(string|array|null $places = null)
{
if (!$places) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Workflow/DefinitionBuilder.php
Expand Up @@ -65,7 +65,7 @@ public function clear()
*
* @return $this
*/
public function setInitialPlaces($initialPlaces)
public function setInitialPlaces(string|array|null $initialPlaces)
{
$this->initialPlaces = $initialPlaces;

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Workflow/Event/Event.php
Expand Up @@ -63,7 +63,7 @@ public function getWorkflowName()
return $this->workflow->getName();
}

public function getMetadata(string $key, $subject)
public function getMetadata(string $key, string|Transition|null $subject)
{
return $this->workflow->getMetadataStore()->getMetadata($key, $subject);
}
Expand Down
23 changes: 3 additions & 20 deletions src/Symfony/Component/Workflow/Metadata/GetMetadataTrait.php
Expand Up @@ -11,38 +11,21 @@

namespace Symfony\Component\Workflow\Metadata;

use Symfony\Component\Workflow\Exception\InvalidArgumentException;
use Symfony\Component\Workflow\Transition;

/**
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
trait GetMetadataTrait
{
public function getMetadata(string $key, $subject = null)
public function getMetadata(string $key, string|Transition|null $subject = null)
{
if (null === $subject) {
return $this->getWorkflowMetadata()[$key] ?? null;
}

if (\is_string($subject)) {
$metadataBag = $this->getPlaceMetadata($subject);
if (!$metadataBag) {
return null;
}
$metadataBag = \is_string($subject) ? $this->getPlaceMetadata($subject) : $this->getTransitionMetadata($subject);

return $metadataBag[$key] ?? null;
}

if ($subject instanceof Transition) {
$metadataBag = $this->getTransitionMetadata($subject);
if (!$metadataBag) {
return null;
}

return $metadataBag[$key] ?? null;
}

throw new InvalidArgumentException(sprintf('Could not find a MetadataBag for the subject of type "%s".', get_debug_type($subject)));
return $metadataBag[$key] ?? null;
}
}
Expand Up @@ -35,5 +35,5 @@ public function getTransitionMetadata(Transition $transition): array;
* Use a string (the place name) to get place metadata
* Use a Transition instance to get transition metadata
*/
public function getMetadata(string $key, $subject = null);
public function getMetadata(string $key, string|Transition|null $subject = null);
}
Expand Up @@ -75,11 +75,4 @@ public function testGetMetadata()
$this->assertNull($this->store->getMetadata('description', $this->transition));
$this->assertNull($this->store->getMetadata('description', new Transition('transition_2', [], [])));
}

public function testGetMetadataWithUnknownType()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Could not find a MetadataBag for the subject of type "bool".');
$this->store->getMetadata('title', true);
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Workflow/Transition.php
Expand Up @@ -25,7 +25,7 @@ class Transition
* @param string|string[] $froms
* @param string|string[] $tos
*/
public function __construct(string $name, $froms, $tos)
public function __construct(string $name, string|array $froms, string|array $tos)
{
$this->name = $name;
$this->froms = (array) $froms;
Expand Down

0 comments on commit f7eee41

Please sign in to comment.