name: CI

on:
  pull_request:

jobs:
  static-analysis-phpstan:
    name: "Static Analysis with PHPStan"
    runs-on: "ubuntu-latest"

    strategy:
      matrix:
        php-version:
          - "7.4"

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

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

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

      - name: "Install dependencies with composer"
        run: "composer install --no-progress --no-suggest --no-interaction --prefer-dist"

      - name: "Run a static analysis with phpstan/phpstan"
        run: "php vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr"

  static-analysis-psalm:
    name: "Static Analysis with Psalm"
    runs-on: "ubuntu-latest"

    strategy:
      matrix:
        php-version:
          - "7.4"

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

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

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

      - name: "Install dependencies with composer"
        run: "composer install --no-interaction --no-progress --no-suggest"

      - name: "Run a static analysis with vimeo/psalm"
        run: "vendor/bin/psalm --show-info=false --stats --output-format=github --threads=$(nproc)"

  coding-standards:
    name: "Coding Standards"
    runs-on: "ubuntu-latest"

    strategy:
      matrix:
        php-version:
          - "7.4"

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

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

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

      - name: "Install dependencies with composer"
        run: "composer install --no-interaction --no-progress --no-suggest"

      - name: "Install git-phpcs"
        run: "wget https://github.com/diff-sniffer/git/releases/download/0.3.2/git-phpcs.phar"

      - name: "Fetch head branch"
        run: "git remote set-branches --add origin $GITHUB_BASE_REF && git fetch origin $GITHUB_BASE_REF"

      - name: "Run git-phpcs"
        run: "php git-phpcs.phar origin/$GITHUB_BASE_REF...$GITHUB_SHA --report=checkstyle|cs2pr"
