8000 [Workflow] Added DefinitionBuilder::setMetadataStore(). by vudaltsov · Pull Request #27190 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Workflow] Added DefinitionBuilder::setMetadataStore(). #27190

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

Merged
merged 1 commit into from
May 11, 2018
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
1 change: 0 additions & 1 deletion src/Symfony/Component/Workflow/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ final class Definition
/**
* @param string[] $places
* @param Transition[] $transitions
* @param string|null $initialPlace
*/
public function __construct(array $places, array $transitions, string $initialPlace = null, MetadataStoreInterface $metadataStore = null)
{
Expand Down
16 changes: 15 additions & 1 deletion src/Symfony/Component/Workflow/DefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Workflow;

use Symfony\Component\Workflow\Metadata\MetadataStoreInterface;

/**
* Builds a definition.
*
Expand All @@ -23,6 +25,7 @@ class DefinitionBuilder
private $places = array();
private $transitions = array();
private $initialPlace;
private $metadataStore;

/**
* @param string[] $places
Expand All @@ -39,7 +42,7 @@ public function __construct(array $places = array(), array $transitions = array(
*/
public function build()
{
return new Definition($this->places, $this->transitions, $this->initialPlace);
return new Definition($this->places, $this->transitions, $this->initialPlace, $this->metadataStore);
}

/**
Expand All @@ -52,6 +55,7 @@ public function clear()
$this->places = array();
$this->transitions = array();
$this->initialPlace = null;
$this->metadataStore = null;

return $this;
}
Expand Down Expand Up @@ -122,6 +126,16 @@ public function addTransition(Transition $transition)
return $this;
}

/**
* @return $this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use a type hint instead of a php doc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What type hint? : self?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppose NewDefinitionBuilder extends our non-final DefinitionBuilder. Then return type of $newDefinitionBuilder->setMetadataStore() will be DefinitionBuilder, not NewDefinitionBuilder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed. And it's not an issue ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lyrixx But method autocompletion in IDE for methods from the inherited class will stop after a call to a method with self as return type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typehinting fluent pattern with : self is okay in a fluent interface because you are not supposed to add any new public methods in it's implementations. And probably in a final class for the same reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found some relevant occurrences in symfony code:
image
but I still don't agree it is correct...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With : self it is actually correct to do this:

<?php

class A
{
    public function foo(): self
    {
        return $this;
    }
}

class B extends A
{
    public function foo(): A
    {
        return new A();
    }
}

It does not contradict the contract.

While @return $this indicates that the returned value should be the same instance.

Copy link
Member
@nicolas-grekas nicolas-grekas May 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to keep it as this provides more than just the return type.
In the code base, we use both return static and $this, dunno if there is a difference thought here.

*/
public function setMetadataStore(MetadataStoreInterface $metadataStore)
{
$this->metadataStore = $metadataStore;

return $this;
}

/**
* @deprecated since Symfony 4.1, use the clear() method instead.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Workflow/Tests/DefinitionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Workflow\DefinitionBuilder;
use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore;
use Symfony\Component\Workflow\Transition;

class DefinitionBuilderTest extends TestCase
Expand Down Expand Up @@ -44,4 +45,14 @@ public function testAddPlace()
$this->assertEquals('a', $definition->getPlaces()['a']);
$this->assertEquals('b', $definition->getPlaces()['b']);
}

public function testSetMetadataStore()
{
$builder = new DefinitionBuilder(array('a'));
$metadataStore = new InMemoryMetadataStore();
$builder->setMetadataStore($metadataStore);
$definition = $builder->build();

$this->assertSame($metadataStore, $definition->getMetadataStore());
}
}
0