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 PHPStan/Psalm templates #124

Open
BackEndTea opened this issue Jul 14, 2019 · 6 comments
Open

Add PHPStan/Psalm templates #124

BackEndTea opened this issue Jul 14, 2019 · 6 comments

Comments

@BackEndTea
Copy link

Currently methods like preg_replace have a return type of string|array specified.
When in reality that return type is dependant on the input.

Static analysis tools, like PHPStan and Psalm can be made aware of this by using @template annotations, like so:

https://phpstan.org/r/4f0fc5ec-ad1a-45e8-a592-a46106267d67

This means that the tools can understand what exactly the output is going to be, if they can infer the input types.

Would it be possible to add those annotations to the generated code?

@moufmouf
Copy link
Member

moufmouf commented Aug 1, 2019

I don't think there is a way to automatically "generate" the @template annotations.
Code is generated from the PHP documentation and from the functionMap file

I cannot analyze those files automatically.

But the idea is very good, and I could make exceptions for those functions and have them written "by hand".

Now, the question is: where can I find a complete list of such functions? Would you have an idea how I can find those?

@BackEndTea
Copy link
Author

I think @ondrejmirtes may know that for phpstan, or @muglug for psalm

@ondrejmirtes
Copy link

Some situations can be described by generics, but a lot of them are more complex and require custom logic. PHPStan has "dynamic return type extensions" for this purpose and they're here in this directory (besides type-specifying extensions for functions like assert or is_int):

https://github.com/phpstan/phpstan/tree/master/src/Type/Php

The problem is that they're not 1:1 reusable with Safe because Safe eliminates some return failure types. The best approach would probably be to copy-paste them to PHPStan Safe extension and change what's needed...

@Kharhamel
Copy link
Collaborator

@ondrejmirtes now that phpstan does implement templates, do you think we can generate them in safe?

@ondrejmirtes
Copy link

ondrejmirtes commented Dec 13, 2019

You might be able to describe some situations with generics, but not everything.

@simPod
Copy link
Contributor

simPod commented Mar 4, 2020

I found eg. this https://github.com/vimeo/psalm/blob/master/src/Psalm/Internal/Provider/ReturnTypeProvider/StrReplaceReturnTypeProvider.php

Perhaps we should be able to port this logic into something like thecodingmachine/safe-psalm-plugin. WDYT?

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

No branches or pull requests

5 participants