Features Releases

Go To Implementation in PhpStorm

At a round-table discussion with Ben Marks (of Magento fame), the developers from Class Llama mentioned that they had a gripe with the “Go to declaration” functionality in PhpStorm.

“Go to declaration” (default Cmd/Ctrl+click on a class name) is used so that you can quickly navigate through your codebase, to find the declaration of the code you are looking at. This is really useful functionality, and something I use every day, particularly when dealing with frameworks or other code I didn’t write personally. Find a type hint for a class I’m not familiar with? I simply Cmd+click it to be taken straight to its definition.

90% of the time, this is just fine, and its a real time saver for me, but there are use cases where this functionality is simply not helpful. Consider the following:

<?php

interface RequestInterface
{
}

class FirstRequestInterface implements RequestInterface
{
}

class SecondRequestInterface implements RequestInterface
{
}

class Consumer
{
    public function __construct(RequestInterface $request)
    {
    }
}

When we are looking at this code, it’s likely that we’ll want to learn more about the `RequestInterface` object that is being passed to the `Consumer` to understand fully what the dependency is here. Keep in mind that in the real world, the interface definition and both of the implementations would be in different files. Using “Go to declaration” here is not as useful, as seeing the source of the `RequestInterface` while useful, is probably not what we are looking to see. It’s much more probable that we will want to jump quickly to the code of the implementation of the interface that is being injected.

I agreed with this wholeheartedly, I’ve hit this snag myself many times, most often when I’m clicking on the `RequestInterface` injection in a Zend Framework application and I’m taken to the interface definition. I know that this is an instance of the `PhpRenderer\Request` object, but I would love an easy way to jump to that definition.

As usual in this situation, I agreed to speak to the developers, and (again, as per usual), when I got back to the booth I was lucky enough to have a developer right there for me to mention it to. It comes as no surprise that I learned that this functionality already exists. Instead of using “Go to declaration”, use the “Go to Implementation” action instead (default Cmd/Ctrl+Alt+B); this will ask you which implementation of the interface you wish to navigate to instead. Much more useful!

Go to implementation

 

I’m tempted to map this to a more natural key combination, I’m thinking something like Cmd+Shift+click to make navigating to where I want to go even easier.

Give it a try and let me know what you think.

— Gary and the PhpStorm Team

The Drive to Develop

 

image description