# Copyright 2023 Rob Spoor
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Code Analysis

on:
  workflow_dispatch:
  push:
    # only run for branches, not tags
    branches-ignore: php8
  pull_request:

jobs:
  sonarcloud:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Set up PHP 7.4
        uses: shivammathur/setup-php@v2
        with:
          php-version: '7.4'
          coverage: xdebug
      - name: Cache Composer packages
        id: composer-cache
        uses: actions/cache@v3
        with:
          path: vendor
          key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-php-
      - name: Install dependencies
        run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
      - name: PHP Unit
        run: |
          echo '{}' | jq '.http.upload.skip |= ${{ secrets.SKIP_HTTP_UPLOAD }}' > tests/config.json
          vendor/bin/phpunit tests --coverage-clover=coverage.xml
          sed -i 's~'$GITHUB_WORKSPACE'~/github/workspace/~g' coverage.xml
      - name: Read composer.json
        run: |
          echo "projectName=`jq -r .name composer.json | sed 's~/~:~'`" >> $GITHUB_ENV
          echo "projectDescription=`jq -r .description composer.json`" >> $GITHUB_ENV
          echo "projectHomepage=`jq -r .homepage composer.json`" >> $GITHUB_ENV
          echo "projectBugUrl=`jq -r .support.issues composer.json`" >> $GITHUB_ENV
          echo "projectRepositoryUrl=`git remote get-url origin | sed 's~git@github.com:~https://github.com/~'`" >> $GITHUB_ENV
      - name: SonarQube Scan
        uses: sonarsource/sonarqube-scan-action@master
        env:
          SONAR_TOKEN: ${{ secrets.SonarCloudToken }}
          SONAR_HOST_URL: https://sonarcloud.io
        with:
          # omitted for now: -Dsonar.projectDescription=${{ env.projectDescription }}
          args: |
            -Dsonar.organization=robtimus-github
            -Dsonar.projectKey=${{ env.projectName }}
            -Dsonar.links.homepage=${{ env.projectHomepage }}
            -Dsonar.links.issue=${{ env.projectBugUrl }}
            -Dsonar.links.scm=${{ env.projectRepositoryUrl }}
            -Dsonar.sources=src
            -Dsonar.tests=tests
            -Dsonar.php.coverage.reportPaths=coverage.xml
      - name: SonarQube Quality Gate check
        uses: sonarsource/sonarqube-quality-gate-action@master
        timeout-minutes: 5
        env:
          SONAR_TOKEN: ${{ secrets.SonarCloudToken }}
          SONAR_HOST_URL: https://sonarcloud.io

  phpstan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up PHP 7.4
        uses: shivammathur/setup-php@v2
        with:
          php-version: '7.4'
          coverage: xdebug
          tools: phpstan
      - name: Cache Composer packages
        id: composer-cache
        uses: actions/cache@v3
        with:
          path: vendor
          key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-php-
      - name: Install dependencies
        run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
      - name: PHPStan
        run: phpstan analyse
