Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions .github/workflows/ci-lint-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ on:
type: boolean
default: false
phpcs-command:
description: "PHPCS binary path or custom command. Leave empty for 'vendor/bin/phpcs'."
description: "PHPCS binary path or custom command. It must forward its arguments to PHPCS. Leave empty for 'vendor/bin/phpcs'."
type: string
default: ""
changed-files:
description: "Newline-separated changed files (ci-detect-changes' php-files). PHP files that still exist are linted file by file; if the list is empty or holds anything else (composer.json, composer.lock, PHPStan config), PHPCS lints the whole project. PHPStan always runs whole-project — it analyses cross-file dependencies."
type: string
default: ""
phpstan-command:
description: "Shell command that runs PHPStan. Leave empty for 'vendor/bin/phpstan analyse'."
description: "Shell command that runs PHPStan. It must forward its arguments to PHPStan. Leave empty for 'vendor/bin/phpstan analyse'."
type: string
default: ""
phpstan-level:
Expand Down Expand Up @@ -76,11 +76,35 @@ jobs:
run: composer validate --no-check-publish

- name: Install Composer dependencies
id: composer
uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0
with:
working-directory: ${{ inputs.working-dir }}
composer-options: ${{ inputs.composer-flags || '--prefer-dist --optimize-autoloader --no-scripts' }}

- name: Resolve PHPStan tmpDir
id: phpstan-tmp
if: ${{ inputs.enable-phpstan }}
shell: bash
run: |
set -euo pipefail
dir="$(vendor/bin/phpstan dump-parameters --json | jq -r '.tmpDir // empty')" || dir=""
echo "dir=$dir" >> "$GITHUB_OUTPUT"

# Per-run key so every run saves fresh results. The brackets stop one working-dir prefixing
# another, and the composer.lock hash starts a clean cache when dependencies change.
- name: Restore lint caches
id: lint-cache
uses: actions/cache/restore@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
env:
KEY_PREFIX: lint-php-${{ runner.os }}-${{ inputs.php-version || '8.3' }}-[${{ inputs.working-dir }}]-${{ hashFiles(format('{0}/composer.lock', inputs.working-dir)) }}-
with:
path: |
${{ runner.temp }}/phpcs.cache
${{ steps.phpstan-tmp.outputs.dir }}
key: ${{ env.KEY_PREFIX }}${{ github.run_id }}-${{ github.run_attempt }}
restore-keys: ${{ env.KEY_PREFIX }}

- name: Run PHPCS
shell: bash
env:
Expand All @@ -89,9 +113,9 @@ jobs:
run: |
set -euo pipefail
# phpcs-command is a command and runs as shell; the report flags are appended as "$@" so
# they stay argv. The pipe to cs2pr stays out here, so a non-zero phpcs still trips
# pipefail exactly as before.
args=(-q --report=checkstyle --runtime-set ignore_warnings_on_exit 1)
# they stay argv. A repo-root basepath keeps annotation paths right in a monorepo.
report="$RUNNER_TEMP/phpcs.xml"
args=(-q --report-full "--report-checkstyle=$report" "--cache=$RUNNER_TEMP/phpcs.cache" "--basepath=$GITHUB_WORKSPACE")
# php-files also lists composer.json, composer.lock and the PHPStan config, so a
# dependency or config change re-runs this job, and it can name files the change
# deleted. PHPCS only takes PHP files that exist; when anything else changed, a
Expand All @@ -105,17 +129,35 @@ jobs:
printf '%s\n' "$php_files" > "$tmp_list"
args=(--file-list="$tmp_list" "${args[@]}")
fi
bash -eo pipefail -c "$PHPCS_COMMAND \"\$@\"" _ "${args[@]}" \
| cs2pr --graceful-warnings
status=0
bash -eo pipefail -c "$PHPCS_COMMAND \"\$@\"" _ "${args[@]}" || status=$?
[ ! -s "$report" ] || cs2pr --graceful-warnings "$report"
exit "$status"

- name: Run PHPStan
if: ${{ inputs.enable-phpstan }}
if: ${{ !cancelled() && inputs.enable-phpstan && steps.composer.outcome == 'success' }}
shell: bash
env:
PHPSTAN_COMMAND: ${{ inputs.phpstan-command || 'vendor/bin/phpstan analyse' }}
PHPSTAN_LEVEL: ${{ inputs.phpstan-level }}
run: |
set -eo pipefail
args=()
args=(--error-format=checkstyle)
[ -z "$PHPSTAN_LEVEL" ] || args+=("--level=$PHPSTAN_LEVEL")
bash -eo pipefail -c "$PHPSTAN_COMMAND \"\$@\"" _ "${args[@]}"
# PHPStan reports paths relative to working-dir, but annotations resolve from the repo root.
root="$(cd "$GITHUB_WORKSPACE" && pwd -P)/"
prefix="$(pwd -P)/"
prefix="${prefix#"$root"}"
case "$prefix" in /* | *[\|\&\\\<\>\"]*) prefix="" ;; esac
bash -eo pipefail -c "$PHPSTAN_COMMAND \"\$@\"" _ "${args[@]}" \
| sed "s|<file name=\"\([^/]\)|<file name=\"$prefix\1|" \
| cs2pr --graceful-warnings

- name: Save lint caches
if: ${{ !cancelled() && steps.composer.outcome == 'success' }}
uses: actions/cache/save@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
with:
path: |
${{ runner.temp }}/phpcs.cache
${{ steps.phpstan-tmp.outputs.dir }}
key: ${{ steps.lint-cache.outputs.cache-primary-key }}
4 changes: 2 additions & 2 deletions .github/workflows/wp-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ on:
type: string
default: ""
phpcs-command:
description: "PHPCS binary path or custom command. Leave empty for ci-lint-php.yml's default."
description: "PHPCS binary path or custom command. It must forward its arguments to PHPCS. Leave empty for ci-lint-php.yml's default."
type: string
default: ""
phpstan-command:
description: "Shell command that runs PHPStan when enable-phpstan is true. Leave empty for ci-lint-php.yml's default."
description: "Shell command that runs PHPStan when enable-phpstan is true. It must forward its arguments to PHPStan. Leave empty for ci-lint-php.yml's default."
type: string
default: ""
test-js-command:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ you can copy as-is.
| [`ci-detect-changes.yml`](.github/workflows/ci-detect-changes.yml) | Buckets changed files, exposes per-type counts and file lists | [example](examples/ci-detect-changes.yml) |
| [`ci-lint-css.yml`](.github/workflows/ci-lint-css.yml) | Stylelint | [example](examples/ci-lint-css.yml) |
| [`ci-lint-js.yml`](.github/workflows/ci-lint-js.yml) | ESLint + optional `package.json` validation | [example](examples/ci-lint-js.yml) |
| [`ci-lint-php.yml`](.github/workflows/ci-lint-php.yml) | PHPCS with `cs2pr` annotations + optional PHPStan | [example](examples/ci-lint-php.yml) |
| [`ci-lint-php.yml`](.github/workflows/ci-lint-php.yml) | PHPCS and optional PHPStan, both annotated via `cs2pr`, with result caches | [example](examples/ci-lint-php.yml) |
| [`ci-test-js.yml`](.github/workflows/ci-test-js.yml) | Jest | [example](examples/ci-test-js.yml) |
| [`ci-test-php.yml`](.github/workflows/ci-test-php.yml) | PHPUnit, via `@wordpress/env` or standalone | [example](examples/ci-test-php.yml) |
| [`ci-test-a11y.yml`](.github/workflows/ci-test-a11y.yml) | pa11y-ci WCAG2AA | [example](examples/ci-test-a11y.yml) |
Expand Down