Skip to content

Conversation

@VincentLanglet
Copy link
Contributor

This is the same than #313 but on the EntityManagerInterface in order to avoid the deadcatch with

try {
    $this->entityManagerInterface->flush();
} catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) {
    // pass
}

or avoid but it's not thrown. phpstan error.

Injecting the interface rather than EntityManager is a really common thing when working with Symfony/doctrine (thanks to autowire/autoconfigure) so the @throws annotation will be more useful on the interface.

@VincentLanglet
Copy link
Contributor Author

VincentLanglet commented Nov 8, 2023

Should I target 1.3.x or 1.4.x @ondrejmirtes ?

@ondrejmirtes
Copy link
Member

1.3.x

@VincentLanglet VincentLanglet changed the base branch from 1.4.x to 1.3.x November 8, 2023 14:06
@VincentLanglet
Copy link
Contributor Author

1.3.x

Thanks, done

@ondrejmirtes ondrejmirtes merged commit b1e4a69 into phpstan:1.3.x Nov 8, 2023
@ondrejmirtes
Copy link
Member

Thank you.

@VincentLanglet
Copy link
Contributor Author

Hi @ondrejmirtes, I think this PR doesn't work and I'm not sure why.

I'm still getting a dead catch error when writing

try {
     $entityManager->flush();
} catch (ORMException|UniqueConstraintViolationException) {}

I can reproduce the bug if I had @throws void in the ObjectManager class but I cannot reproduce the error in the PHPStan tests yet, even with implicitThrows: false...

I think it's because flush method is in the ObjectManager
https://github.com/doctrine/persistence/blob/3.2.x/src/Persistence/ObjectManager.php#L94
and not in the EntityManagerInterface.

Is there a way to say to PHPStan that ORMException|UniqueConstraintViolationException can be thrown by EntityManagerInterface::flush without adding those exception to ObjectManager::flush ?

@ondrejmirtes
Copy link
Member

@VincentLanglet It could be done only with a dynamic throw type extension.

@VincentLanglet
Copy link
Contributor Author

@VincentLanglet It could be done only with a dynamic throw type extension.

Thanks for the advice, I made #486
I check the branch on my personal project, it solves indeed the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants