#!/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 '/dev/null | grep -v "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."