name: Lint and analyse files

permissions:
    contents: read

on:
    workflow_dispatch:
    pull_request:
        paths:
            - "**/*.js"
            - "**/*.php"
    push:
        paths:
            - "**/*.js"
            - "**/*.php"

jobs:
    lint-node:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4
            - uses: actions/setup-node@v4
              with:
                  node-version: 20
            - name: Get yarn cache directory path
              id: yarn-cache-dir-path
              run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
            - name: yarn cache
              uses: actions/cache@v4
              with:
                  path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
                  key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
                  restore-keys: |
                      ${{ runner.os }}-yarn-
            - name: Install modules
              run: yarn install
            - name: Lint files
              run: |
                  set +e
                  yarn run jshint --verbose
                  JSHINT=$?
                  echo "Running prettier"
                  yarn run prettier --list-different
                  PRETTIER=$?
                  if [[ $JSHINT != 0 ]] || [[ $PRETTIER != 0 ]]; then
                      echo "You have some errors to fix !";
                      exit 1;
                  fi

    lint-php:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4
            - name: Use php 7.2
              uses: shivammathur/setup-php@v2
              with:
                  php-version: 7.2
            - name: Validate composer.json and composer.lock
              run: composer validate
            - name: Cache module
              uses: actions/cache@v4
              with:
                  path: ~/.composer/cache/
                  key: composer-cache
            - name: Install dependencies
              run: composer install --no-interaction
            - name: Lint files
              run: composer run phpcs

    analyse-php:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4
            - name: Use php 7.2
              uses: shivammathur/setup-php@v2
              with:
                  php-version: 7.2
            - name: Cache module
              uses: actions/cache@v4
              with:
                  path: ~/.composer/cache/
                  key: composer-cache
            - name: Install dependencies
              run: composer install --no-interaction
            - name: Analyse files
              run: composer run phpstan
