Skip to content

Commit

Permalink
Add support for PHP8 and phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
nguereza-tony committed Jun 18, 2021
1 parent 68f05a3 commit 360426f
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 61 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dist: bionic
language: php
php:
- '7.4'
- '8.0'


before_script:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Platine
Copyright (c) 2020 Platine Event Dispatcher

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

"require-dev": {
"phpmd/phpmd": "@stable",
"phpunit/phpunit": "^9",
"platine-php/test-tools": "1.0.x-dev",
"phpunit/phpunit": "^9.5",
"platine-php/dev": "1.0.x-dev",
"squizlabs/php_codesniffer": "3.*",
"vimeo/psalm": "4.x-dev"
"phpstan/phpstan": "0.12.x-dev"
},

"autoload": {
Expand Down Expand Up @@ -55,11 +55,12 @@

"scripts": {
"test": "phpunit --colors=always",
"static": "psalm",
"static": "phpstan analyze",
"check": "phpcs",
"check-fix": "phpcbf",
"analyze": "phpmd src xml phpmd.xml"
},

"minimum-stability": "dev"
"minimum-stability": "dev",
"prefer-stable": true
}
7 changes: 7 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: 8
paths:
- src
parallel:
jobSize: 50
maximumNumberOfProcesses: 4
6 changes: 4 additions & 2 deletions src/CallableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CallableListener implements ListenerInterface

/**
* The listeners
* @var array
* @var array<$this>
*/
protected static $listeners = [];

Expand Down Expand Up @@ -87,7 +87,8 @@ public function getCallable(): callable
*/
public static function fromCallable(callable $callable): self
{
$listener = new static($callable);
$listener = new self($callable);

return $listener;
}

Expand All @@ -103,6 +104,7 @@ public static function getListener(callable $callable)
return $listener;
}
}

return false;
}

Expand Down
26 changes: 23 additions & 3 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Dispatcher implements DispatcherInterface

/**
* The list of listener
* @var array
* @var array<string, ListenerPriorityQueue>
*/
protected array $listeners = [];

Expand All @@ -67,11 +67,13 @@ public function dispatch($eventName, EventInterface $event = null): void
} elseif (is_null($event)) {
$event = new Event($eventName);
}

if (isset($this->listeners[$event->getName()])) {
foreach ($this->listeners[$event->getName()] as $listener) {
if ($event->isStopPropagation()) {
break;
}

([$listener, 'handle'])($event);
}
}
Expand All @@ -88,15 +90,18 @@ public function addListener(
if (!isset($this->listeners[$eventName])) {
$this->listeners[$eventName] = new ListenerPriorityQueue();
}

if (is_callable($listener)) {
$listener = CallableListener::fromCallable($listener);
}

if (!$listener instanceof ListenerInterface) {
throw new DispatcherException(
'The listener should be the implementation of '
. 'the ListenerInterface or callable'
);
}

$this->listeners[$eventName]->insert($listener, $priority);
}

Expand All @@ -118,9 +123,15 @@ public function removeListener(string $eventName, $listener): void
if (empty($this->listeners[$eventName])) {
return;
}
if (is_callable($listener) && ($listener = CallableListener::getListener($listener)) === false) {

if (is_callable($listener)) {
$listener = CallableListener::getListener($listener);
}

if ($listener === false) {
return;
}

$this->listeners[$eventName]->detach($listener);
}

Expand Down Expand Up @@ -156,9 +167,15 @@ public function hasListener(string $eventName, $listener): bool
if (!isset($this->listeners[$eventName])) {
return false;
}

if (is_callable($listener)) {
$listener = CallableListener::getListener($listener);
}

if ($listener === false) {
return false;
}

return $this->listeners[$eventName]->contains($listener);
}

Expand All @@ -168,12 +185,15 @@ public function hasListener(string $eventName, $listener): bool
public function getListeners(string $eventName = null): array
{
if (!is_null($eventName)) {
return isset($this->listeners[$eventName]) ? $this->listeners[$eventName]->all() : [];
return isset($this->listeners[$eventName])
? $this->listeners[$eventName]->all()
: [];
} else {
$listeners = [];
foreach ($this->listeners as $queue) {
$listeners = array_merge($listeners, $queue->all());
}

return $listeners;
}
}
Expand Down
20 changes: 13 additions & 7 deletions src/DispatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

namespace Platine\Event;

use SplPriorityQueue;

interface DispatcherInterface
{

Expand Down Expand Up @@ -79,17 +81,21 @@ public function dispatch($eventName, EventInterface $event = null): void;
* Register a listener for the given event.
*
* @param string $eventName the name of event
* @param ListenerInterface|callable $listener the ListenerInterface or any callable
* @param ListenerInterface|callable $listener the Listener interface or any callable
* @param int $priority the listener execution priority
*/
public function addListener(string $eventName, $listener, int $priority = self::PRIORITY_DEFAULT): void;
public function addListener(
string $eventName,
$listener,
int $priority = self::PRIORITY_DEFAULT
): void;

/**
* Register a subscriber.
*
* @param subscriberInterface $subscriber the subscriberInterface instance
* @param SubscriberInterface $subscriber the subscriberInterface instance
*/
public function addSubscriber(subscriberInterface $subscriber): void;
public function addSubscriber(SubscriberInterface $subscriber): void;

/**
* Remove a listener for the given event.
Expand All @@ -102,9 +108,9 @@ public function removeListener(string $eventName, $listener): void;
/**
* Remove a subscriber.
*
* @param subscriberInterface $subscriber the subscriberInterface instance
* @param SubscriberInterface $subscriber the subscriberInterface instance
*/
public function removeSubscriber(subscriberInterface $subscriber): void;
public function removeSubscriber(SubscriberInterface $subscriber): void;

/**
* Remove all listener for the given event.
Expand All @@ -127,7 +133,7 @@ public function hasListener(string $eventName, $listener): bool;
* Get all listeners for the given event or all registered listeners.
*
* @param string $eventName the name of event
* @return array
* @return SplPriorityQueue<int, ListenerInterface>[]
*/
public function getListeners(string $eventName = null): array;
}
19 changes: 11 additions & 8 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Event implements EventInterface

/**
* The event data
* @var array
* @var array<string, mixed>
*/
protected array $arguments = [];

Expand All @@ -71,7 +71,7 @@ class Event implements EventInterface
/**
* Create the new instance of the Event
* @param string $name the event name
* @param array $arguments the event data
* @param array<string, mixed> $arguments the event data
*/
public function __construct(string $name, array $arguments = [])
{
Expand Down Expand Up @@ -102,7 +102,7 @@ public function setName(string $name): self

/**
* Get all event data or arguments.
* @return array
* @return array<string, mixed>
*/
public function getArguments(): array
{
Expand All @@ -112,9 +112,9 @@ public function getArguments(): array
/**
* Set array of arguments.
*
* @param array $arguments
* @param array<string, mixed> $arguments
*
* @return self
* @return $this
*/
public function setArguments(array $arguments): self
{
Expand All @@ -125,11 +125,13 @@ public function setArguments(array $arguments): self
/**
* Get event data for the given key.
* @param string $key
* @return mixed
* @return mixed|null
*/
public function getArgument(string $key)
{
return array_key_exists($key, $this->arguments) ? $this->arguments[$key] : null;
return array_key_exists($key, $this->arguments)
? $this->arguments[$key]
: null;
}

/**
Expand All @@ -138,7 +140,7 @@ public function getArgument(string $key)
* @param string $key
* @param mixed $value the event value
*
* @return self
* @return $this
*/
public function setArgument(string $key, $value): self
{
Expand All @@ -160,6 +162,7 @@ public function isStopPropagation(): bool
public function stopPropagation(): self
{
$this->stopPropagation = true;

return $this;
}
}
Loading

0 comments on commit 360426f

Please sign in to comment.