Skip to content

Commit

Permalink
Relax exception check for unique constraints
Browse files Browse the repository at this point in the history
This is a temporary workaround for the changes made in
Doctrine ORM 2.16, discussed in
doctrine/orm#10785

When that discussion is resolved, we should remove the check for
`RuntimeException`, because it might be thrown by other errors that have
nothing to do with duplicate IDs, which would lead to our
`PaymentOverrideException` hinting at the wrong thing.

Also remove the PHPStan rule that had an exception for this check.
When the Exception comes back, the method should have an annotation that
it's thrown. Otherwise we'll have to put the PHPStan rule back or try
out phpstan-doctrine where this has been solved:
phpstan/phpstan-doctrine#315
  • Loading branch information
gbirke committed Aug 3, 2023
1 parent f2cfe68 commit 58c1b6d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
8 changes: 0 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
parameters:
ignoreErrors:
# A workaround for Doctrine not specifying an UniqueConstraintViolationException being thrown
# See https://github.com/doctrine/orm/issues/7780
# This rule might be unneccessary in the future with a fixe discussed in
# https://github.com/phpstan/phpstan-doctrine/issues/295
- message: /Doctrine\\DBAL\\Exception\\UniqueConstraintViolationException is never thrown in the try block/
path: src/DataAccess/DoctrinePaymentRepository.php
count: 1

excludePaths:
analyse:
Expand Down
4 changes: 3 additions & 1 deletion src/DataAccess/DoctrinePaymentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function storePayment( Payment $payment ): void {
$this->entityManager->persist( $payment );
try {
$this->entityManager->flush();
} catch ( UniqueConstraintViolationException $ex ) {
// TODO Remove allowing \RuntimeException when the discussion on https://github.com/doctrine/orm/pull/10785
// has been resolved. Hopefully, they'll re-introduce UniqueConstraintViolationException in a patch release
} catch ( UniqueConstraintViolationException|\RuntimeException $ex ) {
throw new PaymentOverrideException( $ex->getMessage(), $ex->getCode(), $ex );
}
}
Expand Down

0 comments on commit 58c1b6d

Please sign in to comment.