Skip to content

Commit 989cef4

Browse files
authored
A GitHub action to apply PHPCS/PHPCBF fixes (#1)
- This GitHub Action automates PHPCS formatting across your project, ensuring consistent code styling by creating a new branch for review when necessary. It simplifies integrating PHPCS/PHPCBF into your workflow. - A warning appears in the GitHub Actions Annotations section if changes occur and therefore a new branch is created. - A proposed manual changed linked from the warning in the GitHub Actions Annotations section. - The input parameter `commit-changes: true` to commit to the current branch. - **Customizable Commit Message**: Introduced an input `commit-message` to allow users to specify a custom commit message. - **Branch Name Output**: The `branch-name` is provided as an output for downstream workflows. - Notice about a successful commit. - **Checkout code**: Fetches the latest changes so that modifications from a previous job are included, enabling seamless job chaining.
1 parent cf17db2 commit 989cef4

File tree

9 files changed

+344
-2
lines changed

9 files changed

+344
-2
lines changed

.github/dependabot.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
# Set update schedule for GitHub Actions
3+
4+
version: 2
5+
updates:
6+
# See documentation for possible values
7+
- package-ecosystem: "github-actions"
8+
# Location of package manifests
9+
directory: "/"
10+
schedule:
11+
# Check for updates to GitHub Actions every month
12+
interval: "monthly"

.github/linters/.markdown-lint.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
###########################
3+
###########################
4+
## Markdown Linter rules ##
5+
###########################
6+
###########################
7+
8+
# Linter rules doc:
9+
# - https://github.com/DavidAnson/markdownlint
10+
#
11+
# Note:
12+
# To comment out a single error:
13+
# <!-- markdownlint-disable -->
14+
# any violations you want
15+
# <!-- markdownlint-restore -->
16+
#
17+
18+
############################
19+
# Super-linter Rules by id #
20+
############################
21+
MD004: false # Unordered list style
22+
MD007:
23+
indent: 2 # Unordered list indentation
24+
MD013:
25+
line_length: 808 # Line length 80 is far too short
26+
MD026:
27+
punctuation: ".,;:!。,;:" # List of not allowed
28+
MD029: false # Ordered list item prefix
29+
MD033: false # Allow inline HTML
30+
MD036: false # Emphasis used instead of a heading
31+
32+
##############################
33+
# Super-linter Rules by tags #
34+
##############################
35+
blank_lines: false # Error on blank lines
36+
37+
########################
38+
# Seablast Rules by id #
39+
########################
40+
# Multiple heading with the same content (in CHANGELOG.md)
41+
MD024: false # No-duplicate-heading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<ruleset name="super-linter">
4+
<description>The default coding standard for usage with Super-Linter. It just includes PSR12.</description>
5+
<rule ref="PSR12" />
6+
</ruleset>

.github/workflows/phpcs-phpcbf.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
name: Phpcs-fix
3+
on: [pull_request, push]
4+
5+
permissions:
6+
contents: write
7+
8+
jobs:
9+
phpcs-phpcbf:
10+
runs-on: ubuntu-latest
11+
# Limit the running time
12+
timeout-minutes: 10
13+
steps:
14+
- name: Invoke the PHPCS check and PHPCBF fix
15+
# Use the latest commit in the main branch.
16+
uses: WorkOfStan/phpcs-fix@main
17+
with:
18+
commit-changes: true
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
name: Polish the code
3+
4+
on:
5+
push:
6+
branches-ignore:
7+
# notest branches to ignore testing of partial online commits
8+
- "notest/**"
9+
10+
pull_request:
11+
branches-ignore:
12+
# notest branches to ignore testing of partial online commits
13+
- "notest/**"
14+
15+
permissions:
16+
# only prettier-fix needs write permission, for others read is enough
17+
contents: read
18+
19+
jobs:
20+
prettier-fix:
21+
# Note: runs-on doesn't accept all expressions, so a string is used
22+
runs-on: "ubuntu-24.04"
23+
permissions:
24+
contents: write
25+
# Limit the running time
26+
timeout-minutes: 10
27+
steps:
28+
- name: Invoke the Prettier fix
29+
uses: WorkOfStan/[email protected]
30+
with:
31+
commit-changes: true
32+
33+
super-linter:
34+
needs: prettier-fix
35+
uses: WorkOfStan/seablast-actions/.github/workflows/[email protected]
36+
with:
37+
runs-on: "ubuntu-24.04"

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#disable dependency files
2+
/composer.lock
3+
4+
#disable IDE files
5+
/nbproject/
6+
/.idea/

CHANGELOG.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
### `Added` for new features
11+
12+
### `Changed` for changes in existing functionality
13+
14+
### `Deprecated` for soon-to-be removed features
15+
16+
### `Removed` for now removed features
17+
18+
### `Fixed` for any bugfixes
19+
20+
### `Security` in case of vulnerabilities
21+
22+
## [1.0.0] - 2025-01-18
23+
24+
- This GitHub Action automates PHPCS formatting across your project, ensuring consistent code styling by creating a new branch for review when necessary. It simplifies integrating PHPCS/PHPCBF into your workflow.
25+
- A warning appears in the GitHub Actions Annotations section if changes occur and therefore a new branch is created.
26+
- A proposed manual changed linked from the warning in the GitHub Actions Annotations section.
27+
- The input parameter `commit-changes: true` to commit to the current branch.
28+
- **Customizable Commit Message**: Introduced an input `commit-message` to allow users to specify a custom commit message.
29+
- **Branch Name Output**: The `branch-name` is provided as an output for downstream workflows.
30+
- Notice about a successful commit.
31+
- **Checkout code**: Fetches the latest changes so that modifications from a previous job are included, enabling seamless job chaining.
32+
33+
[Unreleased]: https://github.com/WorkOfStan/phpcs-fix/compare/v1.0.0...HEAD?w=1
34+
[1.0.0]: https://github.com/WorkOfStan/phpcs-fix/releases/tag/v1.0.0

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,45 @@
1-
# phpcs-fix
2-
Automatically apply PHP coding standard formatting and create a pull request to fix code style issues, ensuring compatibility with Super-Linter.
1+
# PHPCS-Fix
2+
3+
**PHP Code Beautifier and Fixer** is a GitHub Action that checks your PHP code for style issues using the supported [PHPCSStandards/PHP_CodeSniffer](https://github.com/PHPCSStandards/PHP_CodeSniffer/) and automatically attempts to fix them using phpcbf ensuring compatibility with [Super-Linter](https://github.com/super-linter/super-linter).
4+
(Note that the original [squizlabs/PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) was abandoned.)
5+
This action can either commit changes directly to the current branch or create a new branch if you prefer manual review via pull request.
6+
7+
## Features
8+
9+
- **Automatic Fixes:** Check PHP files for coding standard violations and attempts to automatically fix them when possible.
10+
- **Flexible Committing:** Creates a new branch (prefixed with `phpcbf/fix`) if changes are required, making it easy to review and merge. Or you can set the input parameter `commit-changes: true` to commit to the current branch. (Recommendation: start with committing to another branch and review in order to avoid surprises. Then commit to the same branch for incremental changes.)
11+
- **Configurable:** Adjust extensions, ignore and standard parameters of phpcs. Adjust PHP version, or commit-message.
12+
13+
## Usage
14+
15+
This action is designed to be used as a _reusable workflow_. You can call it from another workflow in your repository or simply add [the provided YAML configuration](.github/workflows/phpcs-phpcbf.yml) to your repository.
16+
17+
By default, if needed, a new branch with a name starting with `prettier/fix` will be created, making it easy to review and merge the fixes into your main branch.
18+
19+
Note: This action is not blocking, so the status remains green even if changes are proposed in the form of a new branch. Then it’s up to you to either create a pull request to merge the changes or delete the branch.
20+
21+
### Permissions
22+
23+
This action requires the following permissions:
24+
25+
```yaml
26+
permissions:
27+
contents: write
28+
```
29+
30+
### Inputs (all optional)
31+
32+
| Input | Description | Type | Default |
33+
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
34+
| `commit-changes` | If set to `true`, the action will commit changes to the current branch; otherwise a new branch is created for manual review. | Boolean | `false` |
35+
| `commit-message` | Commit message to use if the action commits changes. | String | `"PHP Code Beautifier fixes applied automatically"` |
36+
| `extensions` | Comma-delimited list of file extensions to be sniffed. Note: an empty value will disable checking. | String | `"php"` (defaults to PHP only; other file types must be specified) |
37+
| `ignore` | Ignore files based on a comma-separated list of patterns matching files and/or directories. | String | `vendor/` |
38+
| `php-version` | The PHP version to use, e.g. `"8.2"`. | String | `"8.2"` |
39+
| `standard` | The name of, or the path to, the coding standard to use. Can be a comma-separated list specifying multiple standards. | String | The project's `.github/linters/phpcs.xml` (where Super-linter expects it) with fallback to <https://github.com/super-linter/super-linter/blob/main/TEMPLATES/phpcs.xml> copied to [.github/linters/super-linter-templates-phpcs.xml](.github/linters/super-linter-templates-phpcs.xml) will be used. |
40+
41+
### Outputs
42+
43+
| Output | Description |
44+
| ------------- | ----------------------------------- |
45+
| `branch-name` | The name of the branch created/used |

action.yml

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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

Comments
 (0)