Fixing pre-commit hooks (#6891)

* Fixing pre-commit hooks

* Update composer.json

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
TheWitnessandCopilot authored and GitHub committed 2026-03-27 14:01:23 -04:00
1 parent 678039bec8
commit d8d5a944f0
3 files changed
+33 -14

No files matched your search

+3 -3
View File
@@ -3,21 +3,21 @@ repos:
hooks: hooks:
- id: lintcheck - id: lintcheck
name: PHP Lint Checking name: PHP Lint Checking
entry: sh scripts/pre-commit-check.sh lint entry: sh tests/tools/pre-commit-check.sh lint
language: system language: system
types: [php] types: [php]
always_run: true always_run: true
- id: phpcsfixer - id: phpcsfixer
name: PHP CS Fixer Validation name: PHP CS Fixer Validation
entry: sh scripts/pre-commit-check.sh phpcsfixer entry: sh tests/tools/pre-commit-check.sh phpcsfixer
language: system language: system
types: [php] types: [php]
always_run: true always_run: true
- id: phpstan - id: phpstan
name: PHPStan Validation name: PHPStan Validation
entry: sh scripts/pre-commit-check.sh phpstan entry: sh tests/tools/pre-commit-check.sh phpstan
language: system language: system
types: [php] types: [php]
always_run: true always_run: true
+3 -2
View File
@@ -69,8 +69,9 @@
"scripts": { "scripts": {
"lint": "phplint --no-cache --exclude=include/vendor ", "lint": "phplint --no-cache --exclude=include/vendor ",
"phpstan": "phpstan analyse --level 6 ", "phpstan": "phpstan analyse --level 6 ",
"phpcsfixer": "php-cs-fixer fix --allow-unsupported-php-version=yes --dry-run --diff --config=./.php-cs-fixer.php ", "php-cs-fixer": "php-cs-fixer fix --allow-unsupported-php-version=yes --dry-run --diff --config=./.php-cs-fixer.php ",
"phpcsfixit": "php-cs-fixer fix ", "phpcsfixer": "@php-cs-fixer",
"php-cs-fixit": "php-cs-fixer fix ",
"test": "include/vendor/bin/pest --display-warnings" "test": "include/vendor/bin/pest --display-warnings"
}, },
"authors": [ "authors": [
@@ -27,16 +27,20 @@ check_php_version() {
check_merge_conflicts() { check_merge_conflicts() {
staged=$(git diff --cached --name-only --diff-filter=ACM -- '*.php') staged=$(git diff --cached --name-only --diff-filter=ACM -- '*.php')
if [ -z "$staged" ]; then if [ -z "$staged" ]; then
return 0 return 0
fi fi
conflicts=$(echo "$staged" | xargs grep -l '^<<<<<<<\|^=======$\|^>>>>>>>' 2>/dev/null || true) conflicts=$(echo "$staged" | xargs grep -l '^<<<<<<<\|^=======$\|^>>>>>>>' 2>/dev/null || true)
if [ -n "$conflicts" ]; then if [ -n "$conflicts" ]; then
echo "" echo ""
echo "ERROR: Merge conflict markers found in staged files:" echo "ERROR: Merge conflict markers found in staged files:"
echo "$conflicts" echo "$conflicts"
echo " Resolve conflicts before committing." echo " Resolve conflicts before committing."
echo "" echo ""
exit 1 exit 1
fi fi
} }
@@ -48,12 +52,14 @@ check_composer_lock() {
echo " Cacti supports multiple PHP versions; composer.lock must not be committed." echo " Cacti supports multiple PHP versions; composer.lock must not be committed."
echo " Run: git reset HEAD composer.lock" echo " Run: git reset HEAD composer.lock"
echo "" echo ""
exit 1 exit 1
fi fi
} }
check_vendor_dev_deps() { check_vendor_dev_deps() {
staged_vendor=$(git diff --cached --name-only | grep '^include/vendor/' | head -5) staged_vendor=$(git diff --cached --name-only | grep '^include/vendor/' | head -5)
if [ -n "$staged_vendor" ]; then if [ -n "$staged_vendor" ]; then
echo "" echo ""
echo "WARNING: Vendor files are staged for commit:" echo "WARNING: Vendor files are staged for commit:"
@@ -61,22 +67,22 @@ check_vendor_dev_deps() {
echo " Dev dependencies should not be committed to include/vendor/." echo " Dev dependencies should not be committed to include/vendor/."
echo " Run: git reset HEAD include/vendor/" echo " Run: git reset HEAD include/vendor/"
echo "" echo ""
exit 1 exit 1
fi fi
} }
check_autoload_freshness() { check_autoload_freshness() {
if [ -f composer.json ] && [ -f include/vendor/autoload.php ]; then if [ ! -f include/vendor/autoload.php ]; then
if [ composer.json -nt include/vendor/autoload.php ]; then echo "WARNING: your are missing the composer autoload. Please run composer install"
echo "WARNING: composer.json is newer than vendor/autoload.php." exit 1
echo " Run: composer install --ignore-platform-reqs"
fi
fi fi
} }
check_tool() { check_tool() {
tool="$1" tool="$1"
label="$2" label="$2"
if [ ! -x "$VENDOR_BIN/$tool" ]; then if [ ! -x "$VENDOR_BIN/$tool" ]; then
echo "" echo ""
echo "ERROR: $label not found at $VENDOR_BIN/$tool" echo "ERROR: $label not found at $VENDOR_BIN/$tool"
@@ -86,15 +92,18 @@ check_tool() {
echo " Dev dependencies must be installed for pre-commit hooks." echo " Dev dependencies must be installed for pre-commit hooks."
echo " This is a one-time setup after cloning or switching branches." echo " This is a one-time setup after cloning or switching branches."
echo "" echo ""
exit 1 exit 1
fi fi
# Verify the tool can actually load (catches missing Symfony/autoload issues) # Verify the tool can actually load (catches missing Symfony/autoload issues)
if ! "$VENDOR_BIN/$tool" --version > /dev/null 2>&1; then if [ $($VENDOR_BIN/$tool --version > /dev/null 2>&1) -gt 0 ]; then
echo "" echo ""
echo "ERROR: $label exists but failed to load. Autoload may be stale." echo "ERROR: $label exists but failed to load. Autoload may be stale."
echo "" echo ""
echo " Run: composer install --ignore-platform-reqs" echo " Run: composer install --ignore-platform-reqs"
echo "" echo ""
exit 1 exit 1
fi fi
} }
@@ -102,37 +111,46 @@ check_tool() {
# ---- Lint / analysis tools ---- # ---- Lint / analysis tools ----
run_lint() { run_lint() {
if [ -x "$VENDOR_BIN/phplint" ] && "$VENDOR_BIN/phplint" --version > /dev/null 2>&1; then if [ -x "$VENDOR_BIN/phplint" ] && [ $("$VENDOR_BIN/phplint" --version > /dev/null 2>&1) -gt 0 ]; then
echo "Running PHP lint (phplint)..." echo "Running PHP lint (phplint)..."
composer run-script lint composer run-script lint
else else
echo "phplint not available or broken, falling back to php -l on staged files..." echo "phplint not available or broken, falling back to php -l on staged files..."
staged=$(git diff --cached --name-only --diff-filter=ACM -- '*.php') staged=$(git diff --cached --name-only --diff-filter=ACM -- '*.php')
if [ -z "$staged" ]; then if [ -z "$staged" ]; then
echo " No staged PHP files to check." echo " No staged PHP files to check."
return 0 return 0
fi fi
fail=0 fail=0
for f in $staged; do for f in $staged; do
if ! php -l "$f" > /dev/null 2>&1; then if ! php -l "$f" > /dev/null 2>&1; then
php -l "$f" php -l "$f"
fail=1 fail=1
fi fi
done done
if [ "$fail" -eq 1 ]; then if [ "$fail" -eq 1 ]; then
exit 1 exit 1
fi fi
echo " All staged PHP files pass syntax check." echo " All staged PHP files pass syntax check."
fi fi
} }
run_phpcsfixer() { run_phpcsfixer() {
check_tool "php-cs-fixer" "PHP CS Fixer" check_tool "php-cs-fixer" "PHP CS Fixer"
echo "Running PHP CS Fixer (dry-run)..." echo "Running PHP CS Fixer (dry-run)..."
if ! composer run-script phpcsfixer; then
if [ $(composer run-script php-cs-fixer) -gt 0 ]; then
echo "" echo ""
echo "TIP: To auto-fix formatting issues, run:" echo "TIP: To auto-fix formatting issues, run:"
echo " composer run-script phpcsfixit" echo " composer run-script php-cs-fixit"
echo "" echo ""
exit 1 exit 1
fi fi