|
| 1 | +--- |
| 2 | +name: "PHP Code Beautifier and Fixer" |
| 3 | +description: Sniff the PHP code and beautify it if necessary. |
| 4 | + |
| 5 | +author: |
| 6 | + name: "Stanislav Rejthar" |
| 7 | + |
| 8 | + url: "https://github.com/WorkOfStan" |
| 9 | + |
| 10 | +branding: |
| 11 | + icon: "check-circle" |
| 12 | + color: "blue" |
| 13 | + |
| 14 | +inputs: |
| 15 | + commit-changes: |
| 16 | + description: "If true, commit changes to the current branch" |
| 17 | + type: boolean |
| 18 | + default: false |
| 19 | + commit-message: |
| 20 | + description: "Commit message for the changes" |
| 21 | + type: string |
| 22 | + default: "PHP Code Beautifier fixes applied automatically" |
| 23 | + # https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Advanced-Usage#specifying-valid-file-extensions |
| 24 | + # By default, PHP_CodeSniffer will check any file it finds with a .inc, .php, .js or .css extension, although not all standards will actually check all these file types. |
| 25 | + # Empty value means no checking at all. |
| 26 | + # The default is `php` as js and css are handled by prettier. |
| 27 | + # Btw: JS/CSS support will be removed from PHP_CodeSniffer in PHPCS 4.x. Source: https://phpcsutils.com/ |
| 28 | + extensions: |
| 29 | + description: "Comma delimited list of extensions to be sniffed" |
| 30 | + required: false |
| 31 | + type: string |
| 32 | + default: "php" # "php,inc,lib" |
| 33 | + ignore: |
| 34 | + description: "Ignore files based on a comma-separated list of patterns matching files and/or directories." |
| 35 | + required: false |
| 36 | + type: string |
| 37 | + default: "vendor/" |
| 38 | + php-version: |
| 39 | + description: "PHP version to be used for running phpcs and phpcbf" |
| 40 | + required: false |
| 41 | + type: string |
| 42 | + default: "8.2" |
| 43 | + standard: |
| 44 | + description: "The name of, or the path to, the coding standard to use. Can be a comma-separated list specifying multiple standards." |
| 45 | + required: false |
| 46 | + type: string |
| 47 | + default: "" # if left empty, .github/linters/phpcs.xml or https://github.com/super-linter/super-linter/blob/main/TEMPLATES/phpcs.xml will be used |
| 48 | + |
| 49 | +outputs: |
| 50 | + branch-name: |
| 51 | + description: "The branch where changes were committed" |
| 52 | + |
| 53 | +runs: |
| 54 | + using: "composite" |
| 55 | + steps: |
| 56 | + - name: Checkout code |
| 57 | + uses: actions/checkout@v4 |
| 58 | + with: |
| 59 | + ref: ${{ github.ref }} # Fetch latest changes (even by previous job) |
| 60 | + fetch-depth: 0 # Fetch all history for all branches and tags. Otherwise github.event.pull_request.base.ref SHA isn't found. |
| 61 | + |
| 62 | + # Fetch latest changes (even by previous job) |
| 63 | + - name: Fetch latest changes |
| 64 | + run: git pull origin ${{ github.ref }} |
| 65 | + shell: bash |
| 66 | + |
| 67 | + - name: Set up PHP |
| 68 | + uses: shivammathur/setup-php@v2 |
| 69 | + with: |
| 70 | + php-version: "${{ inputs.php-version }}" |
| 71 | + tools: composer:v2 |
| 72 | + |
| 73 | + # todo consider using cache |
| 74 | + - name: "Use the latest PHPCS version" |
| 75 | + run: composer require --dev squizlabs/php_codesniffer --prefer-dist --no-progress |
| 76 | + shell: bash |
| 77 | + |
| 78 | + - name: Run PHP Code Sniffer |
| 79 | + id: phpcs |
| 80 | + run: | |
| 81 | + # Determine which coding standard to use. |
| 82 | + # Priority: |
| 83 | + # 1. Use inputs.standard if provided (non-empty). |
| 84 | + # 2. If not, use the repository's own phpcs.xml if available. |
| 85 | + # 3. Otherwise, fall back to the action's default template. Which is a copy of https://github.com/super-linter/super-linter/blob/main/TEMPLATES/phpcs.xml |
| 86 | + if [ -n "${{ inputs.standard }}" ]; then |
| 87 | + use_standard="${{ inputs.standard }}" |
| 88 | + elif [ -f .github/linters/phpcs.xml ]; then |
| 89 | + use_standard=".github/linters/phpcs.xml" |
| 90 | + else |
| 91 | + use_standard="$GITHUB_ACTION_PATH/.github/linters/super-linter-templates-phpcs.xml" |
| 92 | + fi |
| 93 | + echo "USE_STANDARD=$use_standard" >> "$GITHUB_ENV" # for subsequent steps |
| 94 | + echo "phpcs standard=$use_standard" |
| 95 | + vendor/bin/phpcs --extensions="${{ inputs.extensions }}" . --standard="$use_standard" --ignore="${{ inputs.ignore }}" || HAS_ISSUES=true |
| 96 | + # because of `||`, the previous command always exit 0 |
| 97 | +
|
| 98 | + # Default the variable if no issues were found |
| 99 | + : "${HAS_ISSUES:=false}" |
| 100 | + echo "HAS_ISSUES=$HAS_ISSUES" >> "$GITHUB_ENV" |
| 101 | + shell: bash |
| 102 | + |
| 103 | + - name: Run PHP Code Beautifier if needed |
| 104 | + if: env.HAS_ISSUES == 'true' |
| 105 | + run: | |
| 106 | + echo "phpcbf standard=${{ env.USE_STANDARD }}" |
| 107 | + vendor/bin/phpcbf --extensions="${{ inputs.extensions }}" . --standard="${{ env.USE_STANDARD }}" --ignore="${{ inputs.ignore }}" || echo "::warning title=Some phpcs issues remained::Manual fix necessary. Compare to the PHP Code Sniffer section above." |
| 108 | +
|
| 109 | + # restore composer.json before staging the changes |
| 110 | + git checkout composer.json |
| 111 | +
|
| 112 | + # Check for fixable changes |
| 113 | + if [ -z "$(git status --porcelain)" ]; then |
| 114 | + echo "::warning title=No fixable errors were found by phpcbf::Manual fix necessary. Compare to the PHP Code Sniffer section above." |
| 115 | + # Exiting gracefully |
| 116 | + exit 0 |
| 117 | + # The rest of the script must still be within the same step to really stop the execution |
| 118 | + fi |
| 119 | +
|
| 120 | + git add . |
| 121 | +
|
| 122 | + # Determine branch name based on commit-changes input |
| 123 | + if ${{ inputs.commit-changes }}; then |
| 124 | + BRANCH_NAME=$(git branch --show-current) |
| 125 | + NOTICE_MESSAGE="A PHPCBF commit was successfully added to the current branch: $BRANCH_NAME" |
| 126 | + else |
| 127 | + # name includes timestamp and short_hash |
| 128 | + BRANCH_NAME="phpcbf/fix-$(date +'%y%m%d%H%M%S')-$(git rev-parse --short HEAD)" |
| 129 | + git checkout -b "$BRANCH_NAME" |
| 130 | + echo "::warning title=New branch created::Consider pull request for a new branch: $BRANCH_NAME (or delete it)" |
| 131 | + NOTICE_MESSAGE="A PHPCBF commit was successfully added to the new branch: $BRANCH_NAME" |
| 132 | + fi |
| 133 | +
|
| 134 | + # Configure Git user |
| 135 | + git config user.name "github-actions[bot]" |
| 136 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 137 | +
|
| 138 | + # Commit and push changes with custom message |
| 139 | + git commit -m "${{ inputs.commit-message }} on $(date +'%Y-%m-%d %H:%M:%S') UTC" |
| 140 | + git push origin "$BRANCH_NAME" |
| 141 | + echo "::notice title=Commit added::$NOTICE_MESSAGE" |
| 142 | +
|
| 143 | + # Set branch name as output |
| 144 | + echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT |
| 145 | + shell: bash |
0 commit comments