From c0dc2bc27debe4a34472e200d79173c853e9ad04 Mon Sep 17 00:00:00 2001 From: Robin de Graaf Date: Thu, 11 Mar 2021 13:22:32 +0000 Subject: [PATCH] Rename EventManager to Events, exception too, add psalm --- .github/workflows/tests.yml | 3 + .gitignore | 3 +- CHANGELOG.md | 7 ++ Makefile | 8 ++- README.md | 6 +- composer.json | 3 +- psalm.xml | 19 +++++ src/{EventManager.php => Events.php} | 10 ++- src/{Exception.php => EventsException.php} | 2 +- tests/EventManagerTest.php | 84 ---------------------- tests/EventsTest.php | 84 ++++++++++++++++++++++ 11 files changed, 131 insertions(+), 98 deletions(-) create mode 100644 psalm.xml rename src/{EventManager.php => Events.php} (89%) rename src/{Exception.php => EventsException.php} (60%) delete mode 100644 tests/EventManagerTest.php create mode 100644 tests/EventsTest.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5d3a185..d181c60 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -32,3 +32,6 @@ jobs: - name: Run test suite run: vendor/bin/phpunit tests + + - name: Run static analysis + run: vendor/bin/psalm diff --git a/.gitignore b/.gitignore index 41f9a33..9c59449 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ +.DS_Store .idea composer.lock vendor -coverage \ No newline at end of file +coverage diff --git a/CHANGELOG.md b/CHANGELOG.md index 49e2b41..5493ec3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Parable Event +## 0.4.0 + +_Changes_ +- Add static analysis using psalm. +- `EventManager` has been renamed to `Events`. +- `Exception` has been renamed to `EventsException` for clarity. + ## 0.3.0 _Changes_ diff --git a/Makefile b/Makefile index 79c8f34..289792f 100644 --- a/Makefile +++ b/Makefile @@ -4,16 +4,20 @@ dependencies: --no-plugins \ --no-scripts +psalm: + vendor/bin/psalm --clear-cache + vendor/bin/psalm + tests: dependencies vendor/bin/phpunit --verbose tests coverage: dependencies rm -rf ./coverage - vendor/bin/phpunit --coverage-html ./coverage tests + XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests tests-clean: vendor/bin/phpunit --verbose tests coverage-clean: rm -rf ./coverage - vendor/bin/phpunit --coverage-html ./coverage tests \ No newline at end of file + XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests diff --git a/README.md b/README.md index da26171..6f8afdd 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,12 @@ $ composer require parable-php/event ## Usage Events are very simple. You add listeners to events (`string` values) and then trigger an update with those events. You -can pass payloads into the `trigger` calls, which will get passed to all relevant listeners. +can pass payloads into the `trigger` calls, which will get passed to all relevant listeners. ```php -use \Parable\Event\EventManager; +use \Parable\Event\Events; -$eventManager = new EventManager(); +$eventManager = new Events(); $eventManager->listen('event_number_one', function (string $event, string &$payload) { $payload .= '-updated!'; diff --git a/composer.json b/composer.json index ebbb991..819f547 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "php": ">=8.0" }, "require-dev": { - "phpunit/phpunit": "^8.0" + "phpunit/phpunit": "^8.0", + "vimeo/psalm": "^4.6" }, "autoload": { "psr-4": { diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..8679d7a --- /dev/null +++ b/psalm.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + diff --git a/src/EventManager.php b/src/Events.php similarity index 89% rename from src/EventManager.php rename to src/Events.php index 25fd28d..1025ba9 100644 --- a/src/EventManager.php +++ b/src/Events.php @@ -2,13 +2,11 @@ namespace Parable\Event; -class EventManager +class Events { protected const GLOBAL_EVENT = '*'; - /** - * @var callable[][] - */ + /** @var callable[][] */ protected array $listeners = []; public function listen(string $event, callable $listener): void @@ -33,7 +31,7 @@ public function listenAll(callable $listener): void public function trigger(string $event, &$payload = null) { if ($event === self::GLOBAL_EVENT) { - throw new Exception('Cannot specifically trigger global event.'); + throw new EventsException('Cannot trigger global event, only listen to it.'); } foreach ($this->getListeners($event) as $listener) { @@ -44,7 +42,7 @@ public function trigger(string $event, &$payload = null) /** * Return the callables associated with the event. * - * @return callable[][] + * @return callable[] */ protected function getListeners(string $event): array { diff --git a/src/Exception.php b/src/EventsException.php similarity index 60% rename from src/Exception.php rename to src/EventsException.php index 87021c3..6b79186 100644 --- a/src/Exception.php +++ b/src/EventsException.php @@ -2,6 +2,6 @@ namespace Parable\Event; -class Exception extends \Exception +class EventsException extends \Exception { } diff --git a/tests/EventManagerTest.php b/tests/EventManagerTest.php deleted file mode 100644 index 5b9effd..0000000 --- a/tests/EventManagerTest.php +++ /dev/null @@ -1,84 +0,0 @@ -eventManager = new EventManager(); - } - - public function testListenToEventAndUpdate(): void - { - $this->eventManager->listen('test_event', static function ($event, string &$payload) { - $payload .= "-suffixed"; - }); - - $payload = 'payload'; - - $this->eventManager->trigger('test_event', $payload); - - self::assertSame('payload-suffixed', $payload); - } - - public function testMultipleEvents(): void - { - $this->eventManager->listen('test_event', static function ($event, string &$payload) { - $payload .= "-suffixed"; - }); - $this->eventManager->listen('test_event', static function ($event, string &$payload) { - $payload .= "-twice!"; - }); - - $payload = 'payload'; - - $this->eventManager->trigger('test_event', $payload); - - self::assertSame('payload-suffixed-twice!', $payload); - } - - public function testSameEventMultipleTimesGetsCalledOnce(): void - { - $closure = static function ($event, string &$payload) { - $payload .= "-suffixed"; - }; - - $this->eventManager->listen('test_event', $closure); - $this->eventManager->listen('test_event', $closure); - - $payload = 'payload'; - - $this->eventManager->trigger('test_event', $payload); - - self::assertSame('payload-suffixed', $payload); - } - - public function testGlobalListeners(): void - { - $this->eventManager->listenAll(static function ($event, string &$payload) { - $payload .= "-suffixed"; - }); - - $payload = 'payload'; - - $this->eventManager->trigger('once update', $payload); - $this->eventManager->trigger('twice update', $payload); - - self::assertSame('payload-suffixed-suffixed', $payload); - } - - public function testCannotTriggerGlobalEvent(): void - { - $this->expectException(Exception::class); - $this->expectExceptionMessage('Cannot specifically trigger global event.'); - - $this->eventManager->trigger('*'); - } -} diff --git a/tests/EventsTest.php b/tests/EventsTest.php new file mode 100644 index 0000000..3230db8 --- /dev/null +++ b/tests/EventsTest.php @@ -0,0 +1,84 @@ +events = new Events(); + } + + public function testListenToEventAndUpdate(): void + { + $this->events->listen('test_event', static function ($event, string &$payload) { + $payload .= "-suffixed"; + }); + + $payload = 'payload'; + + $this->events->trigger('test_event', $payload); + + self::assertSame('payload-suffixed', $payload); + } + + public function testMultipleEvents(): void + { + $this->events->listen('test_event', static function ($event, string &$payload) { + $payload .= "-suffixed"; + }); + $this->events->listen('test_event', static function ($event, string &$payload) { + $payload .= "-twice!"; + }); + + $payload = 'payload'; + + $this->events->trigger('test_event', $payload); + + self::assertSame('payload-suffixed-twice!', $payload); + } + + public function testSameEventMultipleTimesGetsCalledOnce(): void + { + $closure = static function ($event, string &$payload) { + $payload .= "-suffixed"; + }; + + $this->events->listen('test_event', $closure); + $this->events->listen('test_event', $closure); + + $payload = 'payload'; + + $this->events->trigger('test_event', $payload); + + self::assertSame('payload-suffixed', $payload); + } + + public function testGlobalListeners(): void + { + $this->events->listenAll(static function ($event, string &$payload) { + $payload .= "-suffixed"; + }); + + $payload = 'payload'; + + $this->events->trigger('once update', $payload); + $this->events->trigger('twice update', $payload); + + self::assertSame('payload-suffixed-suffixed', $payload); + } + + public function testCannotTriggerGlobalEvent(): void + { + $this->expectException(EventsException::class); + $this->expectExceptionMessage('Cannot trigger global event, only listen to it.'); + + $this->events->trigger('*'); + } +}