#!/usr/bin/env bash
# scripts/checks/phpcs.sh — PSR-12 + Yii2 conventions
# Called by lefthook (pre-commit, staged files) and CI (pr-base.yml, PR-range files).
#
# Args:
#   $1 — space- or newline-separated list of PHP file paths to lint, OR empty.
#        Empty input → exit 0 (no PHP files in the diff).
#
# Config:
#   PHPCS_STANDARD — defaults to phpcs.xml at repo root.
#
# Exit: 0 = clean, 1 = violations.

set -uo pipefail

files="${1:-}"
[ -z "$files" ] && exit 0

PHPCS_STANDARD="${PHPCS_STANDARD:-phpcs.xml}"

# shellcheck disable=SC2086
echo "$files" | tr ' ' '\n' | grep -E '\.php$' | xargs --no-run-if-empty \
    vendor/bin/phpcs \
        --standard="$PHPCS_STANDARD" \
        --report=full \
        --colors
