mayflower/mo4-coding-standard
PHP_CodeSniffer ruleset implementing the MO4 coding standard. Extends Symfony’s standard with extra sniffs for array formatting and alignment, multiline arrays, property docblock @var rules, and lexicographically sorted use statements (configurable ordering).
composer require --dev mayflower/mo4-coding-standard
./vendor/bin/phpcs --standard=MO4 src/
./vendor/bin/phpcs --config-set default_standard MO4
./vendor/bin/phpcs --standard=MO4 app/Http/Controllers/UserController.php
./vendor/bin/phpcbf --standard=MO4 src/
MO4/ruleset.xml – Lists all enabled/disabled rules with comments..phpcs.xml or phpcs.xml.dist (see Symfony PHPCS docs).Pre-commit Hook:
Use phpcs in a Git hook to enforce standards before commits:
./vendor/bin/phpcs --standard=MO4 --warning-severity=5 src/ || git status --porcelain
(Exit code 1 if violations exist, 0 if clean.)
CI Pipeline:
Add to Laravel’s .github/workflows/php.yml:
- name: Run PHPCS
run: ./vendor/bin/phpcs --standard=MO4 --warning-severity=5 --error-severity=5 src/
IDE Integration:
phpcs in Settings > Languages & Frameworks > PHP > Code Sniffer.Use Statement Sorting:
Configure alphabetical order in .phpcs.xml:
<rule ref="MO4.Formatting.AlphabeticalUseStatements">
<properties>
<property name="order" value="string-locale"/>
</properties>
</rule>
(Default: dictionary; options: string, string-locale, string-case-insensitive.)
Array Formatting:
Auto-fix alignment with phpcbf:
./vendor/bin/phpcbf --standard=MO4 --ruleset=MO4/ruleset.xml src/
(Targets MO4.Arrays.ArrayDoubleArrowAlignment and MO4.Arrays.MultiLineArray.)
Property Documentation:
Enforce @var in multiline docblocks:
/**
* @var string
*/
private $name;
app/Providers/ to standardize:
./vendor/bin/phpcs --standard=MO4 app/Providers/
.phpcs.xml):
<exclude-pattern>database/migrations/*.php</exclude-pattern>
PHP Version Mismatch:
PHPCS 4.0+ required (since v11.0.0).composer require --dev phpcs/phpcs:^4.0
Auto-fix Limitations:
phpcbf may fail on complex arrays or nested structures. Manually adjust or exclude problematic files:
<exclude-pattern>app/ComplexArrayHandler.php</exclude-pattern>
Use Statement Conflicts:
use statements may not sort correctly (fixed in v11.0.2).<rule ref="MO4.Formatting.AlphabeticalUseStatements">
<severity>0</severity>
</rule>
Variable Interpolation:
VariableInDoubleQuotedString may flag array/property access (e.g., $obj->prop).<rule ref="MO4.Strings.VariableInDoubleQuotedString">
<properties>
<property name="ignorePropertyAccess" value="true"/>
</properties>
</rule>
-v to debug rule application:
./vendor/bin/phpcs -v --standard=MO4 app/Http/Controllers/
./vendor/bin/phpcs --standard=MO4 --report=summary app/Http/Controllers/UserController.php
Custom Ruleset:
Extend MO4/ruleset.xml to disable/enable rules:
<config name="installed_paths" value="vendor/mayflower/mo4-coding-standard/MO4"/>
<rule ref="MO4.Arrays.ArrayDoubleArrowAlignment">
<severity>5</severity> <!-- Treat as error -->
</rule>
PHPCS Configuration:
Override defaults in .phpcs.xml:
<arg name="report" value="full" />
<arg name="colors" value="false" />
Excluding Files: Use patterns to skip tests or generated files:
<exclude-pattern>tests/*</exclude-pattern>
<exclude-pattern>storage/*</exclude-pattern>
--cache to speed up repeated runs:
./vendor/bin/phpcs --standard=MO4 --cache=~/.phpcs_cache src/
parallel for large codebases (requires PHPCS 4.0+):
./vendor/bin/phpcs --standard=MO4 --parallel=4 src/
Facade Usage:
MO4 enforces use statements for imported classes (e.g., use Illuminate\Support\Facades\Log;). Avoid:
// ❌ Violates MO4.Formatting.UnnecessaryNamespaceUsage
Log::info('test');
// ✅ Correct
use Illuminate\Support\Facades\Log;
Log::info('test');
Dynamic Classes:
Exclude dynamically generated classes (e.g., from make:model):
<exclude-pattern>app/Models/Generated*.php</exclude-pattern>
How can I help you explore Laravel packages today?