Skip to content

Instantly share code, notes, and snippets.

@sixlive
Last active August 9, 2022 20:40
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sixlive/2862a2185c76988838acaa45687a575c to your computer and use it in GitHub Desktop.
Save sixlive/2862a2185c76988838acaa45687a575c to your computer and use it in GitHub Desktop.
My Current PHP CS Fixer Workflow

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.

Setup

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')
    );

Usage

Command line

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

CI

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
published: true
@stefnats
Copy link

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?

@sidigi
Copy link

sidigi commented Nov 25, 2019

./vendor/bin/php-cs-fixer fix --dry-run --diff --rules=@PSR2

@joseluisr0307
Copy link

joseluisr0307 commented Jan 15, 2020

./vendor/bin/php-cs-fixer fix --dry-run --diff --rules=@PSR2

https://github.com/FriendsOfPHP/PHP-CS-Fixer

in composer.json file add

"scripts": {
"phpunit" : "phpunit --verbose --testdox",
"phpcs" : "php-cs-fixer fix",
"phpcs_validate" : "php-cs-fixer fix --dry-run --diff",
"test": [
"@phpunit",
"@phpcs_validate"
]
}

and run "composer test" in the shell

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