Skip to content

Instantly share code, notes, and snippets.

@vanodevium
Created September 18, 2019 08:59
Show Gist options
  • Save vanodevium/1d5be7e1387ec0da1073671fb370e0e7 to your computer and use it in GitHub Desktop.
Save vanodevium/1d5be7e1387ec0da1073671fb370e0e7 to your computer and use it in GitHub Desktop.
pre-commit configuration for laravel projects

Pre-commit installation

  • Add hooks configuration into extra segment of composer.json:
"extra": {
	"hooks": {
		"pre-commit": [
	   		"./vendor/bin/php-cs-fixer fix",
	       "git add -u"
	    ]
	}
}
  • Add scripts into scripts segment of composer.json:
"scripts": {
	"lint": "./vendor/bin/php-cs-fixer fix --dry-run --verbose",
	"lint:fix": "./vendor/bin/php-cs-fixer fix",
	"hook": "./vendor/bin/cghooks update"
}
  • Install requirements and setup pre-commit hook:
composer require --dev brainmaestro/composer-git-hooks friendsofphp/php-cs-fixer
composer hook
  • Create configuration file .php_cs and set rules:

Change folders and rules if you want. You are welcome :)

<?php

return PhpCsFixer\Config::create()
    ->setUsingCache(false)
    ->setRiskyAllowed(true)
    ->setRules([
        '@PhpCsFixer' => true,
        'phpdoc_summary' => false,
        'concat_space' => [
            'spacing' => 'one',
        ],
        'psr0' => true,
    ])
    ->setFinder(PhpCsFixer\Finder::create()
        ->files()
        ->in(__DIR__.'/app')
        ->in(__DIR__.'/config')
        ->in(__DIR__.'/routes')
        ->in(__DIR__.'/tests')
        ->name('*.php')
    );
  • Create your first commit :)

You can run autofixing without changes in files, just for reporting:

composer lint
@vanodevium
Copy link
Author

git status --porcelain | grep -e '^\s*[AM]\(.*\).php$' | cut -c 3- | while read line; do
    line="../${line}"
    ./vendor/bin/php-cs-fixer fix --verbose "${line}";
    git add "$line";
done

echo "Code style checked!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment