From 65c6052e9eb2676e840121a1a7b122ab8851cfd4 Mon Sep 17 00:00:00 2001 From: Anton Karpov Date: Thu, 10 Aug 2023 18:53:55 +0300 Subject: [PATCH] Fixed container versions --- .php-cs-fixer.php | 36 ++++++++-- composer.json | 4 +- src/Bus/Exception/NoHandlerDefined.php | 2 +- src/Bus/Handler/AutoClassHandlerRegistry.php | 2 +- .../ParentsAwareClassHandlerRegistry.php | 2 +- .../PsrContainerClassHandlerRegistry.php | 4 +- src/Bus/Reader/ReflectionMessageIdReader.php | 2 +- src/Event/SimpleEventBus.php | 2 +- .../Bus/Handler/PsrContainerHandlersTest.php | 70 +++++++++++-------- 9 files changed, 78 insertions(+), 46 deletions(-) diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index c240d21..300a6c2 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -1,14 +1,38 @@ in(__DIR__) - ->exclude('tests'); +if (!file_exists(__DIR__ . '/src')) { + exit(0); +} $config = new PhpCsFixer\Config(); +$finder = new PhpCsFixer\Finder(); + +$finder + ->in(__DIR__ . '/src') + ->append([ + __DIR__ . '/public', + __DIR__ . '/.php-cs-fixer.php', + ]) + ->exclude('tests'); $config - ->setCacheFile(__DIR__ . '/build/cache/php-cs-fixer/.php-cs-fixer.cache') - ->setRules(['@Symfony' => true]) - ->setFinder($finder); + ->setFinder($finder) + ->setRiskyAllowed(true) + ->setRules([ + '@PHP71Migration' => true, + '@PHPUnit75Migration:risky' => true, + '@Symfony' => true, + '@Symfony:risky' => true, + 'protected_to_private' => false, + 'native_constant_invocation' => ['strict' => false], + 'nullable_type_declaration_for_default_null_value' => ['use_nullable_type_declaration' => false], + 'no_superfluous_phpdoc_tags' => ['remove_inheritdoc' => true], + 'modernize_strpos' => true, + 'get_class_to_class_keyword' => true, + 'final_class' => true, + 'concat_space' => ['spacing' => 'one'], + 'phpdoc_to_comment' => ['ignored_tags' => ['psalm-suppress']], + ]) + ->setCacheFile(__DIR__ . '/tools/cache/php-cs-fixer/.php-cs-fixer.cache'); return $config; diff --git a/composer.json b/composer.json index e8a5d33..a9d2cf1 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "library", "require": { "php": "^8.2", - "psr/container": "^1.0" + "psr/container": "^1.0|^2.0" }, "require-dev": { "phpunit/phpunit": "^9.0", @@ -43,7 +43,7 @@ "mkdir -p build/cache/phpunit", "mkdir -p build/cache/psalm", "mkdir -p build/report", - "cd tools/php-cs-fixer && composer install --prefer-dist --no-progress --no-interaction && cd -" + "composer install --working-dir=tools/php-cs-fixer --prefer-dist --no-progress --no-interaction" ], "dev": "@setup-dev", "csf": "tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php", diff --git a/src/Bus/Exception/NoHandlerDefined.php b/src/Bus/Exception/NoHandlerDefined.php index 54dc932..b7d4b42 100644 --- a/src/Bus/Exception/NoHandlerDefined.php +++ b/src/Bus/Exception/NoHandlerDefined.php @@ -8,6 +8,6 @@ final class NoHandlerDefined extends BusException { public function __construct(object $message) { - parent::__construct(\sprintf('No handlers for a message "%s"', $message::class), 1, null); + parent::__construct(sprintf('No handlers for a message "%s"', $message::class), 1, null); } } diff --git a/src/Bus/Handler/AutoClassHandlerRegistry.php b/src/Bus/Handler/AutoClassHandlerRegistry.php index 9d1c987..ae58d10 100644 --- a/src/Bus/Handler/AutoClassHandlerRegistry.php +++ b/src/Bus/Handler/AutoClassHandlerRegistry.php @@ -67,7 +67,7 @@ public function autoRegister(string $handlerId, string $handlerMethod = '__invok try { $messageId = $this->reader->read($classReflection->getMethod($handlerMethod)); } catch (\ReflectionException $e) { - throw new InvalidHandler(\sprintf('A handler "%s" supposed to have a method "%s" to register automatically.', $handlerId, $handlerMethod)); + throw new InvalidHandler(sprintf('A handler "%s" supposed to have a method "%s" to register automatically.', $handlerId, $handlerMethod)); } $this->parent->register($messageId, $handlerId, $handlerMethod); diff --git a/src/Bus/Handler/ParentsAwareClassHandlerRegistry.php b/src/Bus/Handler/ParentsAwareClassHandlerRegistry.php index b4837d6..47847cd 100644 --- a/src/Bus/Handler/ParentsAwareClassHandlerRegistry.php +++ b/src/Bus/Handler/ParentsAwareClassHandlerRegistry.php @@ -46,7 +46,7 @@ public function has(string $messageId): bool public function get(string $messageId): \Iterator { - foreach (\array_merge([$messageId], $this->parse($messageId)) as $implementation) { + foreach (array_merge([$messageId], $this->parse($messageId)) as $implementation) { foreach ($this->handlers->get($implementation) as $handler) { yield $handler; } diff --git a/src/Bus/Handler/PsrContainerClassHandlerRegistry.php b/src/Bus/Handler/PsrContainerClassHandlerRegistry.php index ada5408..9ff7c21 100644 --- a/src/Bus/Handler/PsrContainerClassHandlerRegistry.php +++ b/src/Bus/Handler/PsrContainerClassHandlerRegistry.php @@ -24,7 +24,7 @@ public function __construct( public function register(string $messageId, string $handlerClass, string $handlerMethod = '__invoke'): void { if (false === $this->serviceLocator->has($handlerClass)) { - throw new InvalidHandler(\sprintf('There is no a service such as "%s" to handle a "%s" message', $handlerClass, $messageId)); + throw new InvalidHandler(sprintf('There is no a service such as "%s" to handle a "%s" message', $handlerClass, $messageId)); } $this->containerHandlers[$messageId][$handlerClass] = $handlerMethod; @@ -52,7 +52,7 @@ public function get(string $messageId): \Iterator foreach ($this->containerHandlers[$messageId] as $handlerId => $handlerMethod) { /** @var object $handler */ $handler = $this->serviceLocator->get($handlerId); - assert(\method_exists($handler, $handlerMethod)); + \assert(method_exists($handler, $handlerMethod)); yield $handler->$handlerMethod(...); } } diff --git a/src/Bus/Reader/ReflectionMessageIdReader.php b/src/Bus/Reader/ReflectionMessageIdReader.php index a9ae9eb..d9b52ee 100644 --- a/src/Bus/Reader/ReflectionMessageIdReader.php +++ b/src/Bus/Reader/ReflectionMessageIdReader.php @@ -24,7 +24,7 @@ public function read(\ReflectionFunctionAbstract $callback): string } $messageId = $messageType->getName(); - if (false === \class_exists($messageId) && false === \interface_exists($messageId) && false === \enum_exists($messageId)) { + if (false === class_exists($messageId) && false === interface_exists($messageId) && false === enum_exists($messageId)) { throw new ParsingException('A message ID must represent an existing class.'); } diff --git a/src/Event/SimpleEventBus.php b/src/Event/SimpleEventBus.php index f555a1b..9b86083 100644 --- a/src/Event/SimpleEventBus.php +++ b/src/Event/SimpleEventBus.php @@ -13,6 +13,6 @@ final class SimpleEventBus extends SimpleBus implements EventBus { public function handle(object $event): void { - \iterator_to_array($this->handleMessage($event)); + iterator_to_array($this->handleMessage($event)); } } diff --git a/tests/Unit/Bus/Handler/PsrContainerHandlersTest.php b/tests/Unit/Bus/Handler/PsrContainerHandlersTest.php index 7fdf52f..587257e 100644 --- a/tests/Unit/Bus/Handler/PsrContainerHandlersTest.php +++ b/tests/Unit/Bus/Handler/PsrContainerHandlersTest.php @@ -54,22 +54,22 @@ final class PsrContainerHandlersTest extends BusTestCase /** @var \AwdStudio\Bus\Handler\PsrContainerClassHandlerRegistry */ private $instance; - /** @var \Prophecy\Prophecy\ObjectProphecy|\Psr\Container\ContainerInterface */ - private $containerProphecy; + /** @var \Psr\Container\ContainerInterface|\Prophecy\Prophecy\ObjectProphecy */ + private $serviceLocator; /** @var \AwdStudio\Bus\HandlerLocator|\Prophecy\Prophecy\ObjectProphecy */ - private $handlerLocatorProphecy; + private $dynamicHandlers; protected function setUp(): void { parent::setUp(); - $this->containerProphecy = $this->prophesize(ContainerInterface::class); - $this->handlerLocatorProphecy = $this->prophesize(HandlerLocator::class); + $this->serviceLocator = $this->prophesize(ContainerInterface::class); + $this->dynamicHandlers = $this->prophesize(HandlerLocator::class); $this->instance = new PsrContainerClassHandlerRegistry( - $this->containerProphecy->reveal(), - $this->handlerLocatorProphecy->reveal() + $this->serviceLocator->reveal(), + $this->dynamicHandlers->reveal() ); } @@ -86,7 +86,7 @@ public function testMustImplementAHandlers(): void */ public function testMustAllowToInstantiateWithoutExternalHandlers(): void { - $this->assertNotNull(new PsrContainerClassHandlerRegistry($this->containerProphecy->reveal())); + $this->assertNotNull(new PsrContainerClassHandlerRegistry($this->serviceLocator->reveal())); } /** @@ -94,7 +94,7 @@ public function testMustAllowToInstantiateWithoutExternalHandlers(): void */ public function testMustCheckAServiceWhenTriesToRegisterIt(): void { - $this->containerProphecy + $this->serviceLocator ->has(Argument::exact(FooHandler::class)) ->willReturn(true) ->shouldBeCalledOnce(); @@ -107,7 +107,7 @@ public function testMustCheckAServiceWhenTriesToRegisterIt(): void */ public function testMustThrowAnExceptionIfAHandlerIsNotInTheServiceLocator(): void { - $this->containerProphecy + $this->serviceLocator ->has(Argument::exact(FooHandler::class)) ->willReturn(false); @@ -123,7 +123,7 @@ public function testMustCallAnExternalHandlersToAddADynamicHandler(): void { $dynamicHandler = static function (): void { return; }; - $this->handlerLocatorProphecy + $this->dynamicHandlers ->add(Argument::exact(\stdClass::class), Argument::exact($dynamicHandler)) ->shouldBeCalledOnce(); @@ -135,7 +135,7 @@ public function testMustCallAnExternalHandlersToAddADynamicHandler(): void */ public function testMustCheckAnExternalHandlersWhenLooksForAHandler(): void { - $this->handlerLocatorProphecy + $this->dynamicHandlers ->has(Argument::exact(FooCallback::class)) ->willReturn(false) ->shouldBeCalledOnce(); @@ -148,7 +148,7 @@ public function testMustCheckAnExternalHandlersWhenLooksForAHandler(): void */ public function testMustReturnTrueIfAHandlerIsInExternalHandlers(): void { - $this->handlerLocatorProphecy + $this->dynamicHandlers ->has(Argument::exact(FooCallback::class)) ->willReturn(true); @@ -160,11 +160,11 @@ public function testMustReturnTrueIfAHandlerIsInExternalHandlers(): void */ public function testMustReturnTrueIfAHandlerRegisteredInTheContainerHandlers(): void { - $this->handlerLocatorProphecy + $this->dynamicHandlers ->has(Argument::exact(FooCallback::class)) ->willReturn(false); - $this->containerProphecy + $this->serviceLocator ->has(Argument::exact(FooHandler::class)) ->willReturn(true); @@ -178,7 +178,7 @@ public function testMustReturnTrueIfAHandlerRegisteredInTheContainerHandlers(): */ public function testMustReturnFalseIfThereAreNoHandlersNorInDynamicHandlersNotInRegisteredOnes(): void { - $this->handlerLocatorProphecy + $this->dynamicHandlers ->has(Argument::exact(FooCallback::class)) ->willReturn(false); @@ -192,11 +192,11 @@ public function testMustYieldAHandlerFromDynamicHandlersIfItHasSome(): void { $dynamicHandler = static function (): void { return; }; - $this->handlerLocatorProphecy + $this->dynamicHandlers ->has(Argument::exact(FooCallback::class)) ->willReturn(true); - $this->handlerLocatorProphecy + $this->dynamicHandlers ->get(Argument::exact(FooCallback::class)) ->willYield([$dynamicHandler]); @@ -210,16 +210,20 @@ public function testMustYieldAHandlerFromContainerIfItRegistered(): void { $dynamicHandler = static function (): void { return; }; - $this->instance->register(FooCallback::class, FooHandler::class); - - $this->handlerLocatorProphecy + $this->dynamicHandlers ->has(Argument::exact(FooCallback::class)) ->willReturn(false); - $this->containerProphecy + $this->serviceLocator + ->has(Argument::exact(FooHandler::class)) + ->willReturn(true); + + $this->serviceLocator ->get(Argument::exact(FooHandler::class)) ->willReturn($dynamicHandler); + $this->instance->register(FooCallback::class, FooHandler::class); + $this->assertContains($dynamicHandler, $this->instance->get(FooCallback::class)); } @@ -232,21 +236,25 @@ public function testMustYieldAllHandlersFromBothAndDynamicHandlersAndTheContaine $dynamicHandler2 = static function (): void { return; }; $dynamicHandler3 = static function (): void { return; }; - $this->instance->register(FooCallback::class, FooHandler1::class); - $this->instance->register(FooCallback::class, FooHandler2::class); - - $this->handlerLocatorProphecy + $this->dynamicHandlers ->has(Argument::exact(FooCallback::class)) ->willReturn(true); - $this->handlerLocatorProphecy + $this->dynamicHandlers ->get(Argument::exact(FooCallback::class)) ->willYield([$dynamicHandler3]); - $this->containerProphecy + $this->serviceLocator ->get(Argument::any()) ->willReturn($dynamicHandler1, $dynamicHandler2); + $this->serviceLocator + ->has(Argument::any()) + ->willReturn(true); + + $this->instance->register(FooCallback::class, FooHandler1::class); + $this->instance->register(FooCallback::class, FooHandler2::class); + $result = []; foreach ($this->instance->get(FooCallback::class) as $handler) { $result[] = $handler; @@ -262,15 +270,15 @@ public function testMustYieldAllHandlersFromBothAndDynamicHandlersAndTheContaine */ public function testMustReturnACallableArrayAsAnObjectAndRegisteredMethod(): void { - $this->handlerLocatorProphecy + $this->dynamicHandlers ->has(Argument::any()) ->willReturn(false); - $this->containerProphecy + $this->serviceLocator ->has(Argument::exact(FooHandler3::class)) ->willReturn(true); - $this->containerProphecy + $this->serviceLocator ->get(Argument::exact(FooHandler3::class)) ->willReturn(new FooHandler3());