PHP Code Style
I recently decided I wanted to apply the PSR-2 strict coding style to my existing PHP code and classes and as result surprised myself how quickly I was able to adopt it naturally in the new code I was writing once I had some similar configuration set across text editors.
I have been a user of emacs for a number of years (as well as jEdit, TextMate and more recently Sublime Text 2) and with it's default indent style and my personal tendency of having curly braces on separate lines my PHP would typically have a form similar to this arbitrary example:
class front extends UserController
{
public function default_event()
{
if($this->isXMLHttpRequest())
{
header('Content-type: application/json');
$message = '';
if(isset($_POST['code']) && ctype_digit($_POST['code']))
{
$messages_arr = array(
'May onions grow out of your ears for two years!',
'May the last page of the next book you read turn blank so you never find out the ending.',
'May your next white wash have a red sock in it.',
'May the dog eat your homework.');
$code = (string)$_POST['code'];
if(strlen($code) == 5)
{
$message = $messages_arr[rand(0, sizeof($messages_arr)-1)];
}
echo json_encode($message);
}
else
{
echo json_encode('');
}
exit();
}
if(isset($_POST['forward']) && $_POST['forward'] == "1")
{
$this->redirect('login');
}
}
}
In order to do away with tabs altogether and use a standard four space indent, I needed to change some basic settings for emacs. I then wanted a method for applying this indentation across a whole file at will. I came across this answer to a Stack Overflow post to help me with that.
My new .emacs configuration file now looked like this:
(setq-default indent-tabs-mode nil)
(setq c-default-style "bsd"
c-basic-offset 4)
(defun indent-file (file)
"prompt for a file and indent it according to its major mode"
(interactive "fWhich file do you want to indent: ")
(find-file file)
;; uncomment the next line to force the buffer into a c-mode
;; (c-mode)
(indent-region (point-min) (point-max)))
The process I established for fixing the coding style of a given file was then twofold. First I opened the file in emacs and used M-x 'indent-file' (and then supplying the current filename again). Then I would run the PHP Coding Standards Fixer without any additional arguments on the command line as such:
$ php-cs-fixer fix ./file.php
The new form of the above class would then be more like this:
class front extends UserController
{
public function default_event()
{
if ($this->isXMLHttpRequest()) {
header('Content-type: application/json');
$message = '';
if (isset($_POST['code']) && ctype_digit($_POST['code'])) {
$messages_arr = array(
'May onions grow out of your ears for two years!',
'May the last page of the next book you read turn blank so you never find out the ending.',
'May your next white wash have a red sock in it.',
'May the dog eat your homework.');
$code = (string) $_POST['code'];
if (strlen($code) == 5) {
$message = $messages_arr[rand(0, sizeof($messages_arr)-1)];
}
echo json_encode($message);
} else {
echo json_encode('');
}
exit();
}
if (isset($_POST['forward']) && $_POST['forward'] == "1") {
$this->redirect('login');
}
}
}
Edit 01/02/2013: I have since learnt that this is called using 'K&R' bracing, named after one of my favourite programming books!