Stepping into chained method calls with XDebug and PHPStorm

Or Shift F7 where have you been all my life?

More and more frameworks are providing API’s that are chainable. It’s descriptive, concise and the in thing.

So, instead of code like this:

We have this:

Much less clutter, and easy to implement. To make this coding style work all these chainable methods need to return their instance value:

But upto now I’ve had a big problem with this syntax. Not with the implementation or appearance but using xdebug breakpoints.

Say I have problems with the ‘order’ method that I want to investigate with XDebug and PHPStorm. With the chainable syntax how do we get there?

Set a breakpoint for the line ‘$select = $dbAdapter->getSelect()’ and then use ‘Step into’.

This steps into the ‘getSelect’ method. Not what we want. So we step over till the end of that method and step over again which lands us in the ‘columns’ method. It’s tempting to use step out while in the ‘getSelect’ method, but that gets us out of the whole chain so we miss everything. Still we are in ‘columns’ now. Not what we want, but closer!

So again repeat this method till we land in the ‘order’ method. Then the debugging can be done. We could of course have put the breakpoint in the ‘order’ method in the first place but this assumes we know what object ‘getSelect’ is going to return. And with big frameworks and modular architectures this is not always obvious.

So this drove me crazy. I just wanted to be able to highlight the method to step into or choose it from a list or something.

And that’s what PHPStorm gives you, I just didn’t know where to find it. I’d always been looking at options in the debugging panel. It would be with ‘step over’ and ‘step into’ buttons right? Wrong!

It’s in the top ‘Run’ menu.

And I never go in any of the top menus. Ever. Well except for creating new projects.

Since PHPStorm gives me contexual menus, panel buttons and keyboard shortcuts galore I’d never though to look in the top menus.

And there it is:

Smart step into

menu

This give’s exactly what’s required. A popup menu allowing you to choose which method to step into.

And a keyboard shortcut to commit to muscle memory.

So thanks again PHPStorm but please add a ‘smart step into’ button on the debug panel so it’s easier for me to find!

Setting XDebug breakpoints for remote CLI scripts with PHPStorm

PHPStorm makes setting and using XDebug breakpoints easy. Just define the server configurations and click the phone icon to listen mode and load your site in a browser. With the correct xdebug.ini parameters it works if your application is either running on the same machine as PHPStorm or on a remote server.

Running CLI scripts on a different server to the machine PHPStorm is running on require a bit more effort though.

First ensure the phone icon is active so PHP Storm is listening for connections.

Again ensure server configurations are setup for your project. There will be a server name and configuration name.

On the remote server define the server name that PHPStorm should listen for:

export PHP_IDE_CONFIG=”serverName=mbp.uko4.com”

Now execute the CLI script on the remote machine.

If the server name you specify matches the server name you have defined in your project execution will halt on your breakpoints.

Or will it?

For me this works fine for some environments. When this is not enough export some more:

export XDEBUG_CONFIG=”idekey=PHPSTORM”

to get it going and

unset XDEBUG_CONFIG

to stop it. Nice!