How To Easily Navigate Your Codebase With PhpStorm

How To Easily Navigate Your Codebase With PhpStorm

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.


You might also be interested in these tutorials too...

Setup Step Debugging in PHP with Xdebug 3 and Docker Compose
Wed, Mar 10, 2021

Setup Step Debugging in PHP with Xdebug 3 and Docker Compose

In versions of Xdebug before version 3 setting up step debugging for code inside Docker containers has often been challenging to say the least. However, in version 3 it’s become almost trivial. In this short tutorial, I’ll step you through what you need to do, regardless of the (supported) text editor or IDE you’re using.

How to Run Tests in PhpStorm
Fri, Jan 3, 2020

How to Run Tests in PhpStorm

PhpStorm offers so much functionality. From syntax highlighting to Docker integration, it’s an extremely comprehensive tool. However, have you ever thought of using it to run your unit tests? In this article, I step you through running tests, from an entire suite to an individual test.

How to Set Up PHP Debugging with PhpStorm, Xdebug, and PHPUnit
Sun, Nov 3, 2019

How to Set Up PHP Debugging with PhpStorm, Xdebug, and PHPUnit

Still using var_dump to debug your PHP code? Stop! While var_dump can be convenient, it’s a very blunt approach. In this article, I’ll show you how to set up proper debugging with PhpStorm, Xdebug, and PHPUnit, and give you a modern, sophisticated debugging experience.

Vim - The Distraction Free Editor
Thu, Jul 20, 2017

Vim - The Distraction Free Editor

A little while ago, I took to Twitter in a sense of jubilant excitement announcing that VIM was THE distraction-free editor. As it’s been quite some time since, I honestly don’t remember exactly what it was that motivated me to do so.


Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus