Skip to content

PHPCS Autofix #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions .github/workflows/phpcs-autofix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: PHP CodeSniffer Autofix

permissions:
contents: write
pull-requests: write

on:
pull_request:
branches:
- master
workflow_dispatch:

jobs:
phpcs_autofix:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.1"
extensions: mbstring, xml, ctype, iconv, json
tools: composer

- name: Allow Composer plugins
run: |
composer config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer config --no-plugins allow-plugins.phpcompatibility/phpcs-compatinfo true
# Add any other plugins if they cause issues

- name: Install PHP_CodeSniffer and WordPress Coding Standards
run: |
composer require --dev squizlabs/php_codesniffer "*"
composer require --dev wp-coding-standards/wpcs "*"
composer require --dev phpcompatibility/phpcompatibility-wp "*"

- name: Run PHPCBF to fix coding standards
run: vendor/bin/phpcbf --standard=phpcs.xml --extensions=php . || true

- name: Commit and Push PHPCBF fixes
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"

echo "Current git status:"
git status

# Add all changed files. phpcbf modifies files in place.
# We expect only .php files to be changed by phpcbf.
git add .

# Check again if staging made any difference to status, then commit
echo "Git status after add:"
git status

if git diff --staged --quiet; then
echo "No changes staged, nothing to commit."
else
echo "Staged changes found, attempting commit..."
git commit -m "Apply PHPCBF fixes [skip ci]" -m "Automated PHPCBF fixes." --no-verify

# Determine the branch to push to
# For PRs, github.head_ref is the source branch name.
# For workflow_dispatch, github.ref_name is the branch the workflow was run on.
BRANCH_NAME=""
if [[ -n "${{ github.head_ref }}" ]]; then
BRANCH_NAME="${{ github.head_ref }}"
else
BRANCH_NAME="${{ github.ref_name }}"
fi

echo "Pushing changes to branch: $BRANCH_NAME"
git push origin "$BRANCH_NAME"
echo "Changes pushed successfully."
fi