Introduction

Before making any changes to the existing ConFOMO codebase, I thought it best to get an understanding of the style of the code I ought to be writing in order to make reviewing pull requests a quicker process. In addition, I took a look at some of Matt's other projects and came up with a set of PHP-CS-Fixer rules to ensure the style is enforced, with no additional effort on my part.

Configuring PHP-CS-Fixer

Once I had identified the necessary rules, I put together the necessary .php_cs configuration file.

<?php

return Symfony\CS\Config\Config::create()
    ->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
    ->fixers([
        // Logical NOT operators (!) should have one trailing whitespace.
        'logical_not_operators_with_successor_space',
        // PHP multi-line arrays should have a trailing comma.
        'multiline_array_trailing_comma',
        // Ordering use statements (alphabetically).
        'ordered_use',
        // Remove line breaks between use statements.
        'remove_lines_between_uses',
        // An empty line feed should precede a return statement.
        'return',
        // PHP arrays should use the PHP 5.4 short-syntax.
        'short_array_syntax',
        // Cast "(boolean)" and "(integer)" should be written as "(bool)" and "(int)". "(double)" and "(real)" as "(float)".
        'short_scalar_cast',
        // PHP single-line arrays should not have a trailing comma.
        'single_array_no_trailing_comma',
        // Arrays should be formatted like function/method arguments, without leading or trailing single line space.
        'trim_array_spaces',
        // Unalign double arrow symbols.
        'unalign_double_arrow',
        // Unalign equals symbols.
        'unalign_equals',
        // Unused use statements must be removed.
        'unused_use',
]);

This uses the default PSR-2 rules, along with some additional rules I've picked up from review of existing codebases. Note that these rules apply specifically to the ConFOMO codebase, so you'll need to adapt them for your own projects.

Configuring a git pre-commit hook

A pre-commit hook is a simple bash script that will be executed when you run git commit, but before the operation is actually executed. This script will look for any PHP files that were added, copied, or modified files and run them through php-cs-fixer - assuming that it is installed globally and available in your $PATH.

Create the file .git/hooks/pre-commit in your project directory with the following content and make it executable with chmod g+x .git/hooks/pre-commit.

#!/bin/bash

echo "Running PHP-CS-Fixer"

CONFIG_FILE=.php_cs

if [ ! -e $CONFIG_FILE ];
then
    echo "$CONFIG_FILE does not exist. Please configure php-cs-fixer."
    exit 1
fi

while read -r file;
do
    file="$(echo -e "${file:1}" | sed -e 's/^[[:space:]]*//')"
    if [[ $file = *.php ]];
    then
        /usr/bin/env php-cs-fixer fix "$file"
        git add "$file"
    fi
done < <(git diff --cached --name-status --diff-filter=ACM)

Sample files

Here's a sample PHP file before it was run through the pre-commit hook:

<?php

namespace SomeNamespace;

// This file is actually named src/Testing.php 
// and src/ is PSR-4 mapped to SomeNamespace.
class Testing {
    /**
     * @var boolean
     */
    private $property;

    public function __construct($property = false)
    {
        $this->property = $property;

        $singleLineArray = [ 'spaces', 'and', 'trailing', 'comma', 'should', 'be', 'trimmed', ];

        $multiLineArrray = [
            'the'    => 'double',
            'arrow'  => 'symbol',
            'should' => 'not',
            'be'     => 'aligned',
        ];

        $shortArrayConversion = array( 'change', 'this', 'to', 'short-array', 'syntax', );

        $logicalNotTest = !empty($singleLineArray);

        $boolean = TRUE;

        if ($boolean) {
            echo "it's good";
        } else if (!$boolean) {
            echo "it's not good";
        } else {
            echo "This is pointless";
        }
    }
}

And the same file after it was automatically run through the fixer:

<?php

namespace SomeNamespace;

// This file is actually named src/Testing.php 
// and src/ is PSR-4 mapped to SomeNamespace.
class Testing
{
    /**
     * @var bool
     */
    private $property;

    public function __construct($property = false)
    {
        $this->property = $property;

        $singleLineArray = ['spaces', 'and', 'trailing', 'comma', 'should', 'be', 'trimmed'];

        $multiLineArrray = [
            'the' => 'double',
            'arrow' => 'symbol',
            'should' => 'not',
            'be' => 'aligned',
        ];

        $shortArrayConversion = ['change', 'this', 'to', 'short-array', 'syntax'];

        $logicalNotTest = ! empty($singleLineArray);

        $boolean = true;

        if ($boolean) {
            echo "it's good";
        } elseif (! $boolean) {
            echo "it's not good";
        } else {
            echo "This is pointless";
        }
    }
}

Conclusion

In using the pre-commit hook, the rules as defined by the project have been automatically applied. This was done with only minor effort on my part. If the PHP project you're working on has a .php_cs file in its repository, it might be worth adding this pre-commit hook. Your open source containers will love you for it!

Insider information

Matt live-streamed his PR process, so I got a bit of information about his PR thought process and some of the conventions he follows in his own projects. Check it out if you've got a spare 40 minutes.