This is the process and standard that I use for the vast majority of my Laravel code bases and projects.
For more information about php-cs-fixer visit the project repository: FriendsOfPHP/PHP-CS-FIXER.
Add the following .php_cs
file to the root of your project.
.php_cs
$rules = [
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'no_multiline_whitespace_before_semicolons' => true,
'no_short_echo_tag' => true,
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'no_useless_else' => true,
'ordered_imports' => [
'sortAlgorithm' => 'length',
],
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_indent' => true,
'phpdoc_no_package' => true,
'phpdoc_order' => true,
'phpdoc_separation' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_trim' => true,
'phpdoc_var_without_name' => true,
'phpdoc_to_comment' => true,
'single_quote' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline_array' => true,
'trim_array_spaces' => true,
];
$excludes = [
'vendor',
'storage',
'node_modules',
];
return PhpCsFixer\Config::create()
->setRules($rules)
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude($excludes)
->notName('README.md')
->notName('*.xml')
->notName('*.yml')
->notName('_ide_helper.php')
);
I put together a custom Docker container just for running PHPCS and PHP-CS-FIXER rather than install it locally.
Lint: docker run -it --rm -v $PWD:/app sixlive/php-lint-fix php-cs-fixer fix --dry-run --diff
Fix: docker run -it --rm -v $PWD:/app sixlive/php-lint-fix php-cs-fixer fix
Gitlab CI Example:
stages:
- lint
lint:php:
stage: lint
image: sixlive/php-lint-fix
script:
- php-cs-fixer fix --dry-run --diff
Drone-CI for example:
pipeline:
lint:
image: sixlive/php-lint-fix
commands:
- php-cs-fixer fix --dry-run --diff
Just tried this, but in GitLab CI it tells me:
$ php-cs-fixer fix --dry-run --diff
bash: line 68: php-cs-fixer: command not found
ERROR: Job failed: exit status 1
What am i doing wrong?