name: build
on:
    push: ~
    pull_request: ~
jobs:
    linux_tests:
        name: PHP ${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.flags }}
        runs-on: ubuntu-22.04

        strategy:
            matrix:
                php: ['8.1', '8.2', '8.3', '8.4', '8.5']
                stability: [prefer-lowest, prefer-stable]

                include:
                    - php: '8.6'
                      stability: prefer-stable
                      flags: "--ignore-platform-req=php"
            fail-fast: false

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

            - name: Setup PHP
              uses: shivammathur/setup-php@v2
              with:
                  php-version: ${{ matrix.php }}
                  coverage: xdebug
                  tools: composer:v2

            - name: Validate composer files
              run: composer validate --strict

            # Cache "vendor" so stable builds skip installation entirely
            - name: Cache vendor
              uses: actions/cache@v3
              with:
                  path: vendor
                  key: ${{ runner.os }}-vendor-${{ hashFiles('composer.lock') }}
                  restore-keys: |
                      ${{ runner.os }}-vendor-

            # INSTALL logic
            # 1. STABLE php + STABLE deps → composer install (NO updates)
            - name: Install dependencies (install)
              if: ${{ matrix.stability == 'prefer-stable' && matrix.php != '8.6' }}
              run: composer install --prefer-dist --no-interaction --no-progress

            # 2. prefer-lowest → must use composer update
            - name: Install dependencies (prefer-lowest)
              if: ${{ matrix.stability == 'prefer-lowest' }}
              run: composer update --prefer-lowest --prefer-dist --no-interaction --no-progress

            # 3. PHP 8.6 dev → always update
            - name: Install dependencies (nightly)
              if: ${{ matrix.php == '8.6' }}
              run: composer update --prefer-stable --prefer-dist --no-interaction --no-progress ${{ matrix.flags }}

            - name: Run Unit tests with coverage
              if: ${{ matrix.php != '8.5' && matrix.php != '8.6' }}
              run: composer phpunit

            - name: Run Unit tests without uri-polyfill
              if: ${{ matrix.php == '8.5' }}
              run: composer phpunit:no-polyfill

            - name: Run Unit tests without coverage (nightly)
              if: ${{ matrix.php == '8.6' }}
              run: composer phpunit:no-coverage
              continue-on-error: true

            # STATIC ANALYSIS (only once)
            - name: Run static analysis
              if: ${{ matrix.php == '8.4' && matrix.stability == 'prefer-stable' }}
              run: composer phpstan:stable

            # CODING STYLE (only once)
            - name: Run Coding style rules
              if: ${{ matrix.php == '8.4' && matrix.stability == 'prefer-stable' }}
              run: composer phpcs:fix
