From 10d8ed2c863b6afa23e665cb4a4ce563e1947d84 Mon Sep 17 00:00:00 2001 From: Dmitriy Derepko Date: Sun, 23 Oct 2022 20:33:49 +0300 Subject: [PATCH] Add rector [batch] (#69) * Add rector files https://github.com/yiisoft/yii-dev-tool/pull/232 * Add rector/rector dependecy * [rector] Apply fixes * Apply fixes from StyleCI * Use predefined rector action * Drop PHP 7.4 * Add changelog line, improve readme * Add import Co-authored-by: rector-bot Co-authored-by: StyleCI Bot Co-authored-by: Rustam Co-authored-by: Alexey Rogachev --- .github/workflows/build.yml | 2 +- .github/workflows/rector.yml | 21 ++++++++++++++++++ .github/workflows/static.yml | 2 +- CHANGELOG.md | 9 ++++---- README.md | 4 ++++ composer.json | 3 ++- rector.php | 22 +++++++++++++++++++ src/Dispatcher/Dispatcher.php | 5 +---- src/Provider/CompositeProvider.php | 2 -- src/Provider/ListenerCollection.php | 2 -- src/Provider/Provider.php | 9 ++------ .../Dispatcher/CompositeDispatcherTest.php | 6 ++--- 12 files changed, 61 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/rector.yml create mode 100644 rector.php diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0dde6b6..8506ea1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,4 +28,4 @@ jobs: os: >- ['ubuntu-latest', 'windows-latest'] php: >- - ['7.4', '8.0', '8.1'] + ['8.0', '8.1'] diff --git a/.github/workflows/rector.yml b/.github/workflows/rector.yml new file mode 100644 index 0000000..adacd73 --- /dev/null +++ b/.github/workflows/rector.yml @@ -0,0 +1,21 @@ +on: + pull_request: + paths-ignore: + - 'docs/**' + - 'README.md' + - 'CHANGELOG.md' + - '.gitignore' + - '.gitattributes' + - 'infection.json.dist' + - 'psalm.xml' + +name: rector + +jobs: + rector: + uses: yiisoft/actions/.github/workflows/rector.yml@master + with: + os: >- + ['ubuntu-latest'] + php: >- + ['8.0'] diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 40ac260..96b2679 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -28,4 +28,4 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['7.4', '8.0', '8.1'] + ['8.0', '8.1'] diff --git a/CHANGELOG.md b/CHANGELOG.md index 7689b72..e677b46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,13 +1,14 @@ # Yii Event Dispatcher Change Log -## 1.0.2 under development +## 1.1.0 under development -- Enh #64: Improve exception message on adding listener to collection (vjik) +- Enh #64: Improve exception message on adding listener to collection (@vjik) +- Chg #69: Raise the minimum `PHP` version to `8.0` (@xepozz, @rustamwin) ## 1.0.1 July 28, 2021 -- New #63: Allow adding a single listener to multiple event class names at once in `ListenerCollection::add()` (vjik) -- Bug #62: Support listeners for events defined with union types (vjik) +- New #63: Allow adding a single listener to multiple event class names at once in `ListenerCollection::add()` (@vjik) +- Bug #62: Support listeners for events defined with union types (@vjik) ## 1.0.0 February 16, 2021 diff --git a/README.md b/README.md index 236b178..8ebf27c 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,10 @@ to events dispatched. - Encourages designing event hierarchy. - Can combine multiple event listener providers. +## Requirements + +- PHP 8.0 or higher. + ## Installation The package could be installed with composer: diff --git a/composer.json b/composer.json index d66926f..113a307 100644 --- a/composer.json +++ b/composer.json @@ -18,11 +18,12 @@ "source": "https://github.com/yiisoft/event-dispatcher" }, "require": { - "php": "^7.4|^8.0", + "php": "^8.0", "psr/event-dispatcher": "1.0.0" }, "require-dev": { "phpunit/phpunit": "^9.5", + "rector/rector": "^0.14.3", "roave/infection-static-analysis-plugin": "^1.16", "spatie/phpunit-watcher": "^1.23", "vimeo/psalm": "^4.18" diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..63713ce --- /dev/null +++ b/rector.php @@ -0,0 +1,22 @@ +paths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + + // define sets of rules + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_80, + ]); +}; diff --git a/src/Dispatcher/Dispatcher.php b/src/Dispatcher/Dispatcher.php index d580402..0c9ab6f 100644 --- a/src/Dispatcher/Dispatcher.php +++ b/src/Dispatcher/Dispatcher.php @@ -15,11 +15,8 @@ */ final class Dispatcher implements EventDispatcherInterface { - private ListenerProviderInterface $listenerProvider; - - public function __construct(ListenerProviderInterface $listenerProvider) + public function __construct(private ListenerProviderInterface $listenerProvider) { - $this->listenerProvider = $listenerProvider; } public function dispatch(object $event): object diff --git a/src/Provider/CompositeProvider.php b/src/Provider/CompositeProvider.php index 392a466..101d4b8 100644 --- a/src/Provider/CompositeProvider.php +++ b/src/Provider/CompositeProvider.php @@ -25,8 +25,6 @@ public function getListenersForEvent(object $event): iterable /** * Adds provider as a source for event listeners. - * - * @param ListenerProviderInterface $provider */ public function attach(ListenerProviderInterface $provider): void { diff --git a/src/Provider/ListenerCollection.php b/src/Provider/ListenerCollection.php index 7ce8447..8522f28 100644 --- a/src/Provider/ListenerCollection.php +++ b/src/Provider/ListenerCollection.php @@ -53,8 +53,6 @@ public function getForEvents(string ...$eventClassNames): iterable * @param string ...$eventClassNames * * @throws InvalidArgumentException If callable is invalid. - * - * @return self */ public function add(callable $listener, string ...$eventClassNames): self { diff --git a/src/Provider/Provider.php b/src/Provider/Provider.php index ac42a2f..c2fa342 100644 --- a/src/Provider/Provider.php +++ b/src/Provider/Provider.php @@ -6,8 +6,6 @@ use Psr\EventDispatcher\ListenerProviderInterface; -use function get_class; - /** * Provider is a listener provider that registers event listeners for interfaces used in callable type-hints * and gives out a list of handlers by event interface provided for further use with Dispatcher. @@ -23,16 +21,13 @@ */ final class Provider implements ListenerProviderInterface { - private ListenerCollection $listeners; - - public function __construct(ListenerCollection $listeners) + public function __construct(private ListenerCollection $listeners) { - $this->listeners = $listeners; } public function getListenersForEvent(object $event): iterable { - yield from $this->listeners->getForEvents(get_class($event)); + yield from $this->listeners->getForEvents($event::class); /** @psalm-suppress MixedArgument */ yield from $this->listeners->getForEvents(...array_values(class_parents($event))); /** @psalm-suppress MixedArgument */ diff --git a/tests/common/Dispatcher/CompositeDispatcherTest.php b/tests/common/Dispatcher/CompositeDispatcherTest.php index 2eb9dca..04d4077 100644 --- a/tests/common/Dispatcher/CompositeDispatcherTest.php +++ b/tests/common/Dispatcher/CompositeDispatcherTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\EventDispatcher\Tests\Dispatcher; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\EventDispatcher\StoppableEventInterface; @@ -60,10 +61,7 @@ public function testPropagationStops(): void $this->assertSame($stoppableEvent, $result); } - /** - * @return \PHPUnit\Framework\MockObject\MockObject|\Psr\EventDispatcher\EventDispatcherInterface - */ - private function createTransparentDispatcher() + private function createTransparentDispatcher(): MockObject|EventDispatcherInterface { $dispatcher = $this->createMock(EventDispatcherInterface::class); $dispatcher