mayflower/mo4-coding-standard
PHP_CodeSniffer ruleset for the MO4 coding standard. Extends Symfony’s standard with extra sniffs for array formatting (alignment, multiline rules), property docblocks (@var), and lexicographically sorted use statements (configurable ordering).
composer require --dev mayflower/mo4-coding-standard
./vendor/bin/phpcs --standard=MO4 src/YourFile.php
./vendor/bin/phpcbf --standard=MO4 src/
Run PHPCS on a file with unsorted use statements:
./vendor/bin/phpcs --standard=MO4 app/Http/Controllers/
Fix automatically:
./vendor/bin/phpcbf --standard=MO4 app/Http/Controllers/
./vendor/bin/phpcs --standard=MO4 app/
~/.phpcs or project config):
./vendor/bin/phpcs --config-set default_standard MO4
./vendor/bin/phpcs -i
Pre-commit Hooks
Use phpcs in a pre-commit hook to block violations:
# .git/hooks/pre-commit
#!/bin/sh
./vendor/bin/phpcs --standard=MO4 --warning-severity=3 --errors-on-warning $1
CI/CD Pipeline Add to Laravel’s GitHub Actions or GitLab CI:
# .github/workflows/lint.yml
- name: PHP CodeSniffer
run: ./vendor/bin/phpcs --standard=MO4 --warning-severity=3 --errors-on-warning .
IDE Integration Configure PHPStorm to use MO4:
MO4./vendor/bin/phpcsAlphabetical Use Statements:
Configure sorting in ruleset.xml:
<rule ref="MO4.Formatting.AlphabeticalUseStatements">
<properties>
<property name="order" value="string-locale"/>
</properties>
</rule>
Auto-fix Arrays:
Use phpcbf for MO4.Arrays.ArrayDoubleArrowAlignment and MO4.Arrays.MultiLineArray.
Property Documentation:
Enforce multiline @var with:
/**
* @var string
*/
private $name;
app/Providers/ to enforce consistent use statements and array formatting.database/migrations/ to catch array alignment issues in factory methods.tests/ to standardize test assertions and data providers.PHP 8.1+ Compatibility:
Auto-fix Limitations:
MO4.Strings.VariableInDoubleQuotedString cannot auto-fix complex cases (e.g., $obj->prop).phpcbf cautiously; review changes for edge cases.Use Statement Sorting Quirks:
string-case-insensitive) may group namespaces unexpectedly.Property Access in Strings:
MO4.Strings.VariableInDoubleQuotedString fails on:
echo "{$user->name}"; // Fails (property access)
echo "{$arr['key']}"; // Fails (array access)
Symfony CS Conflicts:
ruleset.xml if needed:
<rule ref="Symfony">
<exclude name="Classes.ClassDocComment.Missing"/>
</rule>
Verbose Output:
Use --verbose to debug rule application:
./vendor/bin/phpcs --standard=MO4 --verbose app/Models/User.php
Ignore Specific Rules:
Disable a rule in ruleset.xml:
<rule ref="MO4.Arrays.MultiLineArray">
<exclude name="app/Helpers/legacy.php"/>
</rule>
Custom Ruleset:
Extend MO4 in phpcs.xml:
<config name="installed_paths" value="./vendor/mayflower/mo4-coding-standard"/>
<config name="standard" value="MO4"/>
<rule ref="MO4">
<exclude name="MO4.WhiteSpace.MultipleEmptyLines"/>
</rule>
Custom Sniffs:
Add project-specific rules by extending the ruleset.xml:
<rule ref="MO4">
<config name="custom_sniffs_dir" value="./sniffs"/>
</rule>
PHPCS Configuration:
Override default PHPCS behavior in phpcs.xml:
<config name="encoding" value="utf-8"/>
<config name="tab_width" value="4"/>
Auto-fix Scripts:
Create a custom script (bin/fix-mo4) to run phpcbf with MO4:
#!/bin/bash
./vendor/bin/phpcbf --standard=MO4 --diff --report=full .
vendor/, node_modules/, etc.:
./vendor/bin/phpcs --standard=MO4 --exclude=vendor,node_modules app/
parallel-lint or pct for large codebases:
composer require --dev dealerdirect/php-pct
./vendor/bin/pct --standard=MO4
How can I help you explore Laravel packages today?