Skip to content

Commit

Permalink
- Remove all redundant document block comments and types
Browse files Browse the repository at this point in the history
- Use more specific method and property typehints
- Use constructor property promotion where possible
  • Loading branch information
alex-patterson-webdev committed May 7, 2023
1 parent 25d04c0 commit b3842e5
Show file tree
Hide file tree
Showing 35 changed files with 101 additions and 812 deletions.
22 changes: 3 additions & 19 deletions src/AbstractEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,14 @@
use Psr\EventDispatcher\ListenerProviderInterface;
use Psr\EventDispatcher\StoppableEventInterface;

/**
* @author Alex Patterson <[email protected]>
* @package Arp\EventDispatcher
*/
abstract class AbstractEventDispatcher implements EventDispatcherInterface
{
/**
* @return ListenerProviderInterface
*/
abstract protected function getListenerProvider(): ListenerProviderInterface;

/**
* Trigger the registered collection of events.
*
* @param object|StoppableEventInterface $event The event that should be triggered.
* @param object|StoppableEventInterface $event
*
* @return object
*
* @throws \Throwable If an event listener throws an exception
* @throws \Throwable
*/
public function dispatch(object $event): object
{
Expand All @@ -37,7 +26,6 @@ public function dispatch(object $event): object
foreach ($this->getListenerProvider()->getListenersForEvent($event) as $listener) {
$listener($event);

/** @phpstan-ignore-next-line isPropagationStopped() can be modified by reference */
if ($this->isPropagationStopped($event)) {
break;
}
Expand All @@ -47,11 +35,7 @@ public function dispatch(object $event): object
}

/**
* Check if the event propagation has been stopped.
*
* @param object $event
*
* @return bool
* @param object|StoppableEventInterface $event
*/
protected function isPropagationStopped(object $event): bool
{
Expand Down
8 changes: 2 additions & 6 deletions src/Event/AbstractEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@

namespace Arp\EventDispatcher\Event;

/**
* @author Alex Patterson <[email protected]>
* @package Arp\EventDispatcher\Event
*/
abstract class AbstractEvent implements ParametersAwareInterface
{
use ParametersAwareTrait;

/**
* @param ParametersInterface<mixed>|array<mixed>|mixed $params
* @param ParametersInterface<mixed>|iterable<mixed> $params
*/
public function __construct($params = [])
public function __construct(iterable $params = [])
{
if (!$params instanceof ParametersInterface) {
$params = new Parameters(is_array($params) ? $params : []);
Expand Down
93 changes: 7 additions & 86 deletions src/Event/ImmutableParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,168 +4,89 @@

namespace Arp\EventDispatcher\Event;

/**
* A parameter collection that is closed for modifications
*
* @author Alex Patterson <[email protected]>
* @package Arp\EventDispatcher\Event
*/
final class ImmutableParameters implements ParametersInterface
{
/**
* @var ParametersInterface
*/
private ParametersInterface $parameters;

/**
* @param ParametersInterface $parameters
*/
public function __construct(ParametersInterface $parameters)
public function __construct(private readonly ParametersInterface $parameters)
{
$this->parameters = $parameters;
}

/**
* @return int
*/
public function count(): int
{
return $this->parameters->count();
}

/**
* @return bool
*/
public function isEmpty(): bool
{
return $this->parameters->isEmpty();
}


/**
* @param string $name
*
* @return bool
*/
public function hasParam(string $name): bool
{
return $this->parameters->hasParam($name);
}

/**
* @param string $name
* @param mixed $default
*
* @return mixed
*/
public function getParam(string $name, $default = null)
public function getParam(string $name, mixed $default = null): mixed
{
return $this->parameters->getParam($name, $default);
}

/**
* @return array<mixed>
*/
public function getParams(): array
{
return $this->parameters->getParams();
}

/**
* @param array<mixed> $params
*/
public function setParams(array $params): void
{
// Ignore any updates to parameters to respect immutability
}

/**
* @param string $name
* @param mixed $value
*/
public function setParam(string $name, $value): void
public function setParam(string $name, mixed $value): void
{
// Ignore any updates to parameters to respect immutability
}

/**
* @param array<mixed> $params
*/
public function removeParams(array $params = []): void
{
// Ignore any updates to parameters to respect immutability
}

/**
* @param string $name
*
* @return bool
*/
public function removeParam(string $name): bool
{
// Ignore any updates to parameters to respect immutability
return false;
}

/**
* @return array<int, mixed>
*/
public function getKeys(): array
{
return $this->parameters->getKeys();
}

/**
* @return array<mixed>
*/
public function getValues(): array
{
return $this->parameters->getValues();
}

/**
* @return \Traversable<mixed>
*
* @throws \Exception
*/
public function getIterator(): \Traversable
{
return $this->parameters->getIterator();
}

/**
* @param mixed $offset
*
* @return bool
*/
public function offsetExists($offset): bool
public function offsetExists(mixed $offset): bool
{
return $this->parameters->offsetExists($offset);
}

/**
* @param mixed $offset
*
* @return mixed
*/
public function offsetGet($offset)
public function offsetGet(mixed $offset): mixed
{
return $this->parameters->offsetGet($offset);
}

/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value): void
public function offsetSet(mixed $offset, mixed $value): void
{
// Ignore any updates to parameters to respect immutability
}

/**
* @param mixed $offset
*/
public function offsetUnset($offset): void
public function offsetUnset(mixed $offset): void
{
// Ignore any updates to parameters to respect immutability
}
Expand Down
16 changes: 0 additions & 16 deletions src/Event/NamedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,17 @@

use Arp\EventDispatcher\Resolver\EventNameAwareInterface;

/**
* @author Alex Patterson <[email protected]>
* @package Arp\EventDispatcher\Event
*/
class NamedEvent extends AbstractEvent implements EventNameAwareInterface
{
/**
* @var string
*/
protected string $eventName;

/**
* @param string $eventName
* @param array<mixed> $params
*/
public function __construct(string $eventName, array $params = [])
{
parent::__construct($params);

$this->eventName = $eventName;
}

/**
* Return the event name
*
* @return string
*/
public function getEventName(): string
{
return $this->eventName;
Expand Down
Loading

0 comments on commit b3842e5

Please sign in to comment.