From d786ae15119d4c3bce0a47dbcfa141f0bbf171ce Mon Sep 17 00:00:00 2001 From: David Badura Date: Wed, 18 Jun 2025 09:48:10 +0200 Subject: [PATCH] remove RetryOutdatedAggregateCommandBus --- phpstan-baseline.neon | 18 --- phpstan.neon.dist | 2 + src/Attribute/RetryAggregateOutdated.php | 17 --- .../RetryOutdatedAggregateCommandBus.php | 50 ------ .../RetryOutdatedAggregateCommandBusTest.php | 143 ------------------ 5 files changed, 2 insertions(+), 228 deletions(-) delete mode 100644 src/Attribute/RetryAggregateOutdated.php delete mode 100644 src/CommandBus/RetryOutdatedAggregateCommandBus.php delete mode 100644 tests/Unit/CommandBus/RetryOutdatedAggregateCommandBusTest.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 49202891..eeaad944 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,23 +1,5 @@ parameters: ignoreErrors: - - - message: '#^Dead catch \- Patchlevel\\EventSourcing\\Repository\\AggregateOutdated is never thrown in the try block\.$#' - identifier: catch.neverThrown - count: 1 - path: src/CommandBus/RetryOutdatedAggregateCommandBus.php - - - - message: '#^Method Patchlevel\\EventSourcing\\CommandBus\\RetryOutdatedAggregateCommandBus\:\:maxRetries\(\) is unused\.$#' - identifier: method.unused - count: 1 - path: src/CommandBus/RetryOutdatedAggregateCommandBus.php - - - - message: '#^Method Patchlevel\\EventSourcing\\CommandBus\\RetryOutdatedAggregateCommandBus\:\:maxRetries\(\) never returns null so it can be removed from the return type\.$#' - identifier: return.unusedType - count: 1 - path: src/CommandBus/RetryOutdatedAggregateCommandBus.php - - message: '#^Cannot unset offset ''url'' on array\{application_name\?\: string, charset\?\: string, dbname\?\: string, defaultTableOptions\?\: array\, driver\?\: ''ibm_db2''\|''mysqli''\|''oci8''\|''pdo_mysql''\|''pdo_oci''\|''pdo_pgsql''\|''pdo_sqlite''\|''pdo_sqlsrv''\|''pgsql''\|''sqlite3''\|''sqlsrv'', driverClass\?\: class\-string\, driverOptions\?\: array\, host\?\: string, \.\.\.\}\.$#' identifier: unset.offset diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 6452d44b..4e5c658f 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -8,3 +8,5 @@ parameters: ignoreErrors: - identifier: missingType.generics + reportUnmatchedIgnoredErrors: false + diff --git a/src/Attribute/RetryAggregateOutdated.php b/src/Attribute/RetryAggregateOutdated.php deleted file mode 100644 index 3198490f..00000000 --- a/src/Attribute/RetryAggregateOutdated.php +++ /dev/null @@ -1,17 +0,0 @@ -doDispatch($command, 0); - } - - private function doDispatch(object $command, int $retry, int|null $maxRetries = null): void - { - try { - $this->commandBus->dispatch($command); - } catch (AggregateOutdated $exception) { - $maxRetries ??= $this->maxRetries($command); - - if ($retry >= $maxRetries) { - throw $exception; - } - - $this->doDispatch($command, $retry + 1, $maxRetries); - } - } - - private function maxRetries(object $command): int|null - { - $reflectionClass = new ReflectionClass($command); - $attributes = $reflectionClass->getAttributes(RetryAggregateOutdated::class); - - if ($attributes === []) { - return 0; - } - - return $attributes[0]->newInstance()->maxRetries; - } -} diff --git a/tests/Unit/CommandBus/RetryOutdatedAggregateCommandBusTest.php b/tests/Unit/CommandBus/RetryOutdatedAggregateCommandBusTest.php deleted file mode 100644 index 81b84cc4..00000000 --- a/tests/Unit/CommandBus/RetryOutdatedAggregateCommandBusTest.php +++ /dev/null @@ -1,143 +0,0 @@ -createMock(CommandBus::class); - $innerCommandBus - ->expects($this->once()) - ->method('dispatch') - ->with($command); - - $retryCommandBus = new RetryOutdatedAggregateCommandBus($innerCommandBus); - - $retryCommandBus->dispatch($command); - - $this->assertTrue(true); // If no exception is thrown, the test passes - } - - public function testDispatchRetriesUntilSuccess(): void - { - $command = new #[RetryAggregateOutdated(maxRetries: 3)] - class { - public function __construct() - { - } - }; - - $innerCommandBus = $this->createMock(CommandBus::class); - $innerCommandBus - ->expects($this->exactly(3)) - ->method('dispatch') - ->with($command) - ->willReturnOnConsecutiveCalls( - $this->throwException(new AggregateOutdated('profile', ProfileId::fromString('profile'))), - $this->throwException(new AggregateOutdated('profile', ProfileId::fromString('profile'))), - null, // Success on the third attempt - ); - - $retryCommandBus = new RetryOutdatedAggregateCommandBus($innerCommandBus); - - $retryCommandBus->dispatch($command); - - $this->assertTrue(true); // If no exception is thrown, the test passes - } - - public function testDispatchThrowsAfterMaxRetries(): void - { - $command = new #[RetryAggregateOutdated(maxRetries: 2)] - class { - public function __construct() - { - } - }; - - $innerCommandBus = $this->createMock(CommandBus::class); - $innerCommandBus - ->expects($this->exactly(3)) - ->method('dispatch') - ->with($command) - ->willThrowException( - new AggregateOutdated( - 'profile', - ProfileId::fromString('profile'), - ), - ); - - $retryCommandBus = new RetryOutdatedAggregateCommandBus($innerCommandBus); - - $this->expectException(AggregateOutdated::class); - - $retryCommandBus->dispatch($command); - } - - public function testDispatchNotRetry(): void - { - $command = new class { - public function __construct() - { - } - }; - - $innerCommandBus = $this->createMock(CommandBus::class); - $innerCommandBus - ->expects($this->once()) - ->method('dispatch') - ->with($command) - ->willThrowException( - new AggregateOutdated( - 'profile', - ProfileId::fromString('profile'), - ), - ); - - $retryCommandBus = new RetryOutdatedAggregateCommandBus($innerCommandBus); - - $this->expectException(AggregateOutdated::class); - - $retryCommandBus->dispatch($command); - } - - public function testSkipOtherExceptions(): void - { - $command = new #[RetryAggregateOutdated(maxRetries: 2)] - class { - public function __construct() - { - } - }; - - $innerCommandBus = $this->createMock(CommandBus::class); - $innerCommandBus - ->expects($this->once()) - ->method('dispatch') - ->with($command) - ->willThrowException(new RuntimeException('Some other exception')); - - $retryCommandBus = new RetryOutdatedAggregateCommandBus($innerCommandBus); - - $this->expectException(RuntimeException::class); - - $retryCommandBus->dispatch($command); - } -}