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

Use a narrower return type for DriverManager::getAvailableDrivers() #5853

Merged

Conversation

MidnightDesign
Copy link
Contributor

Q A
Type improvement
Fixed issues

Summary

Adds a @psalm-return annotation to DriverManager::getAvailableDrivers() so the return type is narrower than just array<array-key, string>.

@MidnightDesign MidnightDesign changed the title Use a more specific return type for DriverManager::getAvailableDrivers() Use a narrower return type for DriverManager::getAvailableDrivers() Jan 3, 2023
@greg0ire
Copy link
Member

greg0ire commented Jan 3, 2023

This should target 3.6.x

@greg0ire greg0ire changed the base branch from 3.5.x to 3.6.x January 3, 2023 10:37
@MidnightDesign
Copy link
Contributor Author

Would it be useful to add a "static analysis test" that checks whether the method can be used as a type guard?

$driver = getenv('DB_DRIVER');
if (!in_array($driver, DriverManager::getAvailableDrivers(), true) {
    throw new RuntimeException('...');
}
DriverManager::getConnection(['driver' => $driver]);

@derrabus derrabus added this to the 3.6.0 milestone Jan 3, 2023
@derrabus derrabus merged commit fb248c8 into doctrine:3.6.x Jan 3, 2023
@derrabus
Copy link
Member

derrabus commented Jan 3, 2023

Thank you @MidnightDesign!

We don't really have a concept for this kind of test in this repository, I guess. If this is something you want to work on, you're welcome to do so.

@MidnightDesign
Copy link
Contributor Author

You do get what I'm saying though, right? It would be just like the tests in https://github.com/DefinitelyTyped/DefinitelyTyped

@MidnightDesign MidnightDesign deleted the get-available-drivers-return-type branch January 3, 2023 10:56
@greg0ire
Copy link
Member

greg0ire commented Jan 3, 2023

We don't really have a concept for this kind of test in this repository, I guess.

We do actually, do we not? https://github.com/doctrine/dbal/blob/3.5.x/static-analysis/driver-manager-retrieves-correct-connection-type.php

@derrabus
Copy link
Member

derrabus commented Jan 3, 2023

Oh right, we have exactly one test! 🥳

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 4, 2024
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

3 participants