diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8017dc2..54f2850 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -28,31 +28,17 @@ jobs: - name: Checkout uses: actions/checkout@v4 - - name: Setup PHP, extensions and composer with shivammathur/setup-php + - name: Setup PHP and extensions with shivammathur/setup-php uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} extensions: mbstring, ctype, iconv, bcmath, filter, json coverage: xdebug, pcov - tools: composer:v2 - - name: Get composer cache directory - id: composer-cache - run: | - echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - - - name: Cache dependencies - uses: actions/cache@v4 + - name: Setup Composer, install dependencies + uses: ramsey/composer-install@v3 with: - path: ${{ steps.composer-cache.outputs.dir }} - key: composer-${{ runner.os }}-${{ matrix.php }}-${{ hashFiles('**/composer.json') }} - restore-keys: | - composer-${{ runner.os }}-${{ matrix.php }} - composer-${{ runner.os }}- - composer- - - - name: Install Composer dependencies - run: composer install --no-progress --no-interaction --optimize-autoloader --ansi + composer-options: "--optimize-autoloader" - name: Test run: | diff --git a/.gitignore b/.gitignore index 48a56c0..7330583 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ composer.lock vendor .phpunit.result.cache -.phpunit.cache \ No newline at end of file +.phpunit.cache diff --git a/src/EventEmitterInterface.php b/src/EventEmitterInterface.php index 1415eb6..8dfc878 100644 --- a/src/EventEmitterInterface.php +++ b/src/EventEmitterInterface.php @@ -40,11 +40,15 @@ public function removeAllListeners(?string $event = null): void; /** * Allows you to inspect the listeners attached to an event. Particularly useful * to check if there are any listeners at all. + * + * @return array>|list<(callable)> */ public function listeners(?string $event = null): array; /** * Emit an event, which will call all listeners. + * + * @param array $arguments */ public function emit(string $event, array $arguments = []): void; } diff --git a/src/EventEmitterTrait.php b/src/EventEmitterTrait.php index d0f8294..795c1c5 100644 --- a/src/EventEmitterTrait.php +++ b/src/EventEmitterTrait.php @@ -18,6 +18,7 @@ use function array_search; use function array_unique; use function array_values; +use function count; trait EventEmitterTrait { @@ -73,7 +74,7 @@ public function removeListener(string $event, callable $listener): void if (false !== $index) { unset($this->listeners[$event][$index]); - if (\count($this->listeners[$event]) === 0) { + if (count($this->listeners[$event]) === 0) { unset($this->listeners[$event]); } } @@ -85,7 +86,7 @@ public function removeListener(string $event, callable $listener): void if (false !== $index) { unset($this->onceListeners[$event][$index]); - if (\count($this->onceListeners[$event]) === 0) { + if (count($this->onceListeners[$event]) === 0) { unset($this->onceListeners[$event]); } } @@ -102,9 +103,6 @@ public function removeAllListeners(?string $event = null): void } } - /** - * @return array>|list<(callable)> - */ public function listeners(?string $event = null): array { if ($event === null) { @@ -122,6 +120,7 @@ public function listeners(?string $event = null): array $this->onceListeners[$eventName] ?? [] ); } + return $events; } @@ -131,9 +130,6 @@ public function listeners(?string $event = null): array ); } - /** - * @param array $arguments - */ public function emit(string $event, array $arguments = []): void { if ($event === '') { diff --git a/tests/EventEmitterTest.php b/tests/EventEmitterTest.php index 8924d38..ad9dbed 100644 --- a/tests/EventEmitterTest.php +++ b/tests/EventEmitterTest.php @@ -21,13 +21,8 @@ use PHPUnit\Framework\TestCase; #[CoversClass(EventEmitter::class)] -#[BackupGlobals(false)] -#[BackupStaticProperties(false)] class EventEmitterTest extends TestCase { - /** - * @var EventEmitter - */ private EventEmitter $emitter; #[Before] @@ -40,7 +35,7 @@ public function testAddListenerWithLambda(): void { $this->emitter->on('foo', static function (): void {}); - $this->assertCount(1, $this->emitter->listeners('foo')); + self::assertCount(1, $this->emitter->listeners('foo')); } public function testAddListenerWithMethod(): void @@ -48,14 +43,14 @@ public function testAddListenerWithMethod(): void $listener = new Listener(); $this->emitter->on('foo', [$listener, 'onFoo']); - $this->assertCount(1, $this->emitter->listeners('foo')); + self::assertCount(1, $this->emitter->listeners('foo')); } public function testAddListenerWithStaticMethod(): void { $this->emitter->on('bar', ['Evenement\Tests\Listener', 'onBar']); - $this->assertCount(1, $this->emitter->listeners('bar')); + self::assertCount(1, $this->emitter->listeners('bar')); } public function testAddListenerWithInvalidListener(): void @@ -72,15 +67,15 @@ public function testOnce(): void $listenerCalled++; }); - $this->assertSame(0, $listenerCalled); + self::assertSame(0, $listenerCalled); $this->emitter->emit('foo'); - $this->assertSame(1, $listenerCalled); + self::assertSame(1, $listenerCalled); $this->emitter->emit('foo'); - $this->assertSame(1, $listenerCalled); + self::assertSame(1, $listenerCalled); } public function testOnceWithArguments(): void @@ -93,7 +88,7 @@ public function testOnceWithArguments(): void $this->emitter->emit('foo', ['a', 'b']); - $this->assertSame(['a', 'b'], $capturedArgs); + self::assertSame(['a', 'b'], $capturedArgs); } public function testEmitWithoutArguments(): void @@ -104,44 +99,40 @@ public function testEmitWithoutArguments(): void $listenerCalled = true; }); - $this->assertSame(false, $listenerCalled); + self::assertFalse($listenerCalled); $this->emitter->emit('foo'); - $this->assertSame(true, $listenerCalled); + self::assertTrue($listenerCalled); } public function testEmitWithOneArgument(): void { - $test = $this; - $listenerCalled = false; - $this->emitter->on('foo', static function (string $value) use (&$listenerCalled, $test): void { + $this->emitter->on('foo', static function (string $value) use (&$listenerCalled): void { $listenerCalled = true; - $test->assertSame('bar', $value); + self::assertSame('bar', $value); }); - $this->assertSame(false, $listenerCalled); + self::assertFalse($listenerCalled); $this->emitter->emit('foo', ['bar']); - $this->assertSame(true, $listenerCalled); + self::assertTrue($listenerCalled); } public function testEmitWithTwoArguments(): void { - $test = $this; - $listenerCalled = false; - $this->emitter->on('foo', static function (string $arg1, string $arg2) use (&$listenerCalled, $test): void { + $this->emitter->on('foo', static function (string $arg1, string $arg2) use (&$listenerCalled): void { $listenerCalled = true; - $test->assertSame('bar', $arg1); - $test->assertSame('baz', $arg2); + self::assertSame('bar', $arg1); + self::assertSame('baz', $arg2); }); - $this->assertSame(false, $listenerCalled); + self::assertFalse($listenerCalled); $this->emitter->emit('foo', ['bar', 'baz']); - $this->assertSame(true, $listenerCalled); + self::assertTrue($listenerCalled); } #[DoesNotPerformAssertions] @@ -164,9 +155,9 @@ public function testEmitWithTwoListeners(): void $listenersCalled++; }); - $this->assertSame(0, $listenersCalled); + self::assertSame(0, $listenersCalled); $this->emitter->emit('foo'); - $this->assertSame(2, $listenersCalled); + self::assertSame(2, $listenersCalled); } public function testRemoveListenerMatching(): void @@ -180,9 +171,9 @@ public function testRemoveListenerMatching(): void $this->emitter->on('foo', $listener); $this->emitter->removeListener('foo', $listener); - $this->assertSame(0, $listenersCalled); + self::assertSame(0, $listenersCalled); $this->emitter->emit('foo'); - $this->assertSame(0, $listenersCalled); + self::assertSame(0, $listenersCalled); } public function testRemoveListenerNotMatching(): void @@ -196,9 +187,9 @@ public function testRemoveListenerNotMatching(): void $this->emitter->on('foo', $listener); $this->emitter->removeListener('bar', $listener); - $this->assertSame(0, $listenersCalled); + self::assertSame(0, $listenersCalled); $this->emitter->emit('foo'); - $this->assertSame(1, $listenersCalled); + self::assertSame(1, $listenersCalled); } public function testRemoveAllListenersMatching(): void @@ -211,9 +202,9 @@ public function testRemoveAllListenersMatching(): void $this->emitter->removeAllListeners('foo'); - $this->assertSame(0, $listenersCalled); + self::assertSame(0, $listenersCalled); $this->emitter->emit('foo'); - $this->assertSame(0, $listenersCalled); + self::assertSame(0, $listenersCalled); } public function testRemoveAllListenersNotMatching(): void @@ -226,9 +217,9 @@ public function testRemoveAllListenersNotMatching(): void $this->emitter->removeAllListeners('bar'); - $this->assertSame(0, $listenersCalled); + self::assertSame(0, $listenersCalled); $this->emitter->emit('foo'); - $this->assertSame(1, $listenersCalled); + self::assertSame(1, $listenersCalled); } public function testRemoveAllListenersWithoutArguments(): void @@ -245,10 +236,10 @@ public function testRemoveAllListenersWithoutArguments(): void $this->emitter->removeAllListeners(); - $this->assertSame(0, $listenersCalled); + self::assertSame(0, $listenersCalled); $this->emitter->emit('foo'); $this->emitter->emit('bar'); - $this->assertSame(0, $listenersCalled); + self::assertSame(0, $listenersCalled); } public function testCallablesClosure(): void @@ -476,12 +467,12 @@ public function testNestedOn(): void }); $emitter->emit('event'); - $this->assertEquals(1, $first); - $this->assertEquals(0, $second); - $this->assertEquals(0, $third); + self::assertSame(1, $first); + self::assertSame(0, $second); + self::assertSame(0, $third); $emitter->emit('event'); - $this->assertEquals(2, $first); - $this->assertEquals(1, $second); - $this->assertEquals(1, $third); + self::assertSame(2, $first); + self::assertSame(1, $second); + self::assertSame(1, $third); } }