From 554ebf987b4dbe4d13c11ccafb2dfb6c9957aeb8 Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Tue, 5 Sep 2017 22:49:22 +0200 Subject: [PATCH] Setup PHP CS fixer - Add PHP CS Fixer as a dev dependencies. - Add PHP CS Fixer cache file to gitignore/gitattributes - Add custom code style config - Update TravisCI config to check code style --- .gitattributes | 4 ++-- .gitignore | 8 ++++--- .php_cs.dist | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ .travis.yml | 53 ++++++++++++++++++++++++++++++++++++--------- composer.json | 3 ++- 5 files changed, 110 insertions(+), 16 deletions(-) create mode 100644 .php_cs.dist diff --git a/.gitattributes b/.gitattributes index 5305c97..3e98f80 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,11 +1,11 @@ -# Ignore all test and documentation for archive +# Ignore all test, documentation and dot files for archive /tests export-ignore /.editorconfig export-ignore /.gitattributes export-ignore /.gitignore export-ignore +/.php_cs.dist export-ignore /.scrutinizer.yml export-ignore /.travis.yml export-ignore -/composer.lock export-ignore /CONTRIBUTING.md export-ignore /logo.png export-ignore /phpunit.xml export-ignore diff --git a/.gitignore b/.gitignore index 16e0c78..048320b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ +/.idea +*.iml + /tests/resources/generated /tests/coverage /vendor -/.idea -*.iml -composer.lock +/composer.lock +/.php_cs.cache diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..f2a404c --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,58 @@ +setRiskyAllowed(true) + ->setRules([ + '@Symfony' => true, + 'align_multiline_comment' => false, + 'array_syntax' => ['syntax' => 'short'], + 'binary_operator_spaces' => ['align_double_arrow' => true, 'align_equals' => null], + 'blank_line_before_statement' => ['statements' => ['return']], + 'combine_consecutive_unsets' => true, + 'concat_space' => ['spacing' => 'one'], + 'declare_equal_normalize' => ['space' => 'single'], + 'heredoc_to_nowdoc' => true, + 'is_null' => ['use_yoda_style' => false], + 'method_argument_space' => ['ensure_fully_multiline' => true], + 'modernize_types_casting' => true, + 'no_break_comment' => ['comment_text' => 'do nothing'], + 'no_empty_phpdoc' => false, + 'no_null_property_initialization' => true, + 'no_short_echo_tag' => true, + 'no_superfluous_elseif' => true, + 'no_unneeded_control_parentheses' => ['statements' => ['break', 'clone', 'continue', 'echo_print', 'switch_case', 'yield']], + 'no_unneeded_curly_braces' => true, + 'no_unneeded_final_method' => true, + 'no_useless_else' => false, + 'no_useless_return' => true, + 'ordered_imports' => true, + 'phpdoc_add_missing_param_annotation' => true, + 'phpdoc_align' => false, + 'phpdoc_annotation_without_dot' => false, + 'phpdoc_no_empty_return' => false, + 'phpdoc_order' => true, + 'phpdoc_summary' => false, + 'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'], + 'phpdoc_separation' => false, + 'pre_increment' => false, + 'protected_to_private' => true, + 'psr4' => true, + 'return_type_declaration' => ['space_before' => 'one'], + 'semicolon_after_instruction' => true, + 'simplified_null_return' => false, + 'single_line_comment_style' => ['comment_types' => ['hash']], + 'strict_comparison' => true, + 'void_return' => true, + ]); + +$config->setFinder( + PhpCsFixer\Finder::create() + ->exclude('vendor') + ->in(__DIR__) + ->name('*.php') +); + +$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__; +$config->setCacheFile($cacheDir . '/.php_cs.cache'); + +return $config; diff --git a/.travis.yml b/.travis.yml index 2bdc52c..22976cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,27 +1,60 @@ +dist: trusty +sudo: false + language: php -dist: trusty - -php: - - 5.6 - - 7.0 - - 7.1 - - hhvm-3.6 +matrix: + include: + - php: 5.6 + env: WITH_PHPUNIT=true + - php: 5.6 + env: WITH_CS=true + - php: 7.0 + env: WITH_PHPUNIT=true + - php: 7.1 + env: WITH_PHPUNIT=true WITH_COVERAGE=true + - php: hhvm-3.6 + env: WITH_PHPUNIT=true cache: directories: - $HOME/.composer/cache + - $HOME/.php-cs-fixer + +before_install: + - | + if [[ "$WITH_COVERAGE" != "true" ]]; then + phpenv config-rm xdebug.ini + fi + if [[ "$WITH_CS" != "true" ]]; then + composer remove friendsofphp/php-cs-fixer --dev --no-update + fi + - composer validate install: - composer install --no-interaction --prefer-source -script: +before_script: + - mkdir -p "$HOME/.php-cs-fixer" - mkdir -p build/logs - - php vendor/bin/phpunit --coverage-clover=build/logs/coverage.clover + +script: + - | + if [[ "$WITH_CS" == "true" ]]; then + vendor/bin/php-cs-fixer fix --config=.php_cs.dist --verbose --diff --dry-run + fi + - | + if [[ "$WITH_PHPUNIT" == "true" ]]; then + if [[ "$WITH_COVERAGE" == "true" ]]; then + vendor/bin/phpunit --coverage-clover=build/logs/coverage.clover + else + vendor/bin/phpunit --no-coverage + fi + fi after_script: - | - if [[ "$TRAVIS_PHP_VERSION" = '7.1' ]]; then + if [[ "$WITH_COVERAGE" == "true" ]]; then wget https://scrutinizer-ci.com/ocular.phar php ocular.phar code-coverage:upload --format=php-clover build/logs/coverage.clover fi diff --git a/composer.json b/composer.json index 53bea00..b1eaf23 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "ext-xmlreader" : "*" }, "require-dev": { - "phpunit/phpunit": "^5.7.0" + "phpunit/phpunit": "^5.7.0", + "friendsofphp/php-cs-fixer": "^2.5.0" }, "suggest": { "ext-iconv": "To handle non UTF-8 CSV files (if \"php-intl\" is not already installed or is too limited)",