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
Add support for conditional return type #3853
Comments
As a side note, here is my unsuccessful attempt at keeping the annotations for Psalm, and letting PHPStan fallback on a simpler version: https://phpstan.org/r/02c85102-05d0-410f-a76f-037bd4717ed3 I think that's because of the |
This would also be nice for dynamic return type extensions (some of the native ones would become obsolete, e.g |
FYI the annoying message about "unable to resolve template type" will no longer show up in these cases (because the template type is not present in the return type used by PHPStan): https://phpstan.org/r/3273047f-31d7-42bb-8763-357e10553eb5 Thanks to commit: phpstan/phpstan-src@a5f9f0c |
Can we dynamic initialize "dynamic return type extensions" for conditional return types? |
@voku Your question is expressed in a weird way but dynamic return type extension is what you typically implement today to achieve conditional types. |
Sorry, yes "dynamic dynamic" 🙏 is not the best way to ask a question about dynamic initialization. 😇 I wanted to know if its possible to use the current extension code for dynamic return types via phpdoc instead of added more classes. It would be much more easy to share this with others because they do not need to add the extra class, do not change the phpstan config and it would work out of the box. |
That's what this open feature request asks for, it's on my roadmap to implement this. |
Please refer to the Psalm documentation for conditional types[^0] for more information. Although the docs specify that template parameters are required, the reference-a-variable shorthand for templates also work, see the [example]. Usually this project seems to prefer `@phpstan-`-prefixes, but this construct is currently not available to PHPstan[^1]. [^0]: https://psalm.dev/docs/annotating_code/type_syntax/conditional_types/ [^1]: phpstan/phpstan#3853 [example]: https://psalm.dev/r/11cdedd7b3
@greg0ire PHPStan now reports different result with your code snippet: @@ @@
-13: PHPDoc tag @phpstan-return has invalid value (($params is array{wrapperClass:mixed} ? T : Connection)): Unexpected token "$params", expected type at offset 82
-18: Unable to resolve the template type T in call to method static method DriverManager::getConnection()
+13: PHPDoc tag @phpstan-return has invalid value (($params is array{wrapperClass:mixed} ? T : Connection)): Unexpected token "$params", expected type at offset 82 Full report
|
@greg0ire PHPStan now reports different result with your code snippet: @@ @@
-21: Unable to resolve the template type T in call to method static method DriverManager::getConnection()
+No errors |
Awesome! |
Conditional types aren't yet implemented. The "unable to resolve" error disappeared because in PHPStan-read |
Oh right, the message did not appear before because phpstan-bot was not implemented when the the release containing what you commented on was created? Anyway, that bot is great! |
Exactly :) Thank you! |
Please refer to the Psalm documentation for conditional types[^0] for more information. Although the docs specify that template parameters are required, the reference-a-variable shorthand for templates also work, see the [example]. Usually this project seems to prefer `@phpstan-`-prefixes, but this construct is currently not available to PHPstan[^1]. [^0]: https://psalm.dev/docs/annotating_code/type_syntax/conditional_types/ [^1]: phpstan/phpstan#3853 [example]: https://psalm.dev/r/11cdedd7b3
@greg0ire After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-21: Unable to resolve the template type T in call to method static method DriverManager::getConnection()
+16: Template type T of method DriverManager::getConnection() is not referenced in a parameter. Full report
|
@ondrejmirtes After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-No errors
+16: Template type T of method DriverManager::getConnection() is not referenced in a parameter. Full report
|
Part of implementation for phpstan/phpstan#3853
Part of implementation for phpstan/phpstan#3853
@greg0ire After the latest commit in 1.6.x, PHPStan now reports different result with your code snippet: @@ @@
-13: PHPDoc tag @phpstan-return has invalid value (($params is array{wrapperClass:mixed} ? T : Connection)): Unexpected token "$params", expected type at offset 82
18: Unable to resolve the template type T in call to method static method DriverManager::getConnection() Full report
|
…tional types See phpstan/phpstan/issues/3853#issuecomment-1082351763
About:
I looked into some and maybe there won't be too many. Sometimes a function looks like a good candidate, like But for example it looks like it could work for Also |
Feel free to go through them and submit them here to 1.6.x branch along with some tests, that would be helpful! |
…tional types See phpstan/phpstan/issues/3853#issuecomment-1082351763
First baby step for phpstan/phpstan#3853
I'll look into this myself:
|
@ondrejmirtes After the latest commit in 1.6.x, PHPStan now reports different result with your code snippet: @@ @@
-18: Unable to resolve the template type T in call to method static method DriverManager::getConnection()
+No errors |
I'm gonna do these two next:
|
Tomorrow I want to work on this one:
I think PHPStan 1.6.0 is on the right track to be released next week, but I don't want to promise anything :) |
I realized that all of these would have some overlap/duplications so this is going to be taken care of by a single rule:
|
I was playing with the conditional types and came up with this example from Psalm: https://phpstan.org/r/560549e5-71b5-47be-8da2-74d5d87089e1 I know it's still in work in progress, so I didn't want to open a new issue. Shouldn't it dump float in the second case? Because |
@canvural Generics don't work like that. If you have |
I know it is inferred as union. But the union |
|
Done: phpstan/phpstan-src@a8d9628 @rvanvelzen Please is there anything you want to add? I'd like to release PHPStan 1.6.0 later this week :) |
Very nice! The conditional never type was the only thing I ran into, so as far as I'm concerned it's good to go :) |
Awesome! I need to write an article about the flagship things in PHPStan 1.6.0, compile the release notes, and it's good to go :) |
@canvural Your example can be solved with: /**
* @param int|float $a
* @param int|float $b
* @return ($a is int ? ($b is int ? int : float) : float)
*/
function add($a, $b) {
return $a + $b;
} |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Feature request
This is a follow up of #3849 , and of doctrine/dbal#4264
I tried your suggestion, and it seems the root cause is conditional return types are not supported: https://phpstan.org/r/bdef5abe-d824-4311-aa6e-e0cce31ede92
Cc @pkruithof @morozov
The text was updated successfully, but these errors were encountered: