name: Build PHARs

on:
  # Run on pushes to master and on all pull requests.
  # Prevent the build from running when there are only irrelevant changes.
  push:
    branches:
      - master
    paths-ignore:
      - '**.md'
  pull_request:
  # Allow manually triggering the workflow.
  workflow_dispatch:

# Cancels all previous workflow runs for the same branch that have not yet completed.
concurrency:
  # The concurrency group contains the workflow name and the branch name.
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        # Deliberately missing PHP 8.0 as that PHAR is build and used in the test workflow.
        php: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.1', '8.2', '8.3', '8.4']

    name: "Build Phar on PHP: ${{ matrix.php }}"

    continue-on-error: ${{ matrix.php == '8.4' }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: none
          ini-values: phar.readonly=Off, error_reporting=-1, display_errors=On

      - name: Build the phars
        run: php scripts/build-phar.php

      # Both the below only check a file which is rarely changed and therefore unlikely to have issues.
      # This test is about testing that the phars are functional, *not* about whether the code style complies.
      - name: 'PHPCS: check code style using the Phar file to test the Phar is functional'
        run: php phpcs.phar ./scripts

      - name: 'PHPCBF: fix code style using the Phar file to test the Phar is functional'
        run: php phpcbf.phar ./scripts
