name: CI - PHPCS description: >- WordPress Coding Standards (Gitea port of the phpcs job). On pull_request with autofix enabled, runs phpcbf and pushes fixes back to the PR branch; the caller must check out the PR head ref with a token that can push. inputs: autofix: required: false default: 'true' runs: using: composite steps: - name: Use preinstalled PHP 8.1 (peptide-php image) id: pre shell: bash run: | if command -v php8.1 >/dev/null 2>&1 && command -v composer >/dev/null 2>&1; then update-alternatives --set php /usr/bin/php8.1 >/dev/null 2>&1 || ln -sf /usr/bin/php8.1 /usr/local/bin/php php -v | head -1; echo "hit=true" >> "$GITHUB_OUTPUT" else echo "hit=false" >> "$GITHUB_OUTPUT" fi - name: Setup PHP (fallback when not preinstalled) if: ${{ steps.pre.outputs.hit != 'true' }} uses: shivammathur/setup-php@v2 with: php-version: '8.1' coverage: none tools: composer:v2, phpcs - name: Install WordPress Coding Standards shell: bash run: | composer global config --no-plugins \ allow-plugins.dealerdirect/phpcodesniffer-composer-installer true composer global require --no-interaction \ "dealerdirect/phpcodesniffer-composer-installer:^1.0" \ "wp-coding-standards/wpcs:^3.0" \ "phpcsstandards/phpcsutils:^1.0" \ "phpcsstandards/phpcsextra:^1.0" # Without setup-php's `tools: phpcs`, phpcs/phpcbf come from the global # composer bin dir (WPCS pulls in squizlabs/php_codesniffer). composer global config bin-dir --absolute 2>/dev/null | tail -1 >> "$GITHUB_PATH" - name: Auto-fix with phpcbf (PR only, commit back if changed) if: ${{ github.event_name == 'pull_request' && inputs.autofix == 'true' }} shell: bash run: | PHPCS_CONFIG="" if [ -f phpcs.xml ]; then PHPCS_CONFIG="phpcs.xml" elif [ -f phpcs.xml.dist ]; then PHPCS_CONFIG="phpcs.xml.dist" elif [ -f .phpcs.xml.dist ]; then PHPCS_CONFIG=".phpcs.xml.dist" fi if [ -z "$PHPCS_CONFIG" ]; then echo "No phpcs config - phpcbf skipped."; exit 0; fi phpcbf --standard="$PHPCS_CONFIG" --extensions=php \ --ignore=vendor/*,tests/*,node_modules/* . || true if ! git diff --quiet; then git config user.name "peptiderepo-ops" git config user.email "ops@peptiderepo.com" git add -A git commit -m "style: auto-fix PHPCS violations via phpcbf" git push fi - name: Run PHPCS strict (fail on remaining violations) shell: bash run: | PHPCS_CONFIG="" if [ -f phpcs.xml ]; then PHPCS_CONFIG="phpcs.xml" elif [ -f phpcs.xml.dist ]; then PHPCS_CONFIG="phpcs.xml.dist" elif [ -f .phpcs.xml.dist ]; then PHPCS_CONFIG=".phpcs.xml.dist" fi if [ -z "$PHPCS_CONFIG" ]; then echo "No phpcs.xml / phpcs.xml.dist / .phpcs.xml.dist found - PHPCS skipped." exit 0 fi phpcs --standard="$PHPCS_CONFIG" --extensions=php \ --ignore=vendor/*,tests/*,node_modules/* \ --warning-severity=0 --report=full .