name: Coding Standards

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

permissions:
  contents: read

jobs:
  Scan:
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        php: [5.6, 7.4, '8.0', 8.1, 8.2, 8.3, 8.4, 8.5]

    name: PHP ${{ matrix.php }}

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

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

      - name: PHP 5.6 syntax check
        run: |
          find . -type f -iname "*.php" ! -name 'ecs.php' ! -name 'rector.php' ! -path 'tests/*' -print0 | xargs -0 -L 1 php -l | (! grep -v "No syntax errors detected")
          echo $?
        if: matrix.php == 5.6

      - name: Install dependencies for PHP 5.6
        run: composer update --quiet --no-scripts
        if: matrix.php == 5.6

      - name: PHP 7.4+ syntax check
        run: |
          find . -type f -iname "*.php" -print0 | xargs -0 -L 1 php -l | (! grep -v "No syntax errors detected")
          echo $?
        if: matrix.php >= 7.4

      - name: Install dependencies for PHP 7.4+
        run: composer install --quiet --no-scripts
        if: matrix.php >= 7.4

      - name: Execute tests
        run: vendor/bin/phpunit --verbose

      - name: Install additional dependencies
        run: |
          composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
          composer require rector/rector squizlabs/php_codesniffer symplify/easy-coding-standard slevomat/coding-standard --dev --quiet --no-scripts
        if: matrix.php == 8.4

      - name: Execute PHPCodeSniffer
        run: vendor/bin/phpcs -n -s --standard=PSR12 src
        if: matrix.php == 8.4

      - name: Execute Rector
        run: vendor/bin/rector process src --dry-run
        if: matrix.php == 8.4

      - name: Execute ECS
        run: vendor/bin/ecs check src
        if: matrix.php == 8.4

      - name: Execute PHPStan
        run: vendor/bin/phpstan -v analyse src
        if: matrix.php == 8.4
