Inserting a function into all classes in a directory via Terminal

Or; Multi-line Search and Replace regular expression on the Command Line.

The situation

You have a single abstract class which is extended multiple times in 10 or 100 different classes throughout your codebase.

You’ve found a need for a new abstract method which all classes must now implement.

The problem

Who wants to manually insert that new code into all those classes!? (I don’t).

We need an automated way to do this

The solution

ack -l "extends MyClass" | xargs perl -i -pe 'BEGIN{undef $/;} s/(extends MyClass[^{]*{)/\1\r\n\r\n\tpublic function newMethod() {}\r\n\r\n/smg'

How it works

Breaking this up into its parts goes like so:

ack -l "extends MyClass"

Find all files which include the string “extends MyClass” and list them.

xargs perl -i -pe '[...]'

Do an in-place perl regular expression search and replace on each file found from above.

BEGIN{undef $/;}

Tell Perl to no longer consider end-of-line’s – this allows us to do a multi-line search and replace.

s/[SEARCH]/[REPLACE]/smg

Look for all occurrences of [SEARCH] in the file and replace it with [REPLACE].

[SEARCH]

(extends MyClass[^{]*{)

Match all occurrences of the string “extends MyClass” followed by any characters up until the opening brace (“{“) (remember this also includes new lines!), so it will successfully match all of the following:

extends MyClass{

extends MyClass {

extends MyClass
{

extends MyClass
/* Comment about class */
{

[REPLACE]

\1\r\n\r\n\tpublic function newMethod() {}\r\n\r\n

“\1″ will leave what was matched (in the braces “(” and “)” in the result).

“\r\n\r\n” inserts two new lines.

“\t” inserts a tab.

“public function newMethod() {}” inserts the new method.

“\r\n\r\n” inserts a further two lines

Testing Cron Jobs for the correct user

Testing Cron Jobs in Linux can be a pain – especially if you’re using a tool like CPanel which abstracts away some of the processing. However, with a couple of quick commands, it’s possible to track down where your cron jobs may be failing and get them running smooth as silk.
Continue reading

The Terminal 101

What is the Terminal?

The Terminal is simply a way to execute commands and to view the files on your computer. It is very similar to the Graphical User Interface that allows you to click around and shows you icons of files. The Terminal, however, is a text only environment.

The text only environment is a common source of frustration as it can often times be hard to see why a command didn’t work (eg; a typo), or know exactly which directory you’re in, or any number of other new concepts for first time users of the Terminal. Continue reading

Zend Framework Quickstart with MySQL

The Zend Framework Quickstart guide is a great place to start with the Framework, however it bases the example program on SQLite. As MySQL is the choice of the rest of the world (and even Zend’s own Stack installation), here is the Zend Framework Quickstart done with MySQL.

Continue reading

PHPUnit Bootstrap and Autoloading classes

The PHPUnit Bootstrap is perfect when there is code to be run before tests are executed. The limitation however is there can only be one bootstrap per PHPUnit configuration file.

This is an issue if there are a set of classes that need to be included – we don’t want to manually include every class every test could possibly need.

PHP’s Autoloading feature comes to the rescue, allowing us to seamlessly include classes as they’re required for tests.
Continue reading