From dd4e98bde0cc4711c413f82783117253e2a0bf58 Mon Sep 17 00:00:00 2001
From: Perreal Guillaume <guillaume.perreal@irstea.fr>
Date: Tue, 20 Aug 2019 17:55:08 +0200
Subject: [PATCH] Ajout du script phpstan-watch.

---
 bin/phpstan-watch | 74 +++++++++++++++++++++++++++++++++++++++++++++++
 composer.json     |  3 ++
 2 files changed, 77 insertions(+)
 create mode 100755 bin/phpstan-watch

diff --git a/bin/phpstan-watch b/bin/phpstan-watch
new file mode 100755
index 0000000..b424f79
--- /dev/null
+++ b/bin/phpstan-watch
@@ -0,0 +1,74 @@
+#!/usr/bin/env bash
+set -e
+
+if [[ $1 = "-h" ]]; then
+    echo "Usage: $0 [source] !" >&2
+    exit 1
+fi
+
+if ! INOTIFYWAIT="$(which inotifywait)"; then
+    echo "inotifywait introuvable !" >&2
+    exit 1
+fi
+
+findProjectRoot() {
+    local DIR="$1"
+    if [[ $DIR = / ]]; then
+        echo "phpstan.neon introuvable !" >&2
+        return 1
+    fi
+
+    if [[ -e $DIR/phpstan.neon ]]; then
+        echo $DIR
+        return 0
+    fi
+    findProjectRoot "$(dirname $DIR)"
+}
+
+HERE="$(findProjectRoot $(readlink -f $PWD))"
+
+if [[ -x $HERE/vendor/bin/phpstan ]]; then
+    PHPSTAN="$HERE/vendor/bin/phpstan"
+else
+    if [[ -x $HERE/bin/phpstan ]]; then
+        PHPSTAN="$HERE/bin/phpstan"
+    else
+        if ! PHPSTAN=$(which phpstan); then
+            echo "phpstan introuvable !" >&2
+            exit 1
+        fi
+    fi
+fi
+
+TARGET="${1:-$HERE/src}"
+DIR="$(mktemp -d)"
+FIFO="$DIR/fifo"
+mkfifo "$FIFO"
+
+$INOTIFYWAIT --monitor --quiet --recursive --event=close_write --format=%w%f --outfile="$FIFO" "$TARGET" &
+WATCHER_PID=$!
+trap "kill ${WATCHER_PID} >/dev/null 2>&1; rm -rf $DIR" EXIT
+
+work() {
+    clear
+    echo "== Level $1 =="
+    echo "> $PHPSTAN analyse -n --ansi --level=$1 $2"
+    $PHPSTAN analyse -n --ansi --level="$1" "$2"
+    return $?
+}
+
+for LEVEL in $(seq 1 7); do
+
+    while ! work "$LEVEL" "$TARGET"; do
+        while read FNAME; do
+            if work "$LEVEL" "$FNAME"; then
+                break
+            fi
+        done < "$FIFO"
+    done
+
+done
+
+kill ${WATCHER_PID}
+
+wait
diff --git a/composer.json b/composer.json
index 31a4bd5..f25a059 100644
--- a/composer.json
+++ b/composer.json
@@ -18,6 +18,9 @@
         "phpstan/phpstan-phpunit": "^0.11",
         "phpstan/phpstan-symfony": "^0.11"
     },
+    "bin": {
+        "bin/phpstan-watch"
+    },
     "suggest": {
         "beberlei/assert": "Framework léger d'assertion de type.",
         "phpunit/phpunit": "Framework de tests unitaires.",
-- 
GitLab