Skip to content

Commit

Permalink
Added php-cs-fixer and integrated it into GHA workflow.
Browse files Browse the repository at this point in the history
To check codestyle (both `src` and `tests` directories are checked) you
just run `./bin/code-style.sh` script, which will check if everything is
ok.

If there are some issues to fix, running `./bin/code-style.sh --fix` will
fix them.
  • Loading branch information
smuuf committed Nov 4, 2023
1 parent 83c8448 commit bbc318f
Show file tree
Hide file tree
Showing 6 changed files with 1,946 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ jobs:
chmod +x ./bin/*.sh
- name: Run tests
run: ./bin/tests.sh
- name: Run code style analysis
continue-on-error: false
run: ./bin/code-style.sh
- name: Run static analysis
continue-on-error: true
run: ./bin/phpstan.sh
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
.vscode
*.code-workspace

# php-cs-fixer
.php-cs-fixer.cache

# Tests.
tests/**/output/**
!tests/**/output/.gitkeep
Expand Down
166 changes: 166 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

const DIRS = [
__DIR__ . '/src',
__DIR__ . '/tests',
];

$finder = (new \PhpCsFixer\Finder())
->in(DIRS)
->name('/\.phpt?$/');

return (new PhpCsFixer\Config())
->setRules([
'no_whitespace_in_blank_line' => true,
'no_closing_tag' => true,
'ternary_to_null_coalescing' => true,
'ternary_operator_spaces' => true,
'operator_linebreak' => [
'position' => 'beginning',
],
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'new_with_parentheses' => true,
'no_space_around_double_colon' => true,
'long_to_shorthand_operator' => true,
'concat_space' => [
'spacing' => 'one',
],
'object_operator_without_whitespace' => true,
'clean_namespace' => true,
'array_indentation' => true,
'no_spaces_around_offset' => true,
'array_syntax' => true,
'list_syntax' => true,
'attribute_empty_parentheses' => [
'use_parentheses' => true,
],
'blank_line_after_namespace' => true,
'single_space_around_construct' => true,
'yoda_style' => [
'equal' => false,
'identical' => false,
'less_and_greater' => false,
],
'elseif' => true,
'no_superfluous_elseif' => true,
'include' => true,
'explicit_indirect_variable' => true,
'declare_parentheses' => true,
'declare_equal_normalize' => true,
'combine_consecutive_unsets' => true,
'single_line_after_imports' => true,
'single_import_per_statement' => true,
'no_unused_imports' => true,
'no_unneeded_import_alias' => true,
'no_leading_import_slash' => true,
'global_namespace_import' => [
'import_classes' => false,
'import_constants' => false,
'import_functions' => false,
],
'fully_qualified_strict_types' => true,
'no_spaces_after_function_name' => true,
'nullable_type_declaration_for_default_null_value' => true,
'method_argument_space' => true,
'lambda_not_used_import' => true,
'function_declaration' => [
'closure_fn_spacing' => 'none',
'closure_function_spacing' => 'none',
],
'trailing_comma_in_multiline' => [
'elements' => [
'arguments',
'arrays',
'match',
'parameters',
],
],
'switch_case_space' => true,
'switch_continue_to_break' => true,
'switch_case_semicolon_to_colon' => true,
'no_useless_else' => true,
'braces_position' => [
'allow_single_line_anonymous_functions' => true,
'allow_single_line_empty_anonymous_classes' => true,
'anonymous_classes_opening_brace' => 'same_line',
'anonymous_functions_opening_brace' => 'same_line',
'classes_opening_brace' => 'same_line',
'control_structures_opening_brace' => 'same_line',
'functions_opening_brace' => 'same_line',
],
'no_short_bool_cast' => true,
'lowercase_cast' => true,
'cast_spaces' => true,
'lowercase_keywords' => true,
'lowercase_static_reference' => true,
'magic_constant_casing' => true,
'magic_method_casing' => true,
'native_function_casing' => true,
'integer_literal_case' => true,
'control_structure_braces' => true,
'control_structure_continuation_position' => [
'position' => 'same_line',
],
'no_multiple_statements_per_line' => true,
'statement_indentation' => true,
'no_multiline_whitespace_around_double_arrow' => true,
'normalize_index_brace' => true,
'octal_notation' => true,
'indentation_type' => true,
'visibility_required' => true,
'whitespace_after_comma_in_array' => true,
'types_spaces' => [
'space' => 'none',
],
'single_line_empty_body' => true,
'method_chaining_indentation' => true,
'single_blank_line_at_eof' => true,
'spaces_inside_parentheses' => [
'space' => 'none',
],
'type_declaration_spaces' => true,
'compact_nullable_type_declaration' => true,
'space_after_semicolon' => [
'remove_in_empty_for_expressions' => true,
],
'no_trailing_comma_in_singleline' => true,
'encoding' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_empty_statement' => true,
'no_useless_return' => true,
'return_type_declaration' => true,
'multiline_whitespace_before_semicolons' => true,
'multiline_comment_opening_closing' => true,
'no_trailing_whitespace_in_comment' => true,
'single_line_comment_spacing' => true,
'single_line_comment_style' => true,
'phpdoc_var_without_name' => true,
'phpdoc_var_annotation_correct_order' => true,
'phpdoc_trim' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_summary' => true,
'phpdoc_to_comment' => [
'ignored_tags' => [
// To allow "/** @var SomeType $someVar */" annotations over
// variables for IDEs. PHP Intelephense (vscode extension), for
// example, wouldn't recognize "// @var SomeType $someVar"
// properly.
'var',
],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_scalar' => true,
'phpdoc_param_order' => true,
'phpdoc_order' => true,
'phpdoc_indent' => true,
'no_superfluous_phpdoc_tags' => true,
'no_empty_phpdoc' => true,
'no_blank_lines_after_phpdoc' => true,
'align_multiline_comment' => [
'comment_type' => 'all_multiline',
],
])
->setIndent("\t")
->setLineEnding("\n")
->setFinder($finder);
23 changes: 23 additions & 0 deletions bin/code-style.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

cd $(dirname $0)/..

ARG="$1"
[[ $ARG == "--fix" ]] && COMMAND="fix" || COMMAND="check"

./vendor/bin/php-cs-fixer $COMMAND --diff -vvv
EXITCODE=$?

if [[ $COMMAND == "check" && $EXITCODE -gt 0 ]]; then
echo
echo "█ Oh noes, php-cs-fixer found issues with code style."
echo "█ Run '$0 --fix' to fix them."
fi

if [[ $EXITCODE -eq 0 ]]; then
echo
echo "█ Everything went ok."
fi

# Bubble up the exit code to the original caller.
exit $EXITCODE
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"require-dev": {
"nette/tester": "^2.4",
"phpstan/phpstan": "^1.8",
"predis/predis": "^2"
"predis/predis": "^2",
"friendsofphp/php-cs-fixer": "^3.37"
},
"suggest": {
"predis/predis": "Adds support for Predis - a PHP library providing a Redis result backend"
Expand Down
Loading

0 comments on commit bbc318f

Please sign in to comment.