Magento – Can’t trace code in PHPStorm

magento2phpstorm

I'm going to start by apologizing because I'm a little fuzzy on the terminology here.

I'm evaluating PHPStorm for use at work. Normally I use Eclipse because it's free and it works well most of the time. But I thought I'd give PHPStorm a shot. It's got some really nice features and integrates with Git pretty nicely.

But for whatever reason, I can't get code tracing/ctrl-click-on-a-function-and-it-takes-me-to-the-function-definition working on PHPStorm. I'm working in Magento 2 and that's a bit of a deal breaker for me.

So as an example, let's say I've got this phtml file

<?php

/** @var $block \MyCompany\MyModule\Block\Foo */

$helper = $block->_fooHelper;
$bar = $helper->doSomethingCool();

?>

<h2><?php echo $bar; ?></h2>

In Eclipse, I can ctrl-click on the doSomethingCool() function and it'll open up the class for _fooHelper and take me right to the function definition. But in PHPStorm all I get is a message saying

"Method 'doSomethingCool' not found. Referenced method is not found in subject class."  

I've searched around and ran the 'Detect PSR-0 Namespace roots" which came back and labeled the vendor and app/code folders as PSR-0 roots and then ran "Invalidate Cache/Restart" to reindex but I still get the same message.

Any ideas what I need to do to get the code tracing functionality I described working in PHPStorm?

EDIT: I just found that code tracing (i.e. ctrl-click on a function call) works with a type hinting pointing to the vendor folder like

/** var $block \Magento\Backend\Block\Template */

but does not work if it's pointing to something in the app/code folder like

/** var $block \MyCompany\MyModule\Block\Foo */

Best Answer

Namespace root is your most likely problem. PhpStorm absolutely supports the feature you're looking for, and it should work out of box.

Make sure your project encompasses the entire source code, then tell PhpStorm where to go looking, if it wasn't able to work it out on its own. See: https://www.jetbrains.com/help/phpstorm/2016.1/configuring-php-namespaces-in-a-project.html#d140255e161

Configuring Namespace Roots Manually

Open the Settings / Preferences Dialog by choosing File | Settings for Windows and Linux or PhpStorm | Preferences for OS X, and click Directories under Project:. The right-hand pane of the Directories page that opens shows all the content roots configured in the project. As a rule, there is one content root which is the root folder of the current project.

The central pane shows all the folders under the selected content root. Select the folder to be treated as the namespace root and click Sources.

It is perfectly fine to configure multiple source roots: PhpStorm will treat each of those as a namespace root and provide namespace hints for files underneath them. For example every subfolder of a vendor folder can be marked as a namespace root. In the example below, we have two namespace roots: one for the actual application and one for a vendor folder

At minimum, you want to set the 'vendor' folder as Sources, plus the app/code folder if it exists. Under normal circumstances, every module (and namespace) should be within those two locations.

Related Topic