Adjust phan script to run sub-projects
[lhc/web/wiklou.git] / tests / phan / bin / phan
1 #!/bin/bash
2
3 # mediawiki-vagrant installs dont have realpath by default
4 if ! which realpath > /dev/null; then
5 realpath() {
6 php -r "echo realpath('$*');"
7 }
8 fi
9
10 # Note that this isn't loaded in via composer because then composer can
11 # only be run with php7.0
12 if [ ! -f "$PHAN" ]; then
13 echo "The environment variable PHAN must point to the 'phan' file"
14 echo "in a checkout of https://github.com/etsy/phan.git"
15 exit 1
16 fi
17
18 if [ -z "$MW_INSTALL_PATH" ]; then
19 # Figure out where mediawiki is based on the location of this script
20 pushd "$(dirname "$0")" > /dev/null
21 export MW_INSTALL_PATH="$(git rev-parse --show-toplevel)"
22 popd >/dev/null
23 fi
24
25 # If the first argument doesn't start with a -, then it's a path
26 # to another project (extension, skin, etc.) to analyze
27 if [[ "$1" != "-"* ]]; then
28 cd $1
29 shift
30 else
31 cd "$(dirname "$0")"
32 fi
33
34 # Root directory of project
35 export ROOT="$(git rev-parse --show-toplevel)"
36
37 # Go to the root of this git repo
38 cd "$ROOT"
39
40 export CONFIG_FILE="$ROOT/tests/phan/config.php"
41 if [ ! -f "$CONFIG_FILE" ]; then
42 echo "Could not find a phan config file to apply in"
43 echo "$CONFIG_FILE"
44 exit 1
45 fi
46
47 # Phan's issues directory
48 export ISSUES="${ROOT}/tests/phan/issues"
49 mkdir -p "$ISSUES"
50
51 # Get the current hash of HEAD
52 export REV="$(git rev-parse HEAD)"
53
54 # Destination for issues found
55 export RUN="${ISSUES}/issues-${REV}"
56
57
58 # Run the analysis, emitting output to the
59 # issues file.
60 php7.0 $PHAN \
61 --project-root-directory "$ROOT" \
62 --config-file "$CONFIG_FILE" \
63 --output "php://stdout" \
64 "${@}" \
65 | php "$MW_INSTALL_PATH/tests/phan/bin/postprocess-phan.php" "${@}" \
66 > $RUN
67
68 EXIT_CODE="$?"
69
70 # Re-link the latest file
71 rm -f "${ISSUES}/latest"
72 ln -s "${RUN}" "${ISSUES}/latest"
73
74 # Output any issues that were found
75 cat "${RUN}"
76
77 exit $EXIT_CODE