# https://help.github.com/en/categories/automating-your-workflow-with-github-actions

on:
  pull_request:
  push:
  schedule:
    - cron: '3 3 * * 1'

name: CI

jobs:
  php-lint:
    name: PHP Lint
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        php-version: [ '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ]

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Install PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-version }}
          coverage: none

      - name: PHP Lint
        run: find src tests -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l

  unit-tests:
    name: Unit tests

    runs-on: ubuntu-20.04

    needs: [ php-lint ]

    strategy:
      fail-fast: false
      matrix:
        php-version: [ '5.6', '7.0', '7.1', '7.2', '7.3' ]
        coverage: [ 'none' ]
        include:
          - php-version: 7.4
            coverage: xdebug

    steps:
      - name: Checkout
        uses: actions/checkout@v2

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

      - name: Cache dependencies installed with composer
        uses: actions/cache@v1
        with:
          path: ~/.cache/composer
          key: php${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}
          restore-keys: |
            php${{ matrix.php-version }}-composer-

      - name: Install Composer dependencies
        run: |
          composer update --with-dependencies --no-progress;
          composer show;

      - name: Run Tests
        run: ./vendor/bin/phpunit --coverage-clover build/coverage/xml

      - name: Upload coverage results to Codacy
        env:
          CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
        if: "${{ matrix.coverage != 'none' && env.CODACY_PROJECT_TOKEN != '' }}"
        run: |
          ./vendor/bin/codacycoverage clover build/coverage/xml

  static-analysis:
    name: Static Analysis

    runs-on: ubuntu-20.04

    needs: [ php-lint ]

    strategy:
      fail-fast: false
      matrix:
        include:
          - command: sniffer
            php-version: 7.4
          - command: fixer
            php-version: 7.4
          - command: stan
            php-version: 7.4

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Install PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-version }}
          tools: "composer:v2, phive"
          coverage: none

      - name: Cache dependencies installed with composer
        uses: actions/cache@v1
        with:
          path: ~/.cache/composer
          key: php${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}
          restore-keys: |
            php${{ matrix.php-version }}-composer-

      - name: Install Composer dependencies
        run: |
          composer update --with-dependencies --no-progress;
          composer show;

      - name: Install development tools
        run: |
          phive --no-progress install --trust-gpg-keys BBAB5DF0A0D6672989CF1869E82B2FB314E9906E,A972B9ABB95D0B760B51442231C7E470E2138192,D32680D5957DC7116BE29C14CF1A108D0E7AE720

      - name: Run Command
        run: composer ci:php:${{ matrix.command }}
