$ composer global require friendsofphp/php-cs-fixer
$ brew install php-cs-fixer
I store this in my dotfiles and symlink it to my PHP repos.
<?php
/**
* Save script to .php_cs and place .php_cs file in the root of your 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
*/
$header = <<<'EOF'
You can have this put a header at the top of your php files.
EOF;
$fixers = [
'@PHP56Migration' => true,
'@PHPUnit60Migration:risky' => true,
'@PSR1' => true,
'@PSR2' => true,
'@Symfony:risky' => true,
'@Symfony' => true,
'align_multiline_comment' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => ['align_double_arrow' => true, 'align_equals' => true],
'blank_line_before_return' => true,
'blank_line_before_statement' => true,
'combine_consecutive_issets' => true,
'combine_consecutive_unsets' => true,
'comment_to_phpdoc' => true,
'compact_nullable_typehint' => true,
'concat_space' => ['spacing' => 'one'],
'escape_implicit_backslashes' => true,
'explicit_indirect_variable' => true,
'explicit_string_variable' => true,
'final_internal_class' => true,
'fully_qualified_strict_types' => true,
'function_to_constant' => ['functions' => ['get_class', 'get_called_class', 'php_sapi_name', 'phpversion', 'pi']],
'heredoc_to_nowdoc' => true,
'list_syntax' => ['syntax' => 'long'],
'logical_operators' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'method_chaining_indentation' => true,
'multiline_comment_opening_closing' => true,
'new_with_braces' => true,
'no_alternative_syntax' => true,
'no_binary_string' => true,
'no_empty_statement' => true,
'no_extra_blank_lines' => ['tokens' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block']],
'no_extra_consecutive_blank_lines' => ['use'],
'no_extra_consecutive_blank_lines' => true,
'no_leading_import_slash' => true,
'no_multiline_whitespace_around_double_arrow' => true,
'no_null_property_initialization' => true,
'no_short_echo_tag' => true,
'no_superfluous_elseif' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_unneeded_curly_braces' => true,
'no_unneeded_final_method' => true,
'no_unreachable_default_argument_value' => true,
'no_unset_on_property' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'no_whitespace_in_blank_line' => true,
'ordered_class_elements' => true,
'ordered_imports' => true,
'php_unit_internal_class' => true,
'php_unit_ordered_covers' => true,
'php_unit_set_up_tear_down_visibility' => true,
'php_unit_strict' => true,
'php_unit_test_annotation' => true,
'php_unit_test_case_static_method_calls' => ['call_type' => 'this'],
'php_unit_test_class_requires_covers' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_align' => true,
'phpdoc_order' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_types_order' => true,
'return_assignment' => true,
'semicolon_after_instruction' => true,
'single_line_comment_style' => true,
'strict_comparison' => true,
'strict_param' => true,
'string_line_ending' => true,
'trailing_comma_in_multiline_array' => true,
'yoda_style' => true,
// 'header_comment' => ['header' => $header],
];
$finder = PhpCsFixer\Finder::create()
->notName('*.blade.php')
->exclude('vendor')
->in(__DIR__);
return PhpCsFixer\Config::create()
->setRiskyAllowed(true) // The --allow-risky option (pass yes or no) allows you to set whether risky rules may run. Default value is taken from config file. Risky rule is a rule, which could change code behaviour. By default no risky rules are run.
->setRules($fixers)
->setUsingCache(false)
->setFinder($finder);
ln ~/.dotfiles/.php_cs <PATH TO MY PHP REPO>
alias pcf='php-cs-fixer fix $1 --verbose --config=.php_cs'
Call within the root directory of your project, one with .php_cs
in it.
$ pcf <PATH TO FILE OR DIR>