Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Doctrine\DBAL\Exception to an interface #5726

Merged
merged 1 commit into from
Oct 6, 2022

Conversation

morozov
Copy link
Member

@morozov morozov commented Oct 6, 2022

Q A
Type improvement

Currently, the base exception being a class doesn't allow to reuse of the semantics of the SPL exception classes. For instance, distinguish between logic and runtime exceptions.

Not being able to reuse the semantics defined in SPL causes the following issue about documenting exceptions. For example, take the NotSupported exception which has the following ancestry: NotSupported → \Doctrine\DBAL\Exception → \Exception. As static analyzers see it, semantically, it's just a generic exception as it extends the generic \Exception class. But in fact, it identifies an error in the logic of the code that consumes the API and thereby should not be handled by the caller (an unchecked exception).

Now, take this use case for example:

/** @throws Exception If not supported on this platform. */
public function getSequenceNextValSQL(string $sequence): string
{
throw NotSupported::new(__METHOD__);
}

The method is declared as throwing an exception with unknown semantics. As a result, static analyzers like PhpStorm flag the calls that don't handle this exception as incorrect. If we remove the @throws annotation, then the throw statement is flagged as incorrect. The only way to write statically correct code against this API is to catch the exception and do nothing with it, which is still wrong.

Converting the class to an interface allows retaining the relation of the exception to the DBAL and at the same time reusing the semantics defined in SPL. For example:

use Doctrine\DBAL\Exception;
/**
* Exception to be thrown when invalid arguments are passed to any DBAL API
*
* @psalm-immutable
*/
class InvalidArgumentException extends \InvalidArgumentException implements Exception

In subsequent patches, we can fine-tune the semantics of other exceptions (firstly, NotSupported) by changing their parent from \Exception to something more specific.

@morozov morozov marked this pull request as ready for review October 6, 2022 14:55
@morozov morozov merged commit 6600b99 into doctrine:4.0.x Oct 6, 2022
@morozov morozov deleted the exception-interface branch October 6, 2022 21:00
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants