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 -i # Check if MO4 appears in installed standards
./vendor/bin/phpcs --standard=MO4 app/Models/User.php
--diff to preview changes before fixing:
./vendor/bin/phpcbf --standard=MO4 --diff app/Models/User.php
| Command | Purpose |
|---|---|
phpcs --standard=MO4 path/ |
Run MO4 checks on a directory/file. |
phpcbf --standard=MO4 path/ |
Auto-fix MO4 violations (supports ~80% of rules). |
phpcs --config-set default_standard MO4 |
Set MO4 as default for future runs. |
composer require --dev mayflower/mo4-coding-standard
./vendor/bin/phpcs --standard=MO4 app/
- name: Run MO4 Coding Standard
run: ./vendor/bin/phpcs --standard=MO4 --warning-severity=3 app/
husky or pre-commit):
# .git/hooks/pre-commit
#!/bin/sh
./vendor/bin/phpcs --standard=MO4 --colors --report=emacs app/ || exit 1
MO4 as the default standard.settings.json:
{
"php-cs-fixer.executablePath": "./vendor/bin/phpcbf",
"php-cs-fixer.rules": "@MO4"
}
| Pattern | Example |
|---|---|
| Use Statement Sorting | Configure alphabetical order in .phpcs.xml: |
<rule ref="MO4.Formatting.AlphabeticalUseStatements">
<properties>
<property name="order" value="string-locale"/>
</properties>
</rule>
``` |
| **Array Alignment** | Auto-fix with:
```bash
./vendor/bin/phpcbf --standard=MO4 --ruleset=MO4/ruleset.xml app/
``` |
| **Variable Interpolation** | Replace `$var` with `{$var}` in strings:
```bash
./vendor/bin/phpcbf --standard=MO4 --ruleset=MO4/ruleset.xml app/
``` |
### Laravel-Specific Tips
1. **Service Provider Files**:
- Run MO4 on `app/Providers/` to enforce consistent `use` statements and array formatting.
- Example:
```bash
./vendor/bin/phpcs --standard=MO4 app/Providers/
```
2. **Migration Files**:
- Use `--ignore=*/database/migrations/*` to exclude migrations if needed:
```bash
./vendor/bin/phpcs --standard=MO4 --ignore=*/database/migrations/* app/
```
3. **Custom Rulesets**:
- Extend `MO4/ruleset.xml` to disable specific rules (e.g., `MO4.WhiteSpace.MultipleEmptyLines`):
```xml
<rule ref="MO4.WhiteSpace.MultipleEmptyLines">
<severity>0</severity>
</rule>
```
---
## Gotchas and Tips
### Common Pitfalls
| Issue | Solution |
|----------------------------------------|--------------------------------------------------------------------------|
| **`phpcs` not detecting MO4** | Run `./vendor/bin/phpcs --config-set installed_paths vendor/mayflower/mo4-coding-standard` or update `~/.phpcs.dist`:
```ini
installed_paths = vendor/mayflower/mo4-coding-standard
``` |
| **Auto-fix fails on arrays** | Use `--ruleset=MO4/ruleset.xml` explicitly:
```bash
./vendor/bin/phpcbf --standard=MO4 --ruleset=MO4/ruleset.xml app/
``` |
| **Trait `use` statements mis-sorted** | Update to `v11.0.2+` (fixes [#235](https://github.com/mayflower/mo4-coding-standard/issues/235)). |
| **Variable interpolation errors** | Ensure `{$var}` is used (not `$var`) in double-quoted strings. Auto-fix with `phpcbf`. |
| **PHP 8.1+ attributes misaligned** | MO4 enforces `SlevomatCodingStandard.Attributes.AttributesOrder`. Use:
```bash
./vendor/bin/phpcbf --standard=MO4 --ruleset=MO4/ruleset.xml app/
``` |
### Debugging Tips
1. **Verbose Output**:
```bash
./vendor/bin/phpcs --standard=MO4 --verbose app/Models/User.php
AlphabeticalUseStatements):
./vendor/bin/phpcs --standard=MO4 --rules=MO4.Formatting.AlphabeticalUseStatements app/
./vendor/bin/phpcs --standard=MO4 --ignore=*/tests/* app/
MO4/ruleset.xml in your project root to disable/enable rules.MultipleEmptyLines:
<rule ref="MO4.WhiteSpace.MultipleEmptyLines">
<severity>0</severity>
</rule>
~/.phpcs.dist:
[MO4]
encoding = utf-8
tab_width = 4
--warning-severity=3 to treat warnings as errors in CI:
./vendor/bin/phpcs --standard=MO4 --warning-severity=3 app/
phpcbf for 80% of Fixes:
./vendor/bin/phpcbf --standard=MO4 --diff app/ # Preview changes
./vendor/bin/phpcbf --standard=MO4 app/ # Apply fixes
./vendor/bin/phpcs --standard=MO4 --ignore=vendor/ app/
SlevomatCodingStandard.Attributes.* rules in new code.composer require --dev squizlabs/php_codesniffer:^4.0
How can I help you explore Laravel packages today?