llcu / wordpress-coding-standards
PHPCS standard for LLC University WordPress plugins and themes.
Package info
github.com/llcu/wordpress-coding-standards
Type:phpcodesniffer-standard
pkg:composer/llcu/wordpress-coding-standards
Requires
- php: >=8.4
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- phpcompatibility/phpcompatibility-wp: ^2.1
- phpcsstandards/phpcsutils: ^1.1
- slevomat/coding-standard: ^8.22
- squizlabs/php_codesniffer: ^3.13.5
- wp-coding-standards/wpcs: ^3.4.1
Requires (Dev)
- phpunit/phpunit: ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A PHP_CodeSniffer standard for modern WordPress plugins and themes. It starts from the WordPress Coding Standards (WPCS) and adds rules for PHP 8.4 code: PSR-4 file names, short arrays, guard clauses, native types and final classes.
This is how we write PHP at LLC University. Every one of our WordPress plugins and themes runs this standard, so all our code follows the same rules and reads the same way.
One Composer package brings PHPCS, WPCS, PHPCompatibilityWP and the Slevomat Coding Standard. Your project keeps only its own settings.
Requirements
- PHP 8.4 or later
- Composer
Installation
composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer require --dev llcu/wordpress-coding-standards
If your project already requires squizlabs/php_codesniffer, wp-coding-standards/wpcs, phpcompatibility/phpcompatibility-wp or slevomat/coding-standard, remove them from require-dev. This package installs them.
Configuration
Copy the template to the root of your plugin or theme:
cp vendor/llcu/wordpress-coding-standards/templates/phpcs.xml.dist .
Then edit these values in phpcs.xml.dist:
| Value | Replace with |
|---|---|
llcu-plugin-slug |
Your text domain |
llcu_plugin_slug |
Your prefix for functions, hooks and globals |
LLCU\PluginSlug |
Your namespace |
testVersion |
The PHP versions you support, for example 8.4- |
minimum_wp_version |
The oldest WordPress version you support |
The important line is the reference to the standard:
<rule ref="LLCU"/>
Usage
Add the scripts to your composer.json:
"scripts": { "lint": "phpcs", "lint:fix": "phpcbf" }
Then run:
composer lint # report the violations composer lint:fix # fix what can be fixed automatically
Tip
On an existing codebase, run composer lint:fix first. It fixes indentation, arrays, final and early exits. Then fix what composer lint still reports.
What the standard adds to WPCS
| Rule | Sniff |
|---|---|
PSR-4 file names (Verification.php) |
WPCS file-name checks off |
Short arrays [] |
Generic.Arrays.DisallowLongArraySyntax |
Elvis operator ?: allowed |
Universal.Operators.DisallowShortTernary off |
Guard clauses, no else |
SlevomatCodingStandard.ControlStructures.EarlyExit, LLCU.ControlStructures.DisallowElse |
| One-level ternaries | LLCU.Operators.DisallowNestedTernary |
| Shallow, simple functions | Generic.Metrics.NestingLevel (3), Generic.Metrics.CyclomaticComplexity (10) |
| Blank line before and after control structures | SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing |
final or abstract classes |
SlevomatCodingStandard.Classes.RequireAbstractOrFinal |
| Constructor promotion | SlevomatCodingStandard.Classes.RequireConstructorPropertyPromotion |
static fn closures |
SlevomatCodingStandard.Functions.StaticClosure, RequireArrowFunction |
?? and ??= |
SlevomatCodingStandard.ControlStructures.RequireNullCoalesce* |
| Native types on parameters, returns, properties and constants | SlevomatCodingStandard.TypeHints.* |
| Three parameters at most, five in a constructor | LLCU.Functions.MaxParameters |
| No unused imports; imports in order | SlevomatCodingStandard.Namespaces.UnusedUses, AlphabeticallySortedUses |
No exec(), shell_exec(), proc_open() and similar |
Generic.PHP.ForbiddenFunctions |
phpcs:ignore Exact.Sniff -- Reason. |
LLCU.Commenting.PhpcsIgnoreReason |
Explicit autoload in add_option() and update_option() |
LLCU.WP.OptionAutoload |
| PHP and WordPress compatibility | PHPCompatibilityWP |
Indentation is the same as in WPCS: tabs.
Custom sniffs
LLCU.ControlStructures.DisallowElse
Reports every else and elseif. Use a guard clause instead. The alternative syntax (else :) in templates is allowed.
// Error. if ( $user ) { return $user->ID; } else { return 0; } // OK. if ( ! $user ) { return 0; } return $user->ID;
LLCU.Operators.DisallowNestedTernary
Reports a ternary inside another ternary: in a branch, in the condition or in a match arm. A ternary inside a closure or arrow function belongs to that function. Chained Elvis operators ($a ?: $b ?: $c) are allowed.
LLCU.Functions.MaxParameters
Allows three parameters for functions, methods and closures, and five for constructors. Methods with the #[\Override] attribute are skipped, because the parent sets their signature.
You can change the limits in your phpcs.xml.dist:
<rule ref="LLCU.Functions.MaxParameters"> <properties> <property name="max_parameters" value="4"/> <property name="max_constructor_parameters" value="6"/> </properties> </rule>
LLCU.Commenting.PhpcsIgnoreReason
Every phpcs:ignore and phpcs:disable must name its sniffs and give a reason after --:
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Custom table, no API for it.
PHPCS silences a line that holds only an annotation, so the error shows on the next line of code. A bare annotation, with no sniff codes, also silences that line, so its error shows on line 1. Each message names the line of the annotation.
LLCU.WP.OptionAutoload
add_option() and update_option() must pass the autoload flag, so each option makes an explicit choice.
Turning off a rule
Exclude any sniff in your phpcs.xml.dist:
<rule ref="LLCU"> <exclude name="LLCU.ControlStructures.DisallowElse"/> </rule>
Versioning
The package follows Semantic Versioning. A new or stricter rule is a minor version. Removing a rule or renaming the standard is a major version. Your composer.lock pins the version, so a stricter rule reaches your project only when you run composer update.
Contributing
composer install composer test # PHPUnit: each sniff against its fixture and a full plugin in house style composer lint # the package lints its own sniffs with the LLCU standard
- A sniff lives in
LLCU/Sniffs/<Category>/<Name>Sniff.phpand is referenced inLLCU/ruleset.xml. - Its fixture is
tests/Fixtures/<Name>.inc. Mark each line that must report an error with// error. When that comment would change the line itself (an annotation), put// error-belowon the line above. tests/Fixtures/Pluginis a small plugin that follows the standard. The full standard must report nothing on it, so a new rule that contradicts another one fails the suite.