From 45cc83384b55dc82d65b149660dc89dc3b2c0038 Mon Sep 17 00:00:00 2001 From: MGatner Date: Fri, 19 Nov 2021 02:36:38 +0000 Subject: [PATCH 1/2] Add CS Fixer --- README.md | 5 +++ src/.github/workflows/phpcsfixer.yml | 61 ++++++++++++++++++++++++++++ src/.php-cs-fixer.dist.php | 20 +++++++++ 3 files changed, 86 insertions(+) create mode 100644 src/.github/workflows/phpcsfixer.yml create mode 100644 src/.php-cs-fixer.dist.php diff --git a/README.md b/README.md index 030c753..d3a937d 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,11 @@ PHP Copy-Paste Detector analyzes your code and reports when there are blocks of more than a certain number of lines long (default: 5). In most cases this is a sign of poor code structure and an opportunity to consolidate classes or functions. +#### PHP CS Fixer + +PHP CS Fixer is used to enforce coding standards. Once the rules are defined in the config file +the workflow will check your code against the definitions and fail for any deviance. + #### PHPStan *Requires **phpstan.neon.dist*** diff --git a/src/.github/workflows/phpcsfixer.yml b/src/.github/workflows/phpcsfixer.yml new file mode 100644 index 0000000..0792dc4 --- /dev/null +++ b/src/.github/workflows/phpcsfixer.yml @@ -0,0 +1,61 @@ +name: PHPCSFixer + +on: + pull_request: + branches: + - develop + paths: + - '**.php' + - '.github/workflows/phpcsfixer.yml' + push: + branches: + - develop + paths: + - '**.php' + - '.github/workflows/phpcsfixer.yml' + +jobs: + build: + name: PHP ${{ matrix.php-versions }} Coding Standards + runs-on: ubuntu-latest + if: "!contains(github.event.head_commit.message, '[ci skip]')" + strategy: + fail-fast: false + matrix: + php-versions: ['7.3', '7.4', '8.0'] + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: json, tokenizer + coverage: none + env: + COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Get composer cache directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache composer dependencies + uses: actions/cache@v2 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ runner.os }}-composer- + + - name: Install dependencies + run: | + composer -q config -g github-oauth.github.com "${{ secrets.GITHUB_TOKEN }}" + if [ -f composer.lock ]; then + composer install --no-progress --no-interaction --prefer-dist --optimize-autoloader + else + composer update --no-progress --no-interaction --prefer-dist --optimize-autoloader + fi + + - name: Check code for standards compliance + run: vendor/bin/php-cs-fixer fix --verbose --ansi --dry-run --using-cache=no --diff diff --git a/src/.php-cs-fixer.dist.php b/src/.php-cs-fixer.dist.php new file mode 100644 index 0000000..dfa45af --- /dev/null +++ b/src/.php-cs-fixer.dist.php @@ -0,0 +1,20 @@ +files() + ->in(__DIR__) + ->exclude('build') + ->append([__FILE__]); + +$overrides = []; + +$options = [ + 'finder' => $finder, + 'cacheFile' => 'build/.php-cs-fixer.cache', +]; + +return Factory::create(new CodeIgniter4(), $overrides, $options)->forProjects(); From e149695f2ef1da0fe0aae2c0521313e81be47538 Mon Sep 17 00:00:00 2001 From: MGatner Date: Fri, 19 Nov 2021 14:18:42 +0000 Subject: [PATCH 2/2] Restrict directories --- src/.php-cs-fixer.dist.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/.php-cs-fixer.dist.php b/src/.php-cs-fixer.dist.php index dfa45af..208e50e 100644 --- a/src/.php-cs-fixer.dist.php +++ b/src/.php-cs-fixer.dist.php @@ -6,7 +6,10 @@ $finder = Finder::create() ->files() - ->in(__DIR__) + ->in([ + __DIR__ . '/app', + __DIR__ . '/tests', + ]) ->exclude('build') ->append([__FILE__]);