Repository navigation
132 lines (115 loc) · 4.97 KB
/
Copy pathphp-sa.yml
File metadata and controls
132 lines (115 loc) · 4.97 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# This workflow performs static PHP code analysis on the specified PHP version with optional PHP extensions.
# It sets up PHP, installs code dependencies, and runs PHPStan as a static analysis tool.
# Additional dependencies/environment variables can be cloned/set as specified in the inputs.
# If a phpstan.neon file is present, scanDirectories are automatically adjusted to match the runtime environment;
# otherwise, PHPStan analyzes all main directories except vendor.
on:
workflow_call:
inputs:
php-version:
type: string
required: true
description: 'PHP versions to test against.'
php-extensions:
type: string
description: 'PHP extensions to install, comma separated list of strings.'
dependencies:
type: string
description: 'Dependencies to install, JSON encoded map of path => repository url.'
default: '{}'
env:
type: string
description: 'Environment variables to set, JSON encoded map of name => value.'
default: '{}'
setup-commands:
description: 'Custom setup commands (one per line)'
type: string
working-directory:
type: string
default: .
continue-on-error:
type: boolean
description: 'Enable PHPStan to fail gracefully. This workaround addresses the current lack of support by GitHub for continue-on-error in jobs that use reusable workflows.'
default: false
jobs:
phpstan:
name: PHPStan ${{ inputs.php-version }}
runs-on: ubuntu-latest
steps:
- name: Setup PHP and dependencies
uses: Icinga/github-actions/.github/actions/php-and-dependencies@main
with:
php-version: ${{ inputs.php-version }}
php-extensions: ${{ inputs.php-extensions }}
dependencies: ${{ inputs.dependencies }}
env: ${{ inputs.env }}
setup-commands: ${{ inputs.setup-commands }}
working-directory: ${{ inputs.working-directory }}
php-tools: phpstan
- name: Adjust PHPStan scanDirectories
if: ${{ env.PHPSTAN_SCANDIRS != ''}}
uses: Icinga/github-actions/.github/actions/phpstan-adjust-scandirs@main
with:
working-directory: ${{ inputs.working-directory }}
- name: Run PHPStan
# Not using working-directory as the GitHub Actions API
# expects file paths to be relative to the repository root.
# https://github.com/phpstan/phpstan-src/blob/2.1.33/src/Command/ErrorFormatter/GithubErrorFormatter.php#L35
run: |
echo "::group::PHPStan"
BLUE_BOLD='\033[1;34m'
NC='\033[0m' # No Color
log_cmd() {
printf "${BLUE_BOLD}"
printf '%s ' "${@}"
printf "${NC}\n"
}
args=()
configuration=""
if [ -f "${{ inputs.working-directory }}/phpstan.neon" ]; then
configuration="${{ inputs.working-directory }}/phpstan.neon"
fi;
# Supply level and scan paths as CLI arguments if PHPStan config is missing or generated.
if [ -z "$configuration" ] || [ -n "$PHPSTAN_CONFIG_GENERATED" ]; then
# Find all direct child directories containing PHP files; exclude vendor dirs.
php_dirs=()
while IFS= read -r -d '' dir; do
if [[ -n $(find "$dir" -type f -name "*.php" -print -quit) ]]; then
php_dirs+=("$dir")
fi
done < <(find "${{ inputs.working-directory }}" -mindepth 1 -maxdepth 1 \
-type d \
-not -name "vendor" \
-print0)
# Find all direct child PHP files.
php_files=($(find "${{ inputs.working-directory }}" -mindepth 1 -maxdepth 1 \
-type f -name '*.php'))
# Surprisingly, PHPStan does not seem to find composer.json when using a custom working directory.
# Therefore, we will simply pass the Composer autoloader explicitly, if applicable.
autoload=""
composer_json="${{ inputs.working-directory }}/composer.json"
if [ -f "$composer_json" ]; then
composer_vendor="$(composer -d ${{ inputs.working-directory }} config vendor-dir)"
autoload="${{ inputs.working-directory }}/${composer_vendor}/autoload.php"
fi
args=(--level=6)
if [ -n "$autoload" ]; then
args+=(-a "$autoload")
fi
args+=("${php_dirs[@]}" "${php_files[@]}")
fi
if [ -n "$configuration" ]; then
array_unshift() {
local -n arr="$1"
shift
arr=("$@" "${arr[@]}")
}
array_unshift args --configuration "$configuration"
fi
log_cmd phpstan analyse "${args[@]}"
if [ "${{ inputs.continue-on-error }}" = "true" ]; then
phpstan analyse "${args[@]}" || true
else
phpstan analyse "${args[@]}"
fi
echo "::endgroup::"