#!/bin/sh

set -eu

# Always execute from the repository root so the relative paths in the
# CodeSniffer command remain stable.
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
cd "$REPO_ROOT"

echo "Running Drupal coding standards check (phpcs in Docker)..."
if ! docker exec prometheus_metrics_drupal \
  vendor/bin/phpcs \
  --standard=Drupal,DrupalPractice \
  web/modules/contrib/prometheus_metrics/; then
  echo >&2 "Coding standards failed. Fix the reported issues before committing."
  exit 1
fi

echo "Coding standards look good."

echo "Running PHPStan analysis..."
if ! docker exec prometheus_metrics_drupal \
  vendor/bin/phpstan analyze \
  -c web/modules/contrib/prometheus_metrics/phpstan.neon \
  web/modules/contrib/prometheus_metrics \
  --autoload-file="vendor/autoload.php"; then
  echo >&2 "PHPStan analysis failed. Fix the reported issues before committing."
  exit 1
fi

echo "PHPStan analysis looks good."

