diff --git a/.github/workflows/phpcs.yml b/.github/workflows/phpcs.yml index 6615b40..9a5f99e 100644 --- a/.github/workflows/phpcs.yml +++ b/.github/workflows/phpcs.yml @@ -29,11 +29,35 @@ jobs: ref: ${{ inputs.ref }} fetch-depth: 0 + # ------------------------------------------------------------------------------ + # Get changed files — runs right after checkout so we can skip remaining + # steps entirely when no PHP files changed + # ------------------------------------------------------------------------------ + - name: Get list of changed files + id: files + run: | + CHANGED_FILES=$(git diff --name-only --diff-filter=AM ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} -- . ':!tests' | grep '\.php$' || true) + + { + echo 'CHANGED_FILES<> $GITHUB_ENV + + if [ -n "$CHANGED_FILES" ]; then + echo "has_php_files=true" >> $GITHUB_OUTPUT + else + echo "No PHP files changed, skipping phpcs" + echo "has_php_files=false" >> $GITHUB_OUTPUT + fi + - uses: shivammathur/setup-php@v2 + if: steps.files.outputs.has_php_files == 'true' with: php-version: ${{ inputs.php_version }} - uses: ramsey/composer-install@v3 + if: steps.files.outputs.has_php_files == 'true' with: composer-options: "--ignore-platform-reqs" @@ -42,26 +66,20 @@ jobs: run: | sudo chown -R root:root $GITHUB_WORKSPACE - # ------------------------------------------------------------------------------ - # Get changed files - # ------------------------------------------------------------------------------ - - name: Get list of changed files - id: files - run: | - echo "CHANGED_FILES=$(git diff --name-only --diff-filter=AM ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} -- . ':!tests' | grep '\.php$' | tr '\n' ' ')" >> $GITHUB_ENV - # ------------------------------------------------------------------------------ # PHPCS # ------------------------------------------------------------------------------ - uses: reviewdog/action-setup@v1 + if: steps.files.outputs.has_php_files == 'true' with: reviewdog_version: latest # Optional. [latest,nightly,v.X.Y.Z] - name: Run reviewdog + if: steps.files.outputs.has_php_files == 'true' env: REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.access-token }} run: | - # Run phpcs and capture both the output and the exit code - JSON_REPORT=$(vendor/bin/phpcs --report=json -q ${{ env.CHANGED_FILES }} || echo "") + # Run phpcs — printf + xargs -d '\n' keeps filenames with spaces intact + JSON_REPORT=$(printf '%s\n' "${{ env.CHANGED_FILES }}" | xargs -d '\n' vendor/bin/phpcs --report=json -q) PHPCS_EXIT_CODE=$? # Check if phpcs produced a JSON report