From 068624d1fb65fa5f298f495926f4818a63af6d09 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sat, 19 Jun 2021 14:30:19 +0200 Subject: [PATCH] Add PHP CS Fixer to the toolchain This tool will help detect and fix more PSR-12 issues, and it will also help modernize the tests so that it's easier to cover issues found by PHPStan with tests. --- .github/workflows/ci.yml | 5 ++++- .gitignore | 1 + .phive/phars.xml | 1 + composer.json | 18 ++++++++++++------ config/php-cs-fixer.php | 15 +++++++++++++++ 5 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 config/php-cs-fixer.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 28c4e0b4..529d54b0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -90,9 +90,12 @@ jobs: fail-fast: false matrix: include: - - command: sniff + - command: sniffer tool: phpcs php-version: 7.4 + - command: fixer + tool: php-cs-fixer + php-version: 7.4 - command: stan tool: phpstan php-version: 7.4 diff --git a/.gitignore b/.gitignore index 4b6626bb..c1747f26 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /.phive/* +/.php-cs-fixer.cache /.php_cs.cache /composer.lock /phpstan.neon diff --git a/.phive/phars.xml b/.phive/phars.xml index 52cccc1a..2433736a 100644 --- a/.phive/phars.xml +++ b/.phive/phars.xml @@ -1,5 +1,6 @@ + diff --git a/composer.json b/composer.json index f88468f5..1d7c4020 100644 --- a/composer.json +++ b/composer.json @@ -35,25 +35,31 @@ "ci": [ "@ci:static" ], - "ci:php:sniff": "@php ./.phive/phpcs.phar --standard=config/phpcs.xml bin src tests", + "ci:php:fixer": "@php ./.phive/php-cs-fixer.phar --config=config/php-cs-fixer.php fix --dry-run -v --show-progress=dots bin src tests", + "ci:php:sniffer": "@php ./.phive/phpcs.phar --standard=config/phpcs.xml bin src tests", "ci:php:stan": "@php ./.phive/phpstan.phar --configuration=config/phpstan.neon", "ci:static": [ - "@ci:php:sniff", + "@ci:php:fixer", + "@ci:php:sniffer", "@ci:php:stan" ], "fix:php": [ - "@fix:php:sniff" + "@fix:php:fixer", + "@fix:php:sniffer" ], - "fix:php:sniff": "@php ./.phive/phpcbf.phar --standard=config/phpcs.xml bin src tests", + "fix:php:fixer": "@php ./.phive/php-cs-fixer.phar --config=config/php-cs-fixer.php fix bin src tests", + "fix:php:sniffer": "@php ./.phive/phpcbf.phar --standard=config/phpcs.xml bin src tests", "phpstan:baseline": "@php ./.phive/phpstan.phar --configuration=config/phpstan.neon --generate-baseline=config/phpstan-baseline.neon" }, "scripts-descriptions": { "ci": "Runs all dynamic and static code checks (i.e. currently, only the static checks).", - "ci:php:sniff": "Checks the code style with PHP_CodeSniffer.", + "ci:php:fixer": "Checks the code style with PHP CS Fixer.", + "ci:php:sniffer": "Checks the code style with PHP_CodeSniffer.", "ci:php:stan": "Checks the types with PHPStan.", "ci:static": "Runs all static code analysis checks for the code.", "fix:php": "Autofixes all autofixable issues in the PHP code.", - "fix:php:sniff": "Fixes autofixable issues found by PHP_CodeSniffer.", + "fix:php:fixer": "Fixes autofixable issues found by PHP CS Fixer.", + "fix:php:sniffer": "Fixes autofixable issues found by PHP_CodeSniffer.", "phpstand:baseline": "Updates the PHPStan baseline file to match the code." } } diff --git a/config/php-cs-fixer.php b/config/php-cs-fixer.php new file mode 100644 index 00000000..f1b644bf --- /dev/null +++ b/config/php-cs-fixer.php @@ -0,0 +1,15 @@ +setRiskyAllowed(true) + ->setRules( + [ + '@PSR12' => true, + // Disable constant visibility from the PSR12 rule set as this would break compatibility with PHP < 7.1. + 'visibility_required' => ['elements' => ['property', 'method']], + ] + );