PHP: Curly Brackets Substring Access

I just have another lighter PHP specific Code Smell: Curly Brackets Substring Access. If you’re coding PHP since a longer time you might not only know but also have used curly brackets for index-/char-based substring access.

It’s somehow still working these days but can be easily mixed with other curly brackets usage. Therefore it was suggested for a long time to use the alternative square brackets syntax instead – and I haven’t met a coder since ages who does not use square brackets for that.

With the Release of PHP 5.3, curly brackets are finally deprecated for this. So out with them, right? But there is always value, even in this outdated looking stuff you might think of “WTF” when found in code: You can use it to locate old code. Great for quality assurance. Next to that it is pleasing to do a little maintenance and update the code for PHP 5.3,right? In this post I’ll show how to locate the smell and to fix the places at once in a real-life-example.

For those who are curious: I stumbled over it while hacking the wordpress core some time ago, a related ticket is #13900. So this post is based on the wordpress codebase which is somehow a good example but you can experience differences in other projects. So please let me know in the comments if you had not that much luck with my Regex patterns.

Locate and Fix

So for the first run I was looking for a Regular Expression that does search (and replace) it. I came up with this one:

Search:
(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\{([^\}\$][^\}]*)\}

Replace:
\1\[\2\]

Even this is a regex which seems to work pretty good, it can not understand the underlying syntax. That is why I go through each of the changes and review them manually. This works pretty well in Eclipse, I already wrote about that. So this first line was relatively safe, I had not false positives with this one on the whole code so far.

Now I counter-checked with a more broad pattern to find additional places:

(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\{([^\}]+)\}

That one catches everything incl. variable substituion in double-quoted strings ( $test = "$foo{$bar}";), so it is unsafe to be used for automatic replacement. But it will still find valid places which should be replaced. Luckily there are other criteria, like the filename, so I was able to replace relatively safe in a file-based manner.

The result of the session can be found in the according patch, so you can see what happened.

About these ads
This entry was posted in Code Smells, Hacking The Core, PHP Code Smells and tagged , , , , , , . Bookmark the permalink.

2 Responses to PHP: Curly Brackets Substring Access

  1. scribu says:

    I didn’t even know about the square bracket syntax. Nice catch!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s