Skip to content

Instantly share code, notes, and snippets.

@kennedy-osaze
Last active March 26, 2023 17:34
Show Gist options
  • Save kennedy-osaze/92573922cbb6f2ad04314fb2fa15fb19 to your computer and use it in GitHub Desktop.
Save kennedy-osaze/92573922cbb6f2ad04314fb2fa15fb19 to your computer and use it in GitHub Desktop.
PHP CS Fixer - Laravel Coding Standard Ruleset
<?php
use PhpCsFixer\Finder;
use PhpCsFixer\Config;
$directoriesToExclude = [
'bootstrap',
'storage',
'node_modules',
];
$filesToExclude = [
'*.blade.php',
'index.php',
'server.php',
];
$fixerRules = [
'@PSR12' => true,
'align_multiline_comment' => true,
'array_indentation' => true,
'array_syntax' => true,
'assign_null_coalescing_to_coalesce_equal' => true,
'backtick_to_shell_exec' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => [
'break',
'continue',
'declare',
'do',
'exit',
'for',
'foreach',
'if',
'return',
'switch',
'throw',
'try',
'while',
'yield'
],
],
'braces' => [
'allow_single_line_anonymous_class_with_empty_body' => true,
'allow_single_line_closure' => true
],
'cast_spaces' => true,
'class_attributes_separation' => [
'elements' => [
'const' => 'only_if_meta',
'method' => 'one',
'property' => 'one',
],
],
'class_definition' => [
'inline_constructor_arguments' => true,
'multi_line_extends_each_single_line' => true,
'single_item_single_line' => true,
'single_line' => true,
'space_before_parenthesis' => true,
],
'class_reference_name_casing' => true,
'clean_namespace' => true,
'combine_nested_dirname' => true,
'comment_to_phpdoc' => true,
'concat_space' => true,
'control_structure_braces' => true,
'control_structure_continuation_position' => true,
'curly_braces_position' => true,
'declare_parentheses' => true,
'dir_constant' => true,
'empty_loop_body' => true,
'empty_loop_condition' => true,
'explicit_indirect_variable' => true,
'explicit_string_variable' => true,
'fopen_flag_order' => true,
'full_opening_tag' => true,
'fully_qualified_strict_types' => true,
'function_typehint_space' => true,
'get_class_to_class_keyword' => true,
'global_namespace_import' => true,
'heredoc_indentation' => true,
'heredoc_to_nowdoc' => true,
'implode_call' => true,
'include' => true,
'integer_literal_case' => true,
'lambda_not_used_import' => true,
'linebreak_after_opening_tag' => true,
'list_syntax' => true,
'magic_constant_casing' => true,
'magic_method_casing' => true,
'mb_str_functions' => true,
'method_argument_space' => ['after_heredoc' => true, 'on_multiline' => 'ignore'],
'method_chaining_indentation' => true,
'modernize_types_casting' => true,
'multiline_comment_opening_closing' => true,
'multiline_whitespace_before_semicolons' => true,
'native_function_casing' => true,
'native_function_type_declaration_casing' => true,
'new_with_braces' => [
'anonymous_class' => false,
],
'no_alias_functions' => ['sets' => ['@all']],
'no_alias_language_construct_call' => true,
'no_alternative_syntax' => true,
'no_binary_string' => true,
'no_blank_lines_after_phpdoc' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_blank_lines' => ['tokens' => ['extra', 'throw', 'use']],
'no_leading_namespace_whitespace' => true,
'no_mixed_echo_print' => true,
'no_multiline_whitespace_around_double_arrow' => true,
'no_multiple_statements_per_line' => true,
'no_null_property_initialization' => true,
'no_php4_constructor' => true,
'no_short_bool_cast' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_around_offset' => true,
'no_superfluous_elseif' => true,
'no_trailing_comma_in_singleline' => true,
'no_trailing_whitespace_in_string' => true,
'no_unneeded_control_parentheses' => true,
'no_unneeded_curly_braces' => ['namespaces' => true],
'no_unneeded_final_method' => true,
'no_unreachable_default_argument_value' => true,
'no_unset_cast' => true,
'no_unset_on_property' => true,
'no_unused_imports' => true,
'no_useless_concat_operator' => true,
'no_useless_else' => true,
'no_useless_nullsafe_operator' => true,
'no_useless_return' => true,
'no_useless_sprintf' => true,
'no_whitespace_before_comma_in_array' => true,
'normalize_index_brace' => true,
'not_operator_with_successor_space' => true,
'nullable_type_declaration_for_default_null_value' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => ['sort_algorithm' => 'alpha', 'imports_order' => ['class', 'function', 'const']],
'ordered_interfaces' => true,
'operator_linebreak' => true,
'php_unit_construct' => true,
'php_unit_data_provider_static' => true,
'php_unit_dedicate_assert' => true,
'php_unit_dedicate_assert_internal_type' => true,
'php_unit_expectation' => true,
'php_unit_fqcn_annotation' => true,
'php_unit_mock' => true,
'php_unit_mock_short_will_return' => true,
'php_unit_namespaced' => true,
'php_unit_set_up_tear_down_visibility' => true,
'php_unit_strict' => true,
'php_unit_test_annotation' => true,
'phpdoc_align' => ['align' => 'left','tags' => ['param', 'property', 'return', 'throws', 'type', 'var', 'method']],
'phpdoc_annotation_without_dot' => true,
'phpdoc_indent' => true,
'phpdoc_inline_tag_normalizer' => true,
'phpdoc_line_span' => true,
'phpdoc_no_access' => true,
'phpdoc_no_alias_tag' => true,
'phpdoc_no_package' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_order' => true,
'phpdoc_order_by_value' => true,
'phpdoc_return_self_reference' => true,
'phpdoc_scalar' => true,
'phpdoc_separation' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_summary' => true,
'phpdoc_tag_casing' => true,
'phpdoc_tag_type' => ['tags' => ['inheritDoc' => 'inline']],
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_types' => true,
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
'phpdoc_var_annotation_correct_order' => true,
'phpdoc_var_without_name' => true,
'pow_to_exponentiation' => true,
'psr_autoloading' => true,
'random_api_migration' => true,
'return_assignment' => true,
'self_accessor' => true,
'self_static_accessor' => true,
'semicolon_after_instruction' => true,
'set_type_to_cast' => true,
'simple_to_complex_string_variable' => true,
'simplified_if_return' => true,
'single_class_element_per_statement' => true,
'single_line_comment_spacing' => true,
'single_line_comment_style' => ['comment_types' => ['hash']],
'single_quote' => true,
'single_space_after_construct' => true,
'single_trait_insert_per_statement' => false,
'space_after_semicolon' => true,
'standardize_not_equals' => true,
'statement_indentation' => true,
'string_length_to_empty' => true,
'string_line_ending' => true,
'switch_continue_to_break' => true,
'ternary_to_elvis_operator' => true,
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline' => true,
'trim_array_spaces' => true,
'types_spaces' => true,
'unary_operator_spaces' => true,
'whitespace_after_comma_in_array' => true,
];
$finder = Finder::create()
->exclude($directoriesToExclude)
->notName($filesToExclude)
->in(__DIR__);
return Config::create()
->setFinder($finder)
->setRules($fixerRules)
->setRiskyAllowed(true);
@kennedy-osaze
Copy link
Author

kennedy-osaze commented Jan 20, 2020

README

Installation

  • Install the PHP-CS-Fixer: composer require friendsofphp/php-cs-fixer --dev

  • Create a .php_cs.dist file at the root folder of your project. touch .php_cs.dist

Usage

Basic

  • Run the command ./vendor/bin/php-cs-fixer fix --dry-run to check for errors

  • Run the command ./vendor/bin/php-cs-fixer fix --verbose --show-progress=estimating --diff to fix errors

Composer (Recommended)

  • Add the commands above to the scripts section of your composer.json file.
"script": {
    "fix": [
        "./vendor/bin/php-cs-fixer fix --verbose --show-progress=estimating --diff"
    ],
    "lint": [
        "./vendor/bin/php-cs-fixer fix --dry-run"
    ]
}
  • Run composer run lint to check for errors

  • Run composer run fix to fix errors

Note: Not all errors can be fixed by the package, some need manual fixing

Rule Configuration details: https://mlocati.github.io/php-cs-fixer-configurator

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