8000 Add @template return types for better autocomplete/static analysis by rdarcy1 · Pull Request #792 · PHP-DI/PHP-DI · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add @template return types for better autocomplete/static analysis #792

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

Merged
merged 1 commit into from
Sep 2, 2021

Conversation

rdarcy1
Copy link
Contributor
@rdarcy1 rdarcy1 commented Sep 1, 2021

PhpStorm now supports Psalm-style class string and template annotations (announcement). This works by default without needing to install or run Psalm on the project separately.

By adding template annotations to get, make and injectOn methods, we can get autocomplete without any inline docblocks or extra metadata files.

I think by keeping the existing type annotations and just unioning the new template types, it should work fine with IDEs that don't support this syntax (note I haven't tested on any other IDEs).

@mnapoli
Copy link
Member
mnapoli commented Sep 2, 2021

Thank you! I have one question: does that PR still supports non-class-string parameters/returned values?

Because yes, 90% of services are usually "class name -> class instance" mapping. But we don't want to break anything for services and values that are not indexed by a class name.

Here's an example to illustrate:

$container->set('foo', 'bar');

$abc = $container->get('foo');

Will static analysis report any issue?

@rdarcy1
Copy link
Contributor Author
rdarcy1 commented Sep 2, 2021

That's a good point – in that case the return type union with mixed covers it and the IDE just doesn't provide any completion.

Quick demo here, no errors on string/array and autocomplete on the object:

Screenshot 2021-09-02 at 10 39 32

@mnapoli
Copy link
Member
mnapoli commented Sep 2, 2021

Looks good, thank you for trying it out!

Let's give this a try. If there's ever an issue that we did not anticipate, it will not be the kind of issue that will break production suddenly, so I'm fine with releasing now and iterate if needed.

Thanks for the PR!

@mnapoli mnapoli merged commit b8126d0 into PHP-DI:master Sep 2, 2021
@rdarcy1 rdarcy1 deleted the class-string-params branch September 2, 2021 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0