Skip to content

Instantly share code, notes, and snippets.

@o0h
Created October 29, 2023 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save o0h/2b34134b4a6830d963b80755bdbb529e to your computer and use it in GitHub Desktop.
Save o0h/2b34134b4a6830d963b80755bdbb529e to your computer and use it in GitHub Desktop.
ブログ用: staabm/phpstan-baseline-analysisをGitHub Actions上で遊ぶ
#!/usr/bin/env bash
pj_root_dir="$(dirname "$(readlink -f "$0")")"
target_dir="${pj_root_dir}/nanika-no-pj-src/.phpstan-baselines"
# ポイント①
pushd nanika-no-pj-src
for ((day_before=0; day_before<=60; day_before++)); do
# ポイント②
git clean -f
# ポイント③
((day_before > 0)) && git checkout "$(git rev-list -n 1 --before="${day_before} days ago" HEAD)"
php -r "\
\$allIgnoreErrors = [];
foreach (glob('${target_dir}/*') as \$file) {
\$baseline = require(\$file);
\$ignoreErrors = \$baseline['parameters']['ignoreErrors'] ?? [];
\$allIgnoreErrors = array_merge(\$allIgnoreErrors, \$ignoreErrors);
}
\$allIgnoreErrorsExport =var_export(\$allIgnoreErrors, 1);
\$output = <<<BASELINE
<?php declare(strict_types = 1);
\\\$ignoreErrors = {\$allIgnoreErrorsExport};
return ['parameters' => ['ignoreErrors' => \\\$ignoreErrors]];
BASELINE;
file_put_contents('${target_dir}/--union--.php', \$output);
"
for file in "$target_dir"/*; do
[ -f "$file" ] || continue
grep -q "\$ignoreErrors" "$file" || continue
[ "$day_before" -eq 0 ] && "$pj_root_dir"/vendor/bin/phpstan-baseline-analyze "$file" > "${pj_root_dir}/docs/$(basename "${file}" .php).txt"
# ポイント④
result_path="${pj_root_dir}/tmp/$(basename "${file}" .php)-$(git --no-pager log -1 --date=format:'%Y%m%d' --format='%ad').json"
tmp_result_path="${result_path}.tmp"
"$pj_root_dir"/vendor/bin/phpstan-baseline-analyze "$file" --json > "$tmp_result_path"
# ポイント⑤
jq --arg refDate "$(git --no-pager log -1 --pretty=format:'%cD')" '.[0][$file].Date = $refDate' --arg file "$file" "$tmp_result_path" > "$result_path"
rm "$tmp_result_path"
done
done
# ポイント①
popd
php vendor/bin/phpstan-baseline-graph 'tmp/*.json' > docs/trend.html
php vendor/bin/phpstan-baseline-graph 'tmp/--union--*.json' > docs/trend-union.html
cat <<EOC > docs/index.md
<iframe src="./trend-union.html" width="100%" height="220"></iframe>
[トレンドの詳細を見る](./trend.html)
## ファイル別のstats
EOC
for file in docs/*.txt; do
printf "### $(basename "$file" .txt)\n\`\`\`\n%s\n\`\`\`\n\n" "$(cat "$file")" >> docs/index.md
done
name: PHPStan Baseline Analysis
on:
push:
schedule:
- cron: '15 23 * * *'
jobs:
analyse:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Checkout nanika-no-pj
uses: actions/checkout@v3
with:
repository: github-org/nanika-no-pj
path: nanika-no-pj-src
token: ${{ secrets.PAT }}
fetch-depth: 0
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: latest
tools: composer
coverage: none
- name: Composer install
run: composer install
- name: analyze
run: ./main.sh
- name: Commit new files
run: |
git config user.name ${GITHUB_ACTOR}
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git add .
git commit -m"[bot]Update PHPStan Baseline Analysis"
git push origin ${GITHUB_REF_NAME}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment