|
| 1 | +## |
| 2 | +# A reusable workflow that runs PHP Static Analysis tests. |
| 3 | +## |
| 4 | +name: PHP Static Analysis |
| 5 | + |
| 6 | +on: |
| 7 | + workflow_call: |
| 8 | + inputs: |
| 9 | + php-version: |
| 10 | + description: 'The PHP version to use.' |
| 11 | + required: false |
| 12 | + type: 'string' |
| 13 | + default: 'latest' |
| 14 | + |
| 15 | +# Disable permissions for all available scopes by default. |
| 16 | +# Any needed permissions should be configured at the job level. |
| 17 | +permissions: {} |
| 18 | + |
| 19 | +jobs: |
| 20 | + # Runs PHP static analysis tests. |
| 21 | + # |
| 22 | + # Violations are reported inline with annotations. |
| 23 | + # |
| 24 | + # Performs the following steps: |
| 25 | + # - Checks out the repository. |
| 26 | + # - Sets up PHP. |
| 27 | + # - Logs debug information. |
| 28 | + # - Installs Composer dependencies. |
| 29 | + # - Configures caching for PHP static analysis scans. |
| 30 | + # - Make Composer packages available globally. |
| 31 | + # - Runs PHPStan static analysis (with Pull Request annotations). |
| 32 | + # - Saves the PHPStan result cache. |
| 33 | + # - Ensures version-controlled files are not modified or deleted. |
| 34 | + phpstan: |
| 35 | + name: Run PHP static analysis |
| 36 | + runs-on: ubuntu-24.04 |
| 37 | + permissions: |
| 38 | + contents: read |
| 39 | + timeout-minutes: 20 |
| 40 | + |
| 41 | + steps: |
| 42 | + - name: Checkout repository |
| 43 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 44 | + with: |
| 45 | + show-progress: ${{ runner.debug == '1' && 'true' || 'false' }} |
| 46 | + persist-credentials: false |
| 47 | + |
| 48 | + - name: Set up Node.js |
| 49 | + uses: actions/setup-node@2028fbc5c25fe9cf00d9f06a71cc4710d4507903 # v6.0.0 |
| 50 | + with: |
| 51 | + node-version-file: '.nvmrc' |
| 52 | + cache: npm |
| 53 | + |
| 54 | + - name: Set up PHP |
| 55 | + uses: shivammathur/setup-php@20529878ed81ef8e78ddf08b480401e6101a850f # v2.35.3 |
| 56 | + with: |
| 57 | + php-version: ${{ inputs.php-version }} |
| 58 | + coverage: none |
| 59 | + tools: cs2pr |
| 60 | + |
| 61 | + # This date is used to ensure that the Composer cache is cleared at least once every week. |
| 62 | + # http://man7.org/linux/man-pages/man1/date.1.html |
| 63 | + - name: "Get last Monday's date" |
| 64 | + id: get-date |
| 65 | + run: echo "date=$(/bin/date -u --date='last Mon' "+%F")" >> "$GITHUB_OUTPUT" |
| 66 | + |
| 67 | + - name: General debug information |
| 68 | + run: | |
| 69 | + npm --version |
| 70 | + node --version |
| 71 | + composer --version |
| 72 | +
|
| 73 | + # Since Composer dependencies are installed using `composer update` and no lock file is in version control, |
| 74 | + # passing a custom cache suffix ensures that the cache is flushed at least once per week. |
| 75 | + - name: Install Composer dependencies |
| 76 | + uses: ramsey/composer-install@3cf229dc2919194e9e36783941438d17239e8520 # v3.1.1 |
| 77 | + with: |
| 78 | + custom-cache-suffix: ${{ steps.get-date.outputs.date }} |
| 79 | + |
| 80 | + - name: Make Composer packages available globally |
| 81 | + run: echo "${PWD}/vendor/bin" >> "$GITHUB_PATH" |
| 82 | + |
| 83 | + - name: Install npm dependencies |
| 84 | + run: npm ci --ignore-scripts |
| 85 | + |
| 86 | + - name: Build WordPress |
| 87 | + run: npm run build:dev |
| 88 | + |
| 89 | + - name: Cache PHP Static Analysis scan cache |
| 90 | + uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 |
| 91 | + with: |
| 92 | + path: .cache # This is defined in the base.neon file. |
| 93 | + key: "phpstan-result-cache-${{ github.run_id }}" |
| 94 | + restore-keys: | |
| 95 | + phpstan-result-cache- |
| 96 | +
|
| 97 | + - name: Run PHP static analysis tests |
| 98 | + id: phpstan |
| 99 | + run: composer run phpstan -- -vvv --error-format=checkstyle | cs2pr --errors-as-warnings --graceful-warnings |
| 100 | + |
| 101 | + - name: "Save result cache" |
| 102 | + uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 |
| 103 | + if: ${{ !cancelled() }} |
| 104 | + with: |
| 105 | + path: .cache |
| 106 | + key: "phpstan-result-cache-${{ github.run_id }}" |
| 107 | + |
| 108 | + - name: Ensure version-controlled files are not modified or deleted |
| 109 | + run: git diff --exit-code |
0 commit comments