Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

Enabling autocompletion for dynamic return types in PhpStorm

Link –

If you're using PhpStorm you probably make heavy use of it's autocomplete features. The IDE can, for most classes, suggests all accessible functions. Unfortunately, when a function has a dynamic return type PHPStorm will leave you out in the cold. So when resolving something out of Laravel's IoC-container PhpStorm cannot help you. Here's an example with one of the repositories of Blender:

Screen Shot 2015-12-04 at 20.42.42

PhpStorm can't suggest functions because it doesn't know which class the make-function will return. Let's fix that.

The DynamicReturnType plugin provides a way to dynamically specify the return type of a function. You can install it like every other PHPStorm plugin. Next you'll have to create a file called dynamicReturnTypeMeta.json in the root of your project with this contents:

{
    "methodCalls": [
        {
            "class": "\\Illuminate\\Contracts\\Foundation\\Application",
            "method": "make",
            "position": 0
        }
    ]
}
```


This configuration will tell PHPStorm that the return type of the `make`-function of `Illuminate\Contracts\Foundation\Application` will return an instance of class specified in the first argument.

With this file in place PHPStorm can perform autocompleting:

Screen Shot 2015-12-04 at 20.34.59

To learn all other possibilities the plugin offers, read it's documentation on GitHub.

Update: As Nicolas Widart mentions in the comments below there's another (and maybe easier way) to get autocompletion when resolving stuff from Laravel's IoC-container. The ide-helper package can generate a meta file that, when indexed, will make PhpStorm understand which object gets returned.
    

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "Enabling autocompletion for dynamic return types in PhpStorm"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.