How To Easily Navigate Your Codebase With PhpStorm

How To Easily Navigate Your Codebase With PhpStorm October 6th, 2016, by Matthew Setter

If there’s one thing that we as developers to do on a regular basis it’s navigate through code. In this tutorial, I’m going to show you a range of ways in which PhpStorm does so, minimising the effort required by us.

Specifically, we’re going to see how to perform the following code navigation:

  1. To a function definition
  2. To a class definition
  3. To a parent, or super, method

Whilst there are a range of other navigation types on offer, such as navigating to the previous or next file, type declaration, or Emmet edit point, these are the ones that I do on a regular basis.

In addition to these, I’m going to show you another kind of navigation, the “show usages” navigation. But more on that later. For right now, let’s get started seeing how to navigate to a function definition.

Navigating to a function is a very handy thing to do, especially if we’re either not intimately familiar with the function, or are only using it for the first time; well that, and if the documentation is rather light on — even non-existent.

Navigate to a function can be performed in several different ways.

Firstly, let’s see how to do it with the mouse. In a segment of code which makes a call to the function, move the mouse over the function call, and either cmd+click it on a Mac or ctrl+click it on Windows or Linux (assuming that you’re using the standard keyboard mappings).

Alternatively, you can place the cursor somewhere in the function’s name and use the keyboard shortcut cmd+b (Mac) or ctrl+b (Windows/Linux).

Then there’s a third way, one that I’m a huge fan of. Regardless of whether you’re in a segment of code that makes a call to the method, or have no files open, use the keyboard shortcut alt+cmd+o or alt+ctrl+o which will open a mini popup, where you can enter the function’s name (or symbol name).

"PhpStorm Navigation - Find by symbol"

Note: With this approach is that you will have to ensure that the option you pick from the matching list is the one that you want to view.

Taking this approach will have PhpStorm find any definition that matches the criteria you supply. You may end up, inadvertently, navigating to the wrong place.

Regardless of which approach you take, if PhpStorm’s index is up to date, it will move you to the function’s definition, where you can find out all that you need.

Now that we’ve navigated to a function definition, what about navigating to a class definition. Needless to say, it works quite similarly.

Assuming that you know the class’ name, or perhaps even aren’t sure, but have a vague idea of what it’s called, use the keyboard shortcut cmd+o or ctrl+o, which will open a mini popup similar to the function, or symbol, mini popup.

"PhpStorm Navigation - Find by class"

There, as before, start typing in the name of the class which you want to navigate to. As the list starts to populate, based on the text you are entering, click the one that is the correct match.

As with function navigation, you’ll be taken to the class, where the caret (or cursor) will be placed at the top of the file. You can also use the symbol popup as well, as both function and class names are symbols.

You can also navigate with the mouse. Say that you were working with the following code snippet, and you wanted to have a look at the RedirectResponse object.

class UserAuthenticationMiddleware
{
    public function __invoke(Request $request, Response $response, $next)
    {
        $session = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);
        $session->get('id');

        if ($session->get('id') === null && strpos($request->getUri(), '/login') === FALSE) {
            return new RedirectResponse('/login', 302);
        }

        return $next($request, $response);
    }

}

As with function navigation, you could either cmd+click or ctrl+click the class’ name, or move the cursor in to the class’ name and click cmd+b or ctrl+b. After doing so, you’ll be move to the class’, specifically in to the constructor, if one is defined.

Navigating to a parent method is rather similar. Since I’ve shown all of the various ways (including keyboard, menu, and mouse), I’m just going to focus on the mouse for this example.

Say you’d overridden a method, when you created a class which inherited from another and you need to see what the parent method does, even if for no other reason than jogging your memory.

To do so, cmd or ctrl click the function’s name to be taken to its parent’s implementation.

Show Class & Function Usages

Let’s now finish up by making use of a navigation option which has saved me countless time, finding usages. Sometimes you need to know where a class or function was used, but to use a file search, or command-line grep would perhaps take to long.

Powerful as they are, these approaches are broader, more general ways of searching. PhpStorm comes built-in with a laser-focused strategy for helping us out. The animation’s kinda sweet too.

Say that we want to find the usages for the class in the code snippet above. To do that, we can either cmd or ctrl click the class’ name, use alt+f7, or alt+shift+f7.

"PhpStorm Navigation - Show Usages"

The first and last will popup a window listing all of the usages, where we can click one to be taken to it. The second opens up the find panel, where you can see all the usages of the class in a tree view.

"PhpStorm Navigation - Find Usages"

This includes invocations of the class, import statements, and more.

In Conclusion (tl;dr)

And that’s four of the top ways to navigate through your codebase(s) with PhpStorm. Whilst other editors and IDEs provide similar functionality, I don’t believe that any of them provide such a well-rounded, or mature an implementation as PhpStorm does.

What navigation types do you use most often? Share your thoughts in the comments.


I’m Creating a Comprehensive PhpStorm Course

You'll learn everything there is to know. When you’re done, you'll be developing like a coding super hero. Sign up now and get it discounted at launch!