Skip to content

Commit 69371f8

Browse files
committed
creation of the code and its description
1 parent 40a5351 commit 69371f8

File tree

9 files changed

+284
-9
lines changed

9 files changed

+284
-9
lines changed

.github/dependabot.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
# To get started with Dependabot version updates, you'll need to specify which
3-
# package ecosystems to update and where the package manifests are located.
4-
# Please see the documentation for all configuration options:
5-
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
2+
# Set update schedule for GitHub Actions
63

74
version: 2
85
updates:
9-
- package-ecosystem: "" # See documentation for possible values
10-
directory: "/" # Location of package manifests
6+
# See documentation for possible values
7+
- package-ecosystem: "github-actions"
8+
# Location of package manifests
9+
directory: "/"
1110
schedule:
11+
# Check for updates to GitHub Actions every month
1212
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: Prettier-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

.github/workflows/polish-the-code.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ jobs:
3232

3333
super-linter:
3434
needs: prettier-fix
35-
uses: WorkOfStan/seablast-actions/.github/workflows/linter.yml@feature/fetch-latest
35+
uses: WorkOfStan/seablast-actions/.github/workflows/linter.yml@v0.2.1
3636
with:
3737
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+
(As the original [squizlabs/PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) was abandoned.
5+
It 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. Default: `vendor/` | String |
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. If not set, the project's `.github/linters/phpcs.xml` or <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. | String |
40+
41+
### Outputs
42+
43+
| Output | Description |
44+
| -------------- | ------------------------------------------------- |
45+
| `branch-name` | The name of the branch created/used |

action.yml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
email: "rejthar@stanislavrejthar.com"
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+
66+
- name: Set up PHP
67+
uses: shivammathur/setup-php@v2
68+
with:
69+
php-version: "${{ inputs.php-version }}"
70+
tools: composer:v2
71+
72+
- name: "Use the latest PHPCS version"
73+
run: composer require --dev squizlabs/php_codesniffer --prefer-dist --no-progress
74+
75+
- name: Run PHP Code Sniffer
76+
id: phpcs
77+
run: |
78+
# Check if the repo has its own phpcs.xml config file
79+
if [ -f .github/linters/phpcs.xml ]; then
80+
standard=".github/linters/phpcs.xml"
81+
else
82+
# Fallback to the action’s default phpcs.xml template.
83+
# In a GitHub Action, GITHUB_ACTION_PATH points to the action’s source directory.
84+
standard="$GITHUB_ACTION_PATH/.github/linters/super-linter-templates-phpcs.xml"
85+
# Which is a copy of https://github.com/super-linter/super-linter/blob/main/TEMPLATES/phpcs.xml
86+
fi
87+
vendor/bin/phpcs --extensions="${{ inputs.extensions }}" . --standard="${{ inputs.standard || $standard }}" --ignore="${{ inputs.ignore }}" || HAS_ISSUES=true
88+
# because of `||`, the previous command always exit 0
89+
90+
# Default the variable if no issues were found
91+
: "${HAS_ISSUES:=false}"
92+
echo "HAS_ISSUES=$HAS_ISSUES" >> "$GITHUB_ENV"
93+
shell: bash
94+
95+
- name: Run PHP Code Beautifier if needed
96+
if: env.HAS_ISSUES == 'true'
97+
run: |
98+
vendor/bin/phpcbf --extensions="${{ inputs.extensions }}" . --standard=.github/linters/phpcs.xml --ignore=vendor/ || echo "::warning title=Some phpcs issues remained::Manual fix necessary. Compare to the PHP Code Sniffer section above."
99+
100+
# restore composer.json before staging the changes
101+
git checkout composer.json
102+
git add .
103+
104+
# Determine branch name based on commit-changes input
105+
if ${{ inputs.commit-changes }}; then
106+
BRANCH_NAME=$(git branch --show-current)
107+
NOTICE_MESSAGE="A PHPCBF commit was successfully added to the current branch: $BRANCH_NAME"
108+
else
109+
# name includes timestamp and short_hash
110+
BRANCH_NAME="phpcbf/fix-$(date +'%y%m%d%H%M%S')-$(git rev-parse --short HEAD)"
111+
git checkout -b "$BRANCH_NAME"
112+
echo "::warning title=New branch created::Consider pull request for a new branch: $BRANCH_NAME (or delete it)"
113+
NOTICE_MESSAGE="A PHPCBF commit was successfully added to the new branch: $BRANCH_NAME"
114+
fi
115+
116+
# Configure Git user
117+
git config user.name "github-actions[bot]"
118+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
119+
120+
# Commit and push changes with custom message
121+
git commit -m "${{ inputs.commit-message }} on $(date +'%Y-%m-%d %H:%M:%S') UTC"
122+
git push origin "$BRANCH_NAME"
123+
echo "::notice title=Commit added::$NOTICE_MESSAGE"
124+
125+
# Set branch name as output
126+
echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT
127+
shell: bash

0 commit comments

Comments
 (0)