name: CI

on:
  pull_request:
  push:

jobs:
  test:
    name: "PHPUnit PHP ${{ matrix.php }}"

    strategy:
      matrix:
        php: [ 8.1, 8.2, 8.3, 8.4 ]
        include:
          - php: '8.4'
            coverage: true

    runs-on: ubuntu-latest

    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: mbstring
          tools: composer, cs2pr
          coverage: pcov

      - uses: actions/checkout@v2

      - name: Cache Composer cache
        uses: actions/cache@v4
        with:
          path: ~/.composer/cache
          key: composer-cache-php${{ matrix.php }}

      - name: Composer install
        run: composer install --no-progress --no-suggest --no-interaction --prefer-dist --optimize-autoloader

      - name: Run PHPUnit
        run: vendor/bin/phpunit
        if: matrix.coverage != true

      - name: Run PHPUnit with code coverage
        run: vendor/bin/phpunit --coverage-clover coverage.xml
        if: matrix.coverage

      - name: Upload code coverage
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
        if: matrix.coverage

  psalm:
    name: "Psalm"

    runs-on: ubuntu-latest

    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.1
          extensions: mbstring
          tools: composer, cs2pr

      - uses: actions/checkout@v2

      - name: Cache Composer cache
        uses: actions/cache@v4
        with:
          path: ~/.composer/cache
          key: composer-cache-php8.1

      - name: Composer install
        run: composer install --no-progress --no-suggest --no-interaction --prefer-dist --optimize-autoloader

      - name: Psalm
        run: php vendor/bin/psalm --threads=2 --output-format=github --shepherd --stats

  phpstan:
    name: "PHPStan"

    runs-on: ubuntu-latest

    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.1
          extensions: mbstring
          tools: composer, cs2pr

      - uses: actions/checkout@v2

      - name: Cache Composer cache
        uses: actions/cache@v4
        with:
          path: ~/.composer/cache
          key: composer-cache-php8.1

      - name: Composer install
        run: composer install --no-progress --no-suggest --no-interaction --prefer-dist --optimize-autoloader

      - name: PHPStan
        run: php vendor/bin/phpstan analyse --error-format=checkstyle --no-progress src/ tests/ | cs2pr
