Skip to content

Commit

Permalink
Remove legacy Symfony eventdispatcher selection (#1464)
Browse files Browse the repository at this point in the history
We should only need to rely on the new signature
  • Loading branch information
ciaranmcnulty committed Jan 24, 2024
1 parent 250659c commit 3ef6c04
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 41 deletions.
6 changes: 0 additions & 6 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
<MissingParamType><errorLevel type="error"/></MissingParamType>
<MissingReturnType><errorLevel type="error"/></MissingReturnType>
<MissingPropertyType><errorLevel type="error"/></MissingPropertyType>
<DuplicateClass>
<errorLevel type="suppress">
<!-- duplicate classes are inside a conditional so not an error -->
<file name="src/PhpSpec/Event/BaseEvent.php" />
</errorLevel>
</DuplicateClass>
<UndefinedConstant>
<errorLevel type="suppress">
<!-- new token constants in PHP 8 cause an error, despite being behind version detection -->
Expand Down
12 changes: 2 additions & 10 deletions src/PhpSpec/Event/BaseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@

namespace PhpSpec\Event;

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\Event as OldEvent;
use Symfony\Contracts\EventDispatcher\Event as ContractEvent;

if (\is_subclass_of(EventDispatcher::class, EventDispatcherInterface::class)) {
class BaseEvent extends ContractEvent
{
}
} else {
class BaseEvent extends OldEvent
{
}
class BaseEvent extends ContractEvent
{
}
2 changes: 1 addition & 1 deletion src/PhpSpec/Event/ExpectationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getSuite(): Suite
return $this->example->getSpecification()->getSuite();
}

public function getSubject()
public function getSubject() : mixed
{
return $this->subject;
}
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpec/Loader/Node/ExampleNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class ExampleNode
{
private ?SpecificationNode $specification;
private SpecificationNode $specification;

private bool $isPending = false;

Expand Down Expand Up @@ -58,7 +58,7 @@ public function setSpecification(SpecificationNode $specification): void
$this->specification = $specification;
}

public function getSpecification() : ?SpecificationNode
public function getSpecification() : SpecificationNode
{
return $this->specification;
}
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpec/Loader/Node/SpecificationNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class SpecificationNode implements \Countable
{
private ?Suite $suite;
private Suite $suite;

/**
* @var ExampleNode[]
Expand Down Expand Up @@ -68,7 +68,7 @@ public function setSuite(Suite $suite) : void
$this->suite = $suite;
}

public function getSuite() : ?Suite
public function getSuite() : Suite
{
return $this->suite;
}
Expand Down
21 changes: 1 addition & 20 deletions src/PhpSpec/Util/DispatchTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,13 @@

namespace PhpSpec\Util;

use PhpSpec\Wrapper\Collaborator;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @internal
*/
trait DispatchTrait
{
private function dispatch(object $eventDispatcher, object $event, string $eventName) : object
{
if ($this->isNewSymfonyContract($eventDispatcher)) {
return $eventDispatcher->dispatch($event, $eventName);
}

return $eventDispatcher->dispatch($eventName, $event);
}

private function isNewSymfonyContract(object $eventDispatcher): bool
{
// This trait may be used with a double, in the tests
if ($eventDispatcher instanceof Collaborator) {
$eventDispatcher = $eventDispatcher->getWrappedObject();
}

// EventDispatcherInterface contract implemented in Symfony >= 4.3
return $eventDispatcher instanceof EventDispatcherInterface;
return $eventDispatcher->dispatch($event, $eventName);
}
}

0 comments on commit 3ef6c04

Please sign in to comment.