name: CS

on:
  # Run on all pushes and on all pull requests.
  # Prevent the build from running when there are only irrelevant changes.
  push:
  pull_request:
  # Allow manually triggering the workflow.
  workflow_dispatch:

# Cancels all previous workflow runs for the same branch that have not yet completed.
concurrency:
  # The concurrency group contains the workflow name and the branch name.
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  checkcs:
    name: 'Basic CS and QA checks'
    runs-on: ubuntu-latest

    env:
      # - COMPOSER_ROOT_VERSION is needed to get round the recursive dependency when using CI.
      COMPOSER_ROOT_VERSION: '1.99.99'

    steps:
      - name: Checkout code
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false

      - name: Install PHP
        uses: shivammathur/setup-php@bf6b4fbd49ca58e4608c9c89fba0b8d90bd2a39f # 2.35.5
        with:
          php-version: 'latest'
          coverage: none
          tools: cs2pr

      # Validate the composer.json file.
      # @link https://getcomposer.org/doc/03-cli.md#validate
      - name: Validate Composer installation
        run: composer validate --no-check-all --strict

      # Use the WIP/develop branches of the Extra CS dependencies as an early detection system for bugs upstream.
      - name: 'Composer: adjust dependencies'
        run: >
          composer require --no-update --no-scripts --no-interaction
          squizlabs/php_codesniffer:"3.x-dev"
          phpcsstandards/phpcsutils:"dev-develop"

      # Install dependencies and handle caching in one go.
      # @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
      - name: Install Composer dependencies
        uses: "ramsey/composer-install@3cf229dc2919194e9e36783941438d17239e8520" # 3.1.1
        with:
          # Bust the cache at least once a month - output format: YYYY-MM.
          custom-cache-suffix: $(date -u "+%Y-%m")

      # Validate XML files against schema.
      - name: Validate XML rulesets against schema
        uses: phpcsstandards/xmllint-validate@0fd9c4a9046055f621fca4bbdccb8eab1fd59fdc # v1.0.1
        with:
          pattern: "./*/ruleset.xml"
          xsd-file: "vendor/squizlabs/php_codesniffer/phpcs.xsd"

      - name: Validate documentation XML against schema
        uses: phpcsstandards/xmllint-validate@0fd9c4a9046055f621fca4bbdccb8eab1fd59fdc # v1.0.1
        with:
          pattern: "./*/Docs/*/*Standard.xml"
          xsd-file: "vendor/phpcsstandards/phpcsdevtools/DocsXsd/phpcsdocs.xsd"

      - name: Validate Project PHPCS ruleset against schema
        uses: phpcsstandards/xmllint-validate@0fd9c4a9046055f621fca4bbdccb8eab1fd59fdc # v1.0.1
        with:
          pattern: "phpcs.xml.dist"
          xsd-file: "vendor/squizlabs/php_codesniffer/phpcs.xsd"

      - name: "Validate PHPUnit config for use with PHPUnit 8"
        uses: phpcsstandards/xmllint-validate@0fd9c4a9046055f621fca4bbdccb8eab1fd59fdc # v1.0.1
        with:
          pattern: "phpunit.xml.dist"
          xsd-file: "vendor/phpunit/phpunit/schema/8.5.xsd"

      - name: "Validate PHPUnit config for use with PHPUnit 9"
        uses: phpcsstandards/xmllint-validate@0fd9c4a9046055f621fca4bbdccb8eab1fd59fdc # v1.0.1
        with:
          pattern: "phpunit.xml.dist"
          xsd-file: "vendor/phpunit/phpunit/schema/9.2.xsd"

      # Check the code-style consistency of the PHP files.
      - name: Check PHP code style
        id: phpcs
        run: vendor/bin/phpcs --report-full --report-checkstyle=./phpcs-report.xml

      - name: Show PHPCS results in PR
        if: ${{ always() && steps.phpcs.outcome == 'failure' }}
        run: cs2pr ./phpcs-report.xml

      # Check that the sniffs available are feature complete.
      # For now, just check that all sniffs have unit tests.
      # At a later stage the documentation check can be activated.
      - name: Check sniff feature completeness
        run: composer check-complete

  xml-cs:
    name: 'XML Code style'
    runs-on: ubuntu-latest

    env:
      XMLLINT_INDENT: '    '

    steps:
      - name: Checkout code
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false

      # Updating the lists can fail intermittently, typically after Microsoft has released a new package.
      # This should not be blocking for this job, so ignore any errors from this step.
      # Ref: https://github.com/dotnet/core/issues/4167
      - name: Update the available packages list
        continue-on-error: true
        run: sudo apt-get update

      - name: Install xmllint
        run: sudo apt-get install --no-install-recommends -y libxml2-utils

      # Show XML violations inline in the file diff.
      - name: Enable showing XML issues inline
        uses: korelstar/xmllint-problem-matcher@1bd292d642ddf3d369d02aaa8b262834d61198c0 # v1.2.0

      # Check the code-style consistency of the XML ruleset files.
      - name: Check XML code style
        run: |
          diff -B ./Modernize/ruleset.xml <(xmllint --format "./Modernize/ruleset.xml")
          diff -B ./NormalizedArrays/ruleset.xml <(xmllint --format "./NormalizedArrays/ruleset.xml")
          diff -B ./Universal/ruleset.xml <(xmllint --format "./Universal/ruleset.xml")

  phpstan:
    name: "PHPStan"
    uses: PHPCSStandards/.github/.github/workflows/reusable-phpstan.yml@main
    with:
      phpstanVersion: '2.x'

  markdownlint:
    name: 'Lint Markdown'
    uses: PHPCSStandards/.github/.github/workflows/reusable-markdownlint.yml@main

  remark:
    name: 'QA Markdown'
    uses: PHPCSStandards/.github/.github/workflows/reusable-remark.yml@main

  yamllint:
    name: 'Lint Yaml'
    uses: PHPCSStandards/.github/.github/workflows/reusable-yamllint.yml@main
    with:
      strict: true
