.static-analysis:
  image: gitlab.wpdesk.dev:5050/wpdesk/docker-tests/php-box:8.4.1
  needs:
    - prepare prefixed vendor
  stage: tests

phpstan:
  extends: .static-analysis
  script:
    - |
      if [ -f "vendor/bin/phpstan" ]; then
        vendor/bin/phpstan analyse --error-format=gitlab --memory-limit=2G > phpstan-codequality.json || true
        vendor/bin/phpstan analyse --error-format=junit --memory-limit=2G > phpstan-junit.xml
      elif [ -f "vendor/wpdesk/wp-wpdesk-composer/director/vendor/bin/phpstan" ]; then
        # PHPStan installed by director
        vendor/wpdesk/wp-wpdesk-composer/director/vendor/bin/phpstan analyse --error-format=gitlab --memory-limit=2G > phpstan-codequality.json || true
        vendor/wpdesk/wp-wpdesk-composer/director/vendor/bin/phpstan analyse --error-format=junit --memory-limit=2G > phpstan-junit.xml
      else
        echo "PHPStan not found! Install it with 'composer require --dev wpdesk/phpstan-rules'."
        exit 125
      fi
  artifacts:
    paths:
      - phpstan-codequality.json
      - phpstan-junit.json
    expire_in: 1 week
    reports:
      codequality: phpstan-codequality.json
      junit: phpstan-junit.xml
  rules:
    - !reference [.skip-mr, rules]
    - if: $CI_PROJECT_NAMESPACE =~ /predators/
      allow_failure: true
    - exists:
      - phpstan.neon.dist

phpcs:
  extends: .static-analysis
  script:
    - vendor/bin/phpcs --warning-severity=0 --report-junit=phpcs-junit.xml --report-full
  rules:
    - !reference [.skip-mr, rules]
    - if: $CI_PROJECT_NAMESPACE =~ /predators/
      allow_failure: true
    - exists:
      - phpcs.xml.dist
  artifacts:
    paths:
      - phpcs-junit.xml
    expire_in: 1 week
    reports:
      junit: phpcs-junit.xml
