Skip to content

Instantly share code, notes, and snippets.

@ricogoh
Last active March 4, 2020 02:12
Show Gist options
  • Save ricogoh/29129c909366eec43d2d69425a30ce8e to your computer and use it in GitHub Desktop.
Save ricogoh/29129c909366eec43d2d69425a30ce8e to your computer and use it in GitHub Desktop.
PHP Coding Standards Fixer setting for Laravel 5 on FriendsOfPHP/PHP-CS-Fixer 2.*
<?php
/**
* PHP Coding Standards Fixer setting for Laravel 5 on friendsofphp/php-cs-fixer 2.*
* Rules with psr1, psr2, phpdoc and some symfony rules.
* Rules only apply directory and sub-directory inside "app".
*
* Save script to .php_cs and place .php_cs file in the root of your Laravel project
*
* Requirement:
* composer global require friendsofphp/php-cs-fixer
*
* OR install locally
* composer require friendsofphp/php-cs-fixer --dev
*
* Run with command:
* ./vendor/bin/php-cs-fixer fix --config=.php_cs --verbose
* ./vendor/bin/php-cs-fixer fix --config=.php_cs --verbose /path/to/your/file_or_dir
*
* With composer:
* Add below lines to composer.json
* "scripts": {
* "php-fixer": [
* "php-cs-fixer fix --config=.php_cs --verbose"
* ]
* }
*
* then run
* composer run php-fixer
*/
$fixers = [
'@Symfony' => true,
'@PSR1' => true,
'@PSR2' => true,
'yoda_style' => false,
'array_syntax' => ['syntax' => 'short'],
'no_short_echo_tag' => true,
];
$finder = PhpCsFixer\Finder::create()
->notName('*.blade.php')
->exclude('vendor')
->in(__DIR__.'/app/');
return PhpCsFixer\Config::create()
->setRules($fixers)
->setUsingCache(false)
->setFinder($finder);
#!/usr/bin/bash
ROOT="./"
echo "php-cs-fixer pre commit hook start"
PHP_CS_FIXER="/PATH_TO_COMPOSER_VENDOR_BIN/Composer/vendor/bin/php-cs-fixer"
HAS_PHP_CS_FIXER=false
if [ -x $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=.php_cs --verbose "$line";
git add "$line";
done
else
echo ""
echo "Please install php-cs-fixer, e.g.:"
echo ""
echo " composer global require friendsofphp/php-cs-fixer"
echo ""
fi
echo "php-cs-fixer pre commit hook finish"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment