-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphpstan.inc.bash
More file actions
executable file
·111 lines (95 loc) · 4.4 KB
/
Copy pathphpstan.inc.bash
File metadata and controls
executable file
·111 lines (95 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
set +e
phpStanExitCode=99
phpStanLogDir="$varDir/phpstan_logs"
phpStanLogFile="phpstan.log"
mkdir -p "$phpStanLogDir"
# Limit parallel processing to use only half of available CPU threads
# to avoid overwhelming the system (consistent with Rector configuration)
cpuThreads=$(nproc 2>/dev/null || echo 4)
maxProcesses=$(( cpuThreads / 2 ))
if (( maxProcesses < 1 )); then
maxProcesses=1
fi
phpStanParallelConfig="$phpStanLogDir/phpstan-parallel.neon"
cat > "$phpStanParallelConfig" << NEON
includes:
- $phpstanConfigPath
parameters:
parallel:
maximumNumberOfProcesses: $maxProcesses
NEON
phpstanConfigPath="$phpStanParallelConfig"
echo "PHPStan: limiting to $maxProcesses parallel processes (50% of $cpuThreads cores)"
phpstanNoProgress=()
if [[ "true" == "$CI" ]]; then
phpstanNoProgress+=(--no-progress)
fi
if [[ "1" == "${useJsonOutput:-0}" ]]; then
# JSON mode: single run, no retry loop, structured output
phpStanJsonFile="$phpStanLogDir/phpstan.json"
phpNoXdebug -f "$pharDir"/phpstan.phar -- \
analyse ${pathsToCheck[@]} \
-c "$phpstanConfigPath" \
--no-progress \
--error-format=json \
> "$phpStanJsonFile"
phpStanExitCode=$?
# Output JSON to fd 3 (original stdout, bypassing stderr redirect)
cat "$phpStanJsonFile" >&3
# Archive the JSON log
archiveToolLog "PHPStan" "$phpStanLogDir" "phpstan.json" "$specifiedPath" "${pathsToCheck[@]}"
if ((phpStanExitCode > 1)); then
echo "PHPStan crashed (exit code: $phpStanExitCode)" >&2
exit 1
fi
# Exit code 1 = found errors — expected for JSON consumption, don't retry
else
# Text mode: original behavior with retry loop
while ((phpStanExitCode > 0)); do
# Run PHPStan with tee to capture output to both file and stdout
phpNoXdebug -f "$pharDir"/phpstan.phar -- \
analyse ${pathsToCheck[@]} \
-c "$phpstanConfigPath" \
${phpstanNoProgress[@]:-} \
2>&1 | tee "$phpStanLogDir/$phpStanLogFile"
phpStanExitCode=${PIPESTATUS[0]}
# Archive PHPStan log with timestamp - keep last 10 per pattern
# Do this BEFORE tryAgainOrAbort so log is archived even on failure (in CI mode)
# Uses shared archiveToolLog function from functions.inc.bash
archiveToolLog "PHPStan" "$phpStanLogDir" "$phpStanLogFile" "$specifiedPath" "${pathsToCheck[@]}"
#exit code 0 = fine, 1 = ran fine but found errors, else it means it crashed
if ((phpStanExitCode > 1)); then
printf "\n\n\nPHPStan Crashed!!....\n\nrunning again with debug mode:\nWhere ever it stops is probably a fatal PHP error\n\n"
eval phpNoXdebug -f "$pharDir"/phpstan.phar -- analyse $pathsStringArray -c "$phpstanConfigPath" --debug -v
exit 1
fi
if ((phpStanExitCode > 0)); then
# Educational note for the "tautology from stronger types" identifiers. These
# fire when PHPStan can already PROVE the check from the declared types
# (alreadyNarrowedType / alwaysTrue / alwaysFalse / impossibleCheck). In TEST
# code this very often means a recent type-safety improvement made an
# assertion redundant — the production types now guarantee exactly what the
# test was asserting. We only print this when such an identifier is actually
# present, so it stays quiet for ordinary errors.
if grep -qE 'alreadyNarrowedType|alwaysTrue|alwaysFalse|impossibleCheck' "$phpStanLogDir/$phpStanLogFile"; then
printf '\n%s\n' \
"NOTE — possible tautology from stronger types
---------------------------------------------
One or more errors above (alreadyNarrowedType / alwaysTrue / alwaysFalse /
impossibleCheck) report a check PHPStan can already prove from the types alone.
In TEST code this is usually a GOOD sign: a type-safety improvement has made the
assertion tautological — the production types now guarantee what the test pinned.
When that is the case, DELETING the now-redundant assertion (or the whole
tautological test) is the CORRECT fix, not a workaround. The type system has
absorbed the guarantee the test used to provide; that is the goal, not a loss of
coverage. Keep the tests that still exercise real runtime behaviour.
In PRODUCTION code the same identifiers usually flag genuinely dead or redundant
logic — simplify it (remove the impossible branch / redundant comparison).
Either way, fix the CAUSE. Do NOT silence it with treatPhpDocTypesAsCertain:false,
a baseline entry, or a @phpstan-ignore comment."
fi
tryAgainOrAbort "PHPStan"
fi
done
fi
set -e