Skip to content

Commit baf70c1

Browse files
committed
Add github action, phpcs-changed, and pre-commit hook via Husky
1 parent a93d14d commit baf70c1

File tree

7 files changed

+180
-15
lines changed

7 files changed

+180
-15
lines changed

.github/workflows/phpcs.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Code Standards
2+
on:
3+
pull_request:
4+
5+
jobs:
6+
phpcs:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v4
11+
with:
12+
fetch-depth: 0 # Required for phpcs-changed to compare with base branch
13+
14+
- name: Setup PHP
15+
uses: shivammathur/setup-php@v2
16+
with:
17+
php-version: '8.1'
18+
tools: composer:v2
19+
20+
- name: Install Dependencies
21+
run: composer install
22+
23+
- name: Run PHPCS on changed files
24+
run: |
25+
CHANGED_FILES=$(git diff --name-only --diff-filter=d origin/${{ github.base_ref }} | grep ".php$" || true)
26+
if [ ! -z "$CHANGED_FILES" ]; then
27+
vendor/bin/phpcs-changed \
28+
--git-base=origin/${{ github.base_ref }} \
29+
--phpcs-path=vendor/bin/phpcs \
30+
--standard=.phpcs.xml.dist \
31+
$CHANGED_FILES
32+
else
33+
echo "No PHP files changed"
34+
fi

.husky/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
. .husky/pre-commit-phpcbf.sh

.husky/pre-commit-phpcbf.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/sh
2+
3+
# Disable exit on error since we handle exit codes manually
4+
set +e
5+
6+
# Get list of staged PHP files
7+
STAGED_PHP_FILES=$(git diff --cached --name-only --diff-filter=d | grep ".php$") || true
8+
9+
# Exit if no PHP files are staged
10+
if [ -z "$STAGED_PHP_FILES" ]; then
11+
exit 0
12+
fi
13+
14+
echo "Running PHPCBF on staged files..."
15+
16+
# First try to fix what we can with PHPCBF
17+
echo "$STAGED_PHP_FILES" | xargs vendor/bin/phpcbf
18+
19+
echo 'does this run?'
20+
21+
# Check the return code
22+
PHPCBF_STATUS=$?
23+
24+
# If files were fixed, add them back to staging
25+
if [ $PHPCBF_STATUS -ne 3 ]; then
26+
echo "$STAGED_PHP_FILES" | xargs git add
27+
28+
if [ $PHPCBF_STATUS -eq 1 ] || [ $PHPCBF_STATUS -eq 2 ]; then
29+
echo "\nShowing remaining errors after auto-fixing:"
30+
echo "$STAGED_PHP_FILES" | xargs vendor/bin/phpcs
31+
fi
32+
fi
33+
34+
# Now run phpcs-changed to check only modified lines
35+
echo "Checking modified lines with phpcs-changed..."
36+
STAGED_FILES_SPACE_SEPARATED=$(echo "$STAGED_PHP_FILES" | tr '\n' ' ')
37+
38+
# Run phpcs-changed
39+
vendor/bin/phpcs-changed\
40+
--git-staged\
41+
--phpcs-path=vendor/bin/phpcs\
42+
--standard=.phpcs.xml.dist\
43+
$STAGED_FILES_SPACE_SEPARATED
44+
45+
PHPCS_STATUS=$?
46+
47+
if [ $PHPCS_STATUS -ne 0 ]; then
48+
echo "⛔️ Found coding standards violations in changed lines. Please fix them before committing."
49+
exit 1
50+
fi
51+
52+
echo "✅ All coding standards checks passed!"
53+
exit 0

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"require-dev": {
1717
"wp-coding-standards/wpcs": "^3.0",
18-
"phpcompatibility/phpcompatibility-wp": "^2.1"
18+
"phpcompatibility/phpcompatibility-wp": "^2.1",
19+
"sirbrillig/phpcs-changed": "^2.11"
1920
}
2021
}

composer.lock

Lines changed: 60 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 26 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"babel-loader": "^9.2.1",
77
"css-loader": "^7.1.2",
88
"css-minimizer-webpack-plugin": "^7.0.0",
9+
"husky": "^9.0.11",
910
"mini-css-extract-plugin": "^2.9.1",
1011
"sass": "^1.79.5",
1112
"sass-loader": "^16.0.2",
@@ -17,7 +18,8 @@
1718
"scripts": {
1819
"build": "webpack",
1920
"watch": "webpack --watch",
20-
"clean": "rm -rf assets/build/js/*.js assets/build/css/*.css assets/build/js/*.map assets/build/css/*.map assets/build/css/*.js"
21+
"clean": "rm -rf assets/build/js/*.js assets/build/css/*.css assets/build/js/*.map assets/build/css/*.map assets/build/css/*.js",
22+
"prepare": "husky"
2123
},
2224
"dependencies": {
2325
"md5": "^2.3.0"

0 commit comments

Comments
 (0)