Skip to content

Github Action: Add phpcs checks #17

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

Merged
merged 7 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Code Standards
on:
pull_request:

jobs:
phpcs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Required for phpcs-changed to compare with base branch

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
tools: composer:v2

- name: Install Dependencies
run: composer install

- name: Run PHPCS on changed files
env:
BASE_REF: ${{ github.base_ref }}
run: |
CHANGED_FILES=$(git diff --name-only --no-renames --diff-filter=d "origin/$BASE_REF" '*.php' || true)
if [[ -n "$CHANGED_FILES" ]]; then
vendor/bin/phpcs-changed \
--git-base="origin/$BASE_REF" \
--phpcs-path=vendor/bin/phpcs \
--standard=.phpcs.xml.dist \
$CHANGED_FILES
else
echo "No PHP files changed"
fi
2 changes: 2 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
. .husky/pre-commit-phpcbf.sh
49 changes: 49 additions & 0 deletions .husky/pre-commit-phpcbf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/sh

# Disable exit on error since we handle exit codes manually
set +e

# Get list of staged PHP files
STAGED_PHP_FILES=$(git diff --cached --name-only --no-renames --diff-filter=d '*.php' )

# Exit if no PHP files are staged
if [ -z "$STAGED_PHP_FILES" ]; then
exit 0
fi

echo "Running PHPCBF on staged files..."

# First try to fix what we can with PHPCBF
echo "$STAGED_PHP_FILES" | xargs vendor/bin/phpcbf

# Check the return code
PHPCBF_STATUS=$?

# If files were fixed, add them back to staging
if [ $PHPCBF_STATUS -ne 3 ]; then
echo "$STAGED_PHP_FILES" | xargs git add

if [ $PHPCBF_STATUS -eq 1 ] || [ $PHPCBF_STATUS -eq 2 ]; then
echo ""
echo "Some errors were fixed by PHPCBF, but not all. Continuing with phpcs-changed for remaining errors on lines changed by this PR."
fi
fi

# Now run phpcs-changed to check only modified lines
echo "Checking modified lines with phpcs-changed..."

vendor/bin/phpcs-changed\
--git-staged\
--phpcs-path=vendor/bin/phpcs\
--standard=.phpcs.xml.dist\
$STAGED_PHP_FILES

PHPCS_STATUS=$?

if [ $PHPCS_STATUS -ne 0 ]; then
echo "⛔️ Found coding standards violations in changed lines. Please fix them before committing."
exit 1
fi

echo "✅ All coding standards checks passed!"
exit 0
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"require-dev": {
"wp-coding-standards/wpcs": "^3.0",
"phpcompatibility/phpcompatibility-wp": "^2.1"
"phpcompatibility/phpcompatibility-wp": "^2.1",
"sirbrillig/phpcs-changed": "^2.11"
}
}
66 changes: 60 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 26 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"babel-loader": "^9.2.1",
"css-loader": "^7.1.2",
"css-minimizer-webpack-plugin": "^7.0.0",
"husky": "^9.0.11",
"mini-css-extract-plugin": "^2.9.1",
"sass": "^1.79.5",
"sass-loader": "^16.0.2",
Expand All @@ -17,7 +18,8 @@
"scripts": {
"build": "webpack",
"watch": "webpack --watch",
"clean": "rm -rf assets/build/js/*.js assets/build/css/*.css assets/build/js/*.map assets/build/css/*.map assets/build/css/*.js"
"clean": "rm -rf assets/build/js/*.js assets/build/css/*.css assets/build/js/*.map assets/build/css/*.map assets/build/css/*.js",
"prepare": "husky"
},
"dependencies": {
"md5": "^2.3.0"
Expand Down
Loading