weedops-devtools/scripts/lint-php.sh
Alex 010984d461 Initial commit: Weedops dev tooling
Linting configs (PHPCS, ESLint, Stylelint), Forgejo CI pipeline,
WordPress health check, PHP linter, strain migration tool,
and Docker local dev environment.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 05:55:00 +00:00

84 lines
2.3 KiB
Bash
Executable file

#!/bin/bash
# PHP Linting Script for Weedops Projects
# Usage: ./lint-php.sh [path-to-check]
set -euo pipefail
TARGET="${1:-.}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PHPCS_CONFIG="$SCRIPT_DIR/../linting/.phpcs.xml"
echo "Weedops PHP Linter"
echo "==================="
echo "Target: $TARGET"
echo ""
# Check for PHP syntax errors first
echo "--- PHP Syntax Check ---"
SYNTAX_ERRORS=0
while IFS= read -r -d '' file; do
if ! php -l "$file" &>/dev/null; then
echo "SYNTAX ERROR: $file"
php -l "$file" 2>&1 | tail -1
((SYNTAX_ERRORS++))
fi
done < <(find "$TARGET" -name "*.php" -not -path "*/vendor/*" -not -path "*/node_modules/*" -print0)
if [ "$SYNTAX_ERRORS" -eq 0 ]; then
echo "All PHP files pass syntax check."
else
echo ""
echo "Found $SYNTAX_ERRORS file(s) with syntax errors!"
exit 1
fi
echo ""
# Run PHPCS if available
echo "--- WordPress Coding Standards ---"
if command -v phpcs &> /dev/null; then
if [ -f "$PHPCS_CONFIG" ]; then
phpcs --standard="$PHPCS_CONFIG" "$TARGET" || true
else
phpcs --standard=WordPress --extensions=php --ignore=vendor,node_modules "$TARGET" || true
fi
else
echo "phpcs not found. Install with: composer global require squizlabs/php_codesniffer"
echo "Running basic checks instead..."
echo ""
# Basic checks without PHPCS
echo "Checking for common issues..."
ISSUES=0
# Check for short PHP tags
SHORT_TAGS=$(grep -rn '<?[^p=]' "$TARGET" --include="*.php" 2>/dev/null | grep -v "<?php" | grep -v "<?=" || true)
if [ -n "$SHORT_TAGS" ]; then
echo "WARNING: Short PHP tags found:"
echo "$SHORT_TAGS"
((ISSUES++))
fi
# Check for direct database queries
DIRECT_DB=$(grep -rn '\$wpdb->query\|mysql_query\|mysqli_query' "$TARGET" --include="*.php" 2>/dev/null || true)
if [ -n "$DIRECT_DB" ]; then
echo "WARNING: Direct database queries found (use prepared statements):"
echo "$DIRECT_DB"
((ISSUES++))
fi
# Check for eval usage
EVAL_USAGE=$(grep -rn '\beval\s*(' "$TARGET" --include="*.php" 2>/dev/null || true)
if [ -n "$EVAL_USAGE" ]; then
echo "WARNING: eval() usage found:"
echo "$EVAL_USAGE"
((ISSUES++))
fi
if [ "$ISSUES" -eq 0 ]; then
echo "No common issues found."
fi
fi
echo ""
echo "Done."