Introduce PHP_CodeSniffer ruleset based on PSR12 #204
Merged
antecedent merged 2 commits intomasterfrom Mar 30, 2026
Merged
Conversation
Follow-up on PRs 200, 201, 202 and 203. This commit introduces [PHP_CodeSniffer](https://github.com/PHPCSStandards/PHP_CodeSniffer) (PHPCS) as the tooling to enforce code style consistency for the Patchwork codebase. Notes: * As discussed in 180, the ruleset is based on [PSR-12](https://www.php-fig.org/psr/psr-12/). At a later point in time, we may want to update that to PERCS (once available), but that's not that relevant for this codebase as the rule additions in PERCS are mostly related to new PHP syntax, which can't be used in this codebase at this time anyway due to the PHP 7.1 minimum. * Additionally, the [PHPCompatibility](https://github.com/PHPCompatibility/PHPCompatibility) package, an external ruleset for PHPCS, has been added at the 10.0.0-alpha2 release to detect PHP cross-version compatibility issues. While this is an alpha release, I'd still recommend using it over the last stable release as a) it's pretty damn stable anyway and b) it has much much more detection of issues than any previous release. * For PHPCS to recognize the external PHPCompatibility ruleset, the `dealerdirect/phpcodesniffer-composer-installer` composer plugin needs permission to run. This has been granted. * Also note that PHPCS is allowed at version 3 and 4. This is because v4 has a PHP 7.2 minimum (v3 PHP 5.4), which would otherwise impede running of our tests on PHP 7.1. Ruleset specific notes: * At this time, only the `.php` files can be checked. `.phpt` files cannot be analyzed at this time by PHPCS. An issue is open about this upstream and if that ever gets addressed, we could consider scanning the `.phpt` test files too. * The test fixtures are explicitly excluded from the scans as those may contain code written in a non-compliant way explicitly to trigger a specific test situation and we don't want to limit the test fixtures in what can be tested. * There are two sniffs, both related to file organisation, which have been explicitly excluded. Whether or not this codebase should follow the "one class per file", PSR-4 and "no side-effects" rules is a different conversation and should be discussed separately. This should not block this PR, which is why I've excluded the rules for the time being. This is related to the work previously started in 98. * Additionally, I've made some selective exclusions (= exclude a specific sniff for one specific file), where the changes PSR12 expects would lead to breaking changes otherwise. It could be considered to address these in a future major release, but making these changes at this time is incompatible with Semver. * I've also elected to make the line length rule a little more lenient. Includes: * A GitHub Actions workflow job to enforce the code style on all pushes and pull requests. When the workflow runs for a pull request, code style violations will be shown in the PR code view via inline annotations using the `cs2pr` tool. * Some simple Composer scripts to make it straight-forward for contributors to know what CI checks are being run and how to run these locally. * Excluding the PHPCS ruleset file from being included in distribution archives. * Git-ignoring typical PHPCS ruleset overload files (which can be used by devs locally to try additional rules). A
* `CallRerouting`: I've verified that the issue flagged will not occur. * `Namespaces`: I've verified that the issue flagged will not occur. * `RedefinitionOfNew`: I've elected not to address the line length issue for this code snippet.
antecedent
approved these changes
Mar 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduce PHP_CodeSniffer ruleset based on PSR12
Follow-up on PRs #200, #201, #202 and #203.
This commit introduces PHP_CodeSniffer (PHPCS) as the tooling to enforce code style consistency for the Patchwork codebase.
Notes:
At a later point in time, we may want to update that to PERCS (once available), but that's not that relevant for this codebase as the rule additions in PERCS are mostly related to new PHP syntax, which can't be used in this codebase at this time anyway due to the PHP 7.1 minimum.
While this is an alpha release, I'd still recommend using it over the last stable release as a) it's pretty damn stable anyway and b) it has much much more detection of issues than any previous release.
dealerdirect/phpcodesniffer-composer-installercomposer plugin needs permission to run. This has been granted.Ruleset specific notes:
.phpfiles can be checked..phptfiles cannot be analyzed at this time by PHPCS. An issue is open about this upstream and if that ever gets addressed, we could consider scanning the.phpttest files too.Whether or not this codebase should follow the "one class per file", PSR-4 and "no side-effects" rules is a different conversation and should be discussed separately. This should not block this PR, which is why I've excluded the rules for the time being.
This is related to the work previously started in Clean class and function files as per PSR-4 #98.
It could be considered to address these in a future major release, but making these changes at this time is incompatible with Semver.
Includes:
When the workflow runs for a pull request, code style violations will be shown in the PR code view via inline annotations using the
cs2prtool.PHPCS: add some targetted inline ignore annotations
CallRerouting: I've verified that the issue flagged will not occur.Namespaces: I've verified that the issue flagged will not occur.RedefinitionOfNew: I've elected not to address the line length issue for this code snippet.Note: I've verified that the CI job correctly fails when there are CS violations: https://github.com/antecedent/patchwork/actions/runs/23746720708/job/69177215774