sca.yml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. name: Static Code Analysis
  2. on:
  3. - pull_request
  4. - push
  5. permissions:
  6. contents: read # to fetch code (actions/checkout)
  7. concurrency:
  8. group: sca-${{ github.head_ref || github.run_id }} # will be canceled on subsequent pushes in pull requests but not branches
  9. cancel-in-progress: true
  10. jobs:
  11. everything:
  12. name: Static Code Analysis
  13. runs-on: 'ubuntu-24.04'
  14. env:
  15. php-version: '8.3'
  16. steps:
  17. - name: Checkout code
  18. uses: actions/checkout@v4
  19. # We need to fetch base branch because `actions/checkout` only initializes empty repo and fetches PR's meta-branch
  20. # which leads to `fatal: ambiguous argument 'origin/...': unknown revision or path not in the working tree.`,
  21. # because of that `CHANGED_PHP_FILES` is not set, and tools based on diff between branches (like Mess Detector) don't work.
  22. - name: Fetch base branch
  23. if: ${{ github.event_name == 'pull_request' }}
  24. run: git fetch --no-tags --prune --no-recurse-submodules --depth=1 origin $GITHUB_BASE_REF
  25. - name: Setup PHP with Composer deps
  26. uses: ./.github/composite-actions/setup-php-with-composer-deps
  27. with:
  28. os: ${{ runner.os }}
  29. php: ${{ env.php-version }}
  30. ## We want to have a lock-file used on PR level, so contributors are not bothered by SCA complains unrelated to their changes,
  31. ## and same time we want to be aware that we are complying with bleeding edge of SCA tools as maintainers observing the push hook.
  32. - name: Unlock dev-tools
  33. if: ${{ github.event_name != 'pull_request' }}
  34. run: rm ./dev-tools/composer.lock
  35. - name: Cache dev-tools
  36. uses: actions/cache@v4
  37. with:
  38. path: dev-tools/bin/
  39. key: DevTools-${{ hashFiles('dev-tools/install.sh') }}
  40. - name: Install dev-tools
  41. uses: nick-invision/retry@v3
  42. with:
  43. timeout_minutes: 5
  44. max_attempts: 5
  45. retry_wait_seconds: 30
  46. command: ./dev-tools/install.sh
  47. - name: Show PHPUnit version
  48. run: vendor/bin/phpunit --version
  49. - name: Run AutoReview
  50. run: vendor/bin/paraunit run --testsuite auto-review
  51. - name: Check - file permissions
  52. run: ./dev-tools/check_file_permissions.sh
  53. - name: Check - trailing spaces
  54. run: ./dev-tools/check_trailing_spaces.sh
  55. - name: Check - Composer's autoload
  56. run: composer dump-autoload --dry-run --optimize --strict-psr
  57. - name: Check - phpstan
  58. run: ./dev-tools/vendor/bin/phpstan analyse --ansi
  59. - name: Check - composer-unused
  60. run: ./dev-tools/vendor/bin/composer-unused --no-progress --excludePackage=composer/xdebug-handler
  61. - name: Check - composer-require-checker
  62. run: ./dev-tools/vendor/bin/composer-require-checker check composer.json --config-file .composer-require-checker.json
  63. - name: Check - composer normalize
  64. run: |
  65. composer normalize --dry-run --working-dir=./dev-tools ../composer.json
  66. composer normalize --dry-run --working-dir=./dev-tools composer.json
  67. - name: Check - shell scripts
  68. run: ./dev-tools/check_shell_scripts.sh
  69. - name: Find changed files (for pull request)
  70. if: ${{ github.event_name == 'pull_request' }}
  71. run: |
  72. if git diff origin/$GITHUB_BASE_REF --name-only --diff-filter=ACMRTUXB | grep -E "\.php$"; then
  73. echo 'CHANGED_PHP_FILES<<EOF' >> $GITHUB_ENV
  74. git diff origin/$GITHUB_BASE_REF --name-only --diff-filter=ACMRTUXB | grep -E "\.php$" >> $GITHUB_ENV
  75. echo 'EOF' >> $GITHUB_ENV
  76. fi
  77. - name: Find changed files (for push)
  78. if: ${{ github.event_name != 'pull_request' }}
  79. run: |
  80. if git diff --name-only --diff-filter=ACMRTUXB HEAD~..HEAD | grep -E "\.php$"; then
  81. echo 'CHANGED_PHP_FILES<<EOF' >> $GITHUB_ENV
  82. git diff --name-only --diff-filter=ACMRTUXB HEAD~..HEAD | grep -E "\.php$" >> $GITHUB_ENV
  83. echo 'EOF' >> $GITHUB_ENV
  84. fi
  85. - name: Check - well defined array keys
  86. if: ${{ env.CHANGED_PHP_FILES }}
  87. run: |
  88. echo "Array types must explicitly declare key-type, i.e. as \`array<type-of-key, type-of-value>\`, \`list<type-of-value>\` or \`array{...}\` - instead of \`array<type-of-value>\` or \`type-of-value[]\`."
  89. echo "Hint: don't apply those rules blindly, provide array key type explicitly\!"
  90. ./php-cs-fixer check --config=dev-tools/.php-cs-fixer.well-defined-arrays.php --path-mode=intersection $CHANGED_PHP_FILES
  91. - name: Check - Mess Detector (phpmd)
  92. if: ${{ env.CHANGED_PHP_FILES }}
  93. run: |
  94. if [ '${{ github.event_name }}' == 'pull_request' ]; then
  95. ./dev-tools/vendor/bin/phpmd `echo "$CHANGED_PHP_FILES" | grep -v /Fixtures/ | xargs | sed 's/ /,/g'` github ./dev-tools/mess-detector/phpmd.xml
  96. else
  97. ./dev-tools/vendor/bin/phpmd `echo "$CHANGED_PHP_FILES" | grep -v /Fixtures/ | xargs | sed 's/ /,/g'` ansi ./dev-tools/mess-detector/phpmd.xml
  98. fi
  99. - name: Check - ensure test files are not present in the archive
  100. run: |
  101. git archive -o /dev/null HEAD -v 2>&1 | grep tests | grep \.php \
  102. && (echo "Test files detected in archive" && exit 1) || echo "No test files detected in archive"