Skip to content

Commit

Permalink
Add php-cs-fixer code style pre-commit hook
Browse files Browse the repository at this point in the history
Check staged files for code style correctness and abort
if any file fails a style check test.

Reorganise repository to allow per-hook documentation.
  • Loading branch information
ajbonner committed Mar 23, 2014
1 parent c2350f2 commit 137544a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
File renamed without changes.
13 changes: 13 additions & 0 deletions php-cs-fixer-pre-commit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# PHP Code Style Fixer Pre-Commit

To use this pre-commit hook, you will need to have [php-cs-fixer.phar](http://get.sensiolabs.org/php-cs-fixer.phar) somewhere in your path.

This pre-commit hook will look at files being commited if the file

- has a .php extension
- is inside the local code pool, i.e. app/code/local
- is not a controller (which do not obey PSR-0 class name -> path conventions)

Qualifying files will be run through php-cs-fixer.phar to check for code style issues. If an issue is found, a diff of suggested changes will be displayed and the fixers that failed listed. The commit will then be aborted.

To force a commit to bypass these checks run git commit with the --no-verify argument.
23 changes: 23 additions & 0 deletions php-cs-fixer-pre-commit/php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$finder = Symfony\CS\Finder\DefaultFinder::create()
->name('*.php')
->name('*.phtml')
->name('*.xml')
->exclude(array(
'lib',
'shell',
'app/Mage.php',
'app/code/core',
'app/code/community',
'app/design/frontend/default',
'app/design/frontend/enterprise/default',
'app/design/frontend/base',
'app/design/adminhtml/default')
)
->notName('*Controller.php')
->notName('*ControllerTest.php')
->in(__DIR__);

return Symfony\CS\Config\Config::create()
->finder($finder);
18 changes: 18 additions & 0 deletions php-cs-fixer-pre-commit/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

ABORT_COMMIT=false

while read -r FILE; do
if [[ $FILE =~ 'app/code/local' ]]; then
php-cs-fixer.phar fix --dry-run --diff -v $FILE
if [ $? -gt 0 ]; then
ABORT_COMMIT=true
fi
fi
done < <(git diff-index --name-only --diff-filter=ACM --cached HEAD | \
egrep '\.(php)$' | egrep -v 'Controller.php$')

if [ $ABORT_COMMIT = true ]; then
echo "Code style errors found, aborting commit"
exit 1
fi
File renamed without changes.

0 comments on commit 137544a

Please sign in to comment.