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!

PHP pound sign not rendering correctly

You know how it is, your web page should have pound signs before those quantities of money but instead it’s using some alien hieroglyphic.

This was tracked down to the htmlentities php command, the very command to ensure this doesn’t happen!

It actually needs a few arguments to indicate the page encoding. So the following did the trick:

Configuring Snow Leopard as a SLAMP box (Snow Leopard Apache MySQL PHP)

#install xdebug

http://aspn.activestate.com/ASPN/Downloads/Komodo/RemoteDebugging

Enter the “5.3” directory and find the file named “xdebug.so”. You need to move this file to the/usr/lib/php/extensions/no-debug-non-zts-20090626 directory. First, open a new Finder window and press Cmd + Shift + G. Enter /usr/lib/php/extensions/no-debug-non-zts-20090626/ into the prompt that appears and press “Go”. This will open the destination directory in the Finder window. Drag xdebug.so into the destination directory. You may be prompted for your administrator password. If the /usr/lib/php/extensions/no-debug-non-zts-20090626/ directory does not exist, locate the /usr/lib/php/extensions/ directory instead. This directory will contain a directory named something similar to no-debug-non-zts-20090626. Place xdebug.so into that directory instead.
edit php.ini:
1.[xdebug]
2.zend_extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so

Creating a parent-child object heirachy to display in tree view

I had a module that required display of a set of categories organised by id and parent id. To create a tree view I created a node object and small bit of code to render a HTML drop down form element.

Of course after creating this code I realised this information was in fact surplus to requirements and would potentially confuse users.

The code took too much love to delete so here it is for next time…

Community Builder broken in the admin console…

Had a very strange fault today whilst working on a community builder Joomla site. I had a version of the site on 2 different servers. Both working fine but the sites were using Community Builder 1.1.x, which had a security warning. So 1 upgrade later, both sites are fine again and running 1.2.1

But this was only the front-end. In the Joomla backend of one site none of the action icons for community builder were showing. There was a text hyperlink in their place, but clicking them had no effect. Creating a new user was impossible as the save button had no effect.

So, I had one site 100% fine and another with the problems in the admin console.

I assumed something had corrupted during the upgrade of the problem site so I had a sniff around:

Copied both sites my machine. Ran a diff. Everything the same.

Got a MySQL dump off both sites. Ran a diff. Both the same.

Running out of options now, filesystem and db are the same.

Permissions? Checked ok.

PHP version? Ahhhh – ok site running under PHP5, problem site with PHP4.

Kicking the server onto PHP5 sorted it. This is bound to be a requirement stated in the CB docs, which I must of missed.

So make mental note – community builder backend only works in PHP5. I don’t mind this, PHP5 is wayyy superior to 4. Upgrade all servers now!