Skip to content

Instantly share code, notes, and snippets.

@jwage jwage/.php_cs
Last active Feb 3, 2020

Embed
What would you like to do?
php-cs-fixer git pre commit hook
<?php
return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::SYMFONY_LEVEL)
->fixers([
'short_array_syntax',
'ordered_use',
])
;
#!/usr/bin/env bash
ROOT="/path/to/your/code"
echo "php-cs-fixer pre commit hook start"
PHP_CS_FIXER="vendor/bin/php-cs-fixer"
HAS_PHP_CS_FIXER=false
if [ -x vendor/bin/php-cs-fixer ]; then
HAS_PHP_CS_FIXER=true
fi
if $HAS_PHP_CS_FIXER; then
git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | while read line; do
$PHP_CS_FIXER fix --config-file=$ROOT/.php_cs --verbose "$line";
git add "$line";
done
else
echo ""
echo "Please install php-cs-fixer, e.g.:"
echo ""
echo " composer require --dev fabpot/php-cs-fixer:dev-master"
echo ""
fi
echo "php-cs-fixer pre commit hook finish"
@hxgdzyuyi

This comment has been minimized.

Copy link

hxgdzyuyi commented Jun 23, 2017

With the --config option you can specify the path to the .php_cs file. in new version.

@connorhu

This comment has been minimized.

@rbaarsma

This comment has been minimized.

Copy link

rbaarsma commented Sep 21, 2019

I found the running of the php-cs-fixer per line unnecessary slow, changed it to this:

FILES=` git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | tr '\n' ' '`
$PHP_CS_FIXER fix --config=.php_cs ${FILES}
git add ${FILES}

This simplifies the git lines into one single line, because php-cs-fixer can accept multiple paths. It's faster.

@softrebel

This comment has been minimized.

Copy link

softrebel commented Feb 3, 2020

if no php file exists in commit, php-cs-fixer run all over the php files of the repo. To avoid this, check FILES to not null then run php-cs-fixer.

FILES=` git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | tr '\n' ' '`
	if [ -z "$FILES" ]
	then
		  echo "php files not found."
	else
		  $PHP_CS_FIXER fix --config=.php_cs --verbose ${FILES}
		  git add ${FILES}
	fi

for more information visit here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.