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

Add template support to callable property phpdoc #8964

Open
mvorisek opened this issue Feb 26, 2023 · 10 comments
Open

Add template support to callable property phpdoc #8964

mvorisek opened this issue Feb 26, 2023 · 10 comments

Comments

@mvorisek
Copy link
Contributor

mvorisek commented Feb 26, 2023

Feature request

https://phpstan.org/r/fb6ebb4f-3db2-4c46-99b3-be10a1afacf5

https://phpstan.org/r/7e370b44-57fa-4bcf-9a1d-24a0bbf3834e

Closure type in property should be possible to describe the same way as in method phpdoc.

@ondrejmirtes
Copy link
Member

What you're asking for is generic callables/closures: Closure<T>(T): T.

@template above a method makes the method generic, but not the closure.

This is very easy to achieve since both CallableType and ClosureType can contain TemplateTypeMap. We need support for this syntax in phpstan/phpdoc-parser.

@ondrejmirtes ondrejmirtes added this to the Generics milestone Feb 26, 2023
@phpstan-bot
Copy link
Contributor

@mvorisek After the latest push in 1.10.x, PHPStan now reports different result with your code snippet:

@@ @@
 13: PHPDoc tag @var for property Cl::$prop contains unresolvable type.
-13: Property Cl::$prop has no type specified.
+13: Property Cl::$prop has no type specified.
+19: Tip: Method Cl::method() always throws an exception, it should have return type "never".
Full report
Line Error
13 PHPDoc tag @var for property Cl::$prop contains unresolvable type.
13 Property Cl::$prop has no type specified.
19 Tip: Method Cl::method() always throws an exception, it should have return type "never".

@phpstan phpstan deleted a comment from phpstan-bot Apr 13, 2023
@mvorisek mvorisek changed the title Add template support to property phpdoc Add template support to callable property phpdoc Apr 15, 2023
@dktapps
Copy link
Contributor

dktapps commented Apr 21, 2023

Fixed a mistake in your example: https://phpstan.org/r/fb6ebb4f-3db2-4c46-99b3-be10a1afacf5

@mvorisek
Copy link
Contributor Author

mvorisek commented Jun 22, 2023

@dktapps yes, thank you, I have updated the description. (if anyone read this, there was a typo CLosure instead of Closure, although it should be the same as CN are CI, phpstan does some matches in CS fashion)

@mvorisek
Copy link
Contributor Author

mvorisek commented Sep 1, 2023

What you're asking for is generic callables/closures: Closure<T>(T): T.

@template above a method makes the method generic, but not the closure.

This is very easy to achieve since both CallableType and ClosureType can contain TemplateTypeMap. We need support for this syntax in phpstan/phpdoc-parser.

Hi @ondrejmirtes, implemented in phpstan/phpdoc-parser#199, can I please ask you to review it?

@mvorisek
Copy link
Contributor Author

@param callable template support has been added in phpstan/phpstan-src#2938

however assign to a property is still not working - https://phpstan.org/r/7e370b44-57fa-4bcf-9a1d-24a0bbf3834e

/cc @mad-briller

@ondrejmirtes
Copy link
Member

Closures do not support PHPDocs above them, you can't mark your closure as generic.

There's an open feature request about PHPDocs above closures.

@mvorisek
Copy link
Contributor Author

mvorisek commented Feb 25, 2024

Closures do not support PHPDocs above them, you can't mark your closure as generic.

You probably opened the (old) repro from this issue description, in the last post, I have linked sensual repro. To avoid confusion, I have linked it in the issue description as well.

@ondrejmirtes
Copy link
Member

The question is - why it works for methods and not for property assignments. I'm not really sure.

Another question is - how these should be validated in regard to generics and TemplateTypeMap, I'm also not sure.

@mvorisek
Copy link
Contributor Author

mvorisek commented Mar 7, 2024

Here is a little better repro - https://phpstan.org/r/bb9171d8-60f4-477c-9b2a-3e8d867d3a74

$o->prop = function (Model $c) {};

passes but

$o->prop = function (ModelCountry $c) {};

emits phpstan error.

It seems the callable template of the property is resolved before the assign.

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

No branches or pull requests

4 participants