name: CI

on:
  push:
    branches:
      - master
  pull_request: ~

jobs:
  check_composer:
    name: Check composer.json
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: shivammathur/setup-php@v2
        with:
          coverage: none
          php-version: '8.2'
      - run: composer validate --strict --no-check-lock

  tests:
    runs-on: ${{ matrix.operating-system }}

    strategy:
      fail-fast: false
      matrix:
        php: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
        operating-system: [ ubuntu-latest ]
        composer_flags: [ '' ]
        include:
          - php: '7.4'
            composer_flags: '--prefer-lowest'
            operating-system: ubuntu-latest
          - php: '8.0'
            composer_flags: '--prefer-lowest'
            operating-system: ubuntu-latest
          - php: '8.1'
            operating-system: windows-latest

    steps:
      - name: Prepare git # see https://github.com/actions/checkout/issues/226#issue-606867805
        run: git config --global core.autocrlf false && git config --global core.eol lf
      - uses: actions/checkout@v4
      - uses: shivammathur/setup-php@v2
        with:
          php-version: "${{ matrix.php }}"
      - name: Install dependencies
        run: composer update --ansi --no-progress --prefer-dist --no-interaction ${{ matrix.composer_flags }}
      - name: Run tests
        run: vendor/bin/phpunit -v --colors=always --coverage-clover=coverage.clover
      - name: Upload code coverage
        continue-on-error: true
        run: wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover

  static_analysis:
    name: Static analysis
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: shivammathur/setup-php@v2
        with:
          coverage: "none"
          php-version: "8.3"
          ini-file: development
      - name: Install dependencies
        run: composer update --ansi --no-progress
      - name: Run phpstan
        run: vendor/bin/phpstan analyse --ansi --no-progress
