#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PHP_BIN="${PHP_BIN:-php}"

echo "Using PHP: $("$PHP_BIN" -r 'echo PHP_VERSION;')"

find "$ROOT_DIR" \
  -path "$ROOT_DIR/.svn" -prune -o \
  -path "$ROOT_DIR/vendor/freemius" -prune -o \
  -name '*.php' -type f -print0 |
while IFS= read -r -d '' file; do
  "$PHP_BIN" -d display_errors=1 -d error_reporting='E_ALL & ~E_DEPRECATED' -l "$file" >/dev/null
done

if [ -n "${WP_PATH:-}" ] && [ -x "${WP_CLI_BIN:-/opt/homebrew/bin/wp}" ]; then
  "${WP_CLI_BIN:-/opt/homebrew/bin/wp}" plugin list --path="$WP_PATH" --fields=name,status,version
fi

echo "PHP lint completed."
