oceanmoon / coding-standard
The Jumbo Coding Standard for PHP_CodeSniffer: a PERCS31 ruleset for PER Coding Style 3.1, and a Jumbo ruleset that adds lowerCamelCase naming and modern PHP 8.4+ rules.
Package info
github.com/mossy2100/PHP-Coding-Standard
Type:phpcodesniffer-standard
pkg:composer/oceanmoon/coding-standard
Requires
- php: ^8.4
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- slevomat/coding-standard: ^8.25
- squizlabs/php_codesniffer: ^4.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A PHP_CodeSniffer coding standard for PHP 8.4, developed by Shaun Moss.
Description
The Jumbo Coding Standard has two rulesets, each usable as a PHP_CodeSniffer standard:
- PERCS31: PER Coding Style 3.1, and nothing more, built on PSR-12. No complete PHP_CodeSniffer standard for PER Coding Style existed. See PERCS31.
- Jumbo: PERCS31 plus extra rules for consistent naming and modern PHP 8.4+ code. See Jumbo.
Use PERCS31 to follow PER Coding Style 3.1 exactly, or Jumbo to add the extra rules. Usage shows how to set either.
Key features of Jumbo
- Follows PER Coding Style 3.1, through PERCS31.
- Enforces
$camelCasenaming for variables, parameters and properties. - Enforces consistent array formatting: single-line if it fits, otherwise one element per line with a trailing comma. No alignment of arrows or assignments, to keep diffs small.
- Enforces correct indentation for property hooks.
Why the name "Jumbo"?
- Refers to a famous elephant, the elephant being PHP's spirit animal.
- It sounds good.
- Pairs nicely with
Dumbo, another famous elephant.Dumbowill be theJumbovariant for Drupal.
Development and Quality Assurance
This package was developed with Claude Code:
- The author developed the initial concept, designed the rules, and did most of the initial coding.
- Claude Code wrote the tests and most of the documentation.
- Claude Code also assisted with multiple code reviews, which led to bug fixes, performance improvements and other refinements.
The code is checked with:
- PHP_CodeSniffer, for the Jumbo coding standard
- PHPStan, at level 9
- PHPUnit, with a unit test for each custom sniff
Requirements
- PHP ^8.4
- squizlabs/php_codesniffer ^4.0
- slevomat/coding-standard ^8.31.3
- phpcsstandards/phpcsextra ^1.5
- phpcsstandards/phpcsutils ^1.2
- php-collective/code-sniffer ^0.6.10
- dealerdirect/phpcodesniffer-composer-installer ^1.0
Installation
composer require --dev mossy2100/coding-standard
The standard is automatically registered with PHP_CodeSniffer via the dealerdirect/phpcodesniffer-composer-installer
plugin.
Usage
Create a phpcs.xml file in your project root:
<?xml version="1.0"?> <ruleset name="My Project"> <description>Coding standard for my project</description> <file>src</file> <file>tests</file> <rule ref="Jumbo"/> </ruleset>
Then run:
vendor/bin/phpcs # Check for issues vendor/bin/phpcbf # Auto-fix issues
To follow PER Coding Style 3.1 without the extra rules, use <rule ref="PERCS31"/> instead.
The default maximum line length is 120. To change it, see Line Length.
Included Sniffs
Each standard has its own page, listing the sniffs it uses, inherited and custom, with the reason for each. PSR-12's own sniffs are listed only where PERCS31 changes or replaces them.
- PERCS31: PSR-12, sniffs from Generic, PEAR, Squiz, Slevomat, PHPCSExtra (
Universal) and PhpCollective, and custom sniffs, for PER Coding Style 3.1. - Jumbo: what Jumbo changes in PERCS31, and the Generic, Squiz, PHPCSExtra, Slevomat and Zend sniffs and custom sniffs it adds as extra rules.
- PER-CS 3.1 Judgement Calls: how PERCS31 reads the points PER Coding Style 3.1 leaves unclear, contradicts or leaves out, and why.
Variable and Property Names
Variables, parameters and properties must use $camelCase, with no leading underscore. See Variable and Property Names
for which sniff checks each kind of name, and why the standard uses camelCase.
Yoda-style Comparisons
A Yoda condition puts the constant on the left of a comparison (true === $x instead of $x === true), so an
accidental = when == was meant becomes a parse error instead of a silent, always-truthy assignment.
Opinion on Yoda conditions is divided:
- Symfony requires them.
- WordPress required them, but is phasing them out in favour of tooling that catches accidental assignments.
- Drupal disallows them, for left-to-right readability. Its
drupal/coderruleset has aDisallowYodaConditionssniff. - Laravel doesn't use them, and Laravel Pint rewrites them to the usual order.
- Slevomat, which this standard borrows from, disallows them with
DisallowYodaComparison(not included here).
So neither standard checks operand order. Write comparisons in whichever order reads best to you.
Jumbo includes Generic.CodeAnalysis.AssignmentInCondition, which flags assignments in if, elseif, while, for,
switch, case and match conditions. That solves the problem Yoda conditions were meant to solve, without dictating
operand order. PERCS31 doesn't include it, as PER has no such rule. IDEs such as PhpStorm, and VS Code with
Intelephense, flag these assignments too.
License
MIT License - see LICENSE for details.
Support
- Issues: https://github.com/mossy2100/PHP-Coding-Standard/issues
- Examples: The
phpcs.xmlin Mossy2100 Core, and in the other Mossy2100 packages
Changelog
See CHANGELOG.md for version history and changes.
