sandfox.dev/code-standard
Shared coding standards and tooling used across sandfox.dev PHP/Laravel packages. Provides a consistent rule set for formatting and static analysis to keep code style uniform, reduce review friction, and maintain quality across projects.
Install the Package Require the package in your Laravel project via Composer:
composer require --dev sandfox.dev/code-standard
Ensure it’s listed under require-dev in composer.json.
Configure PHP_CodeSniffer
Create or update .phpcs.xml in your project root:
<?xml version="1.0"?>
<ruleset name="Project Standard">
<config name="installed_paths" value="./vendor/sandfox.dev/code-standard"/>
<rule ref="SandfoxDev/CodeStandard"/>
</ruleset>
First Execution Run the linter on your Laravel codebase:
./vendor/bin/phpcs --standard=SandfoxDev/CodeStandard app/
For Blade templates, add the --extensions=blade flag.
Quick CI Integration
Add a GitHub Actions step (.github/workflows/phpcs.yml):
- name: PHP_CodeSniffer
run: ./vendor/bin/phpcs --standard=SandfoxDev/CodeStandard --warning-severity=3 app/
Pre-Commit Hook
Use husky to auto-run checks before commits:
npm install husky --save-dev
npx husky add .husky/pre-commit "phpcs --standard=SandfoxDev/CodeStandard --report=json app/ | jq -e '.files | length == 0'"
IDE Integration
Settings > Editor > Inspections > PHP > Code Sniffer and set the standard to SandfoxDev/CodeStandard.settings.json:
"intelephense.codeSnifferStandard": "SandfoxDev/CodeStandard"
Laravel-Specific Patterns
routes/ from strict checks if the standard doesn’t support Blade syntax:
<file>app/</file>
<exclude-pattern>routes/</exclude-pattern>
snake_case for migration files:
<rule ref="SandfoxDev/CodeStandard.NamingConventions">
<arg name="case" value="snake_case"/>
</rule>
Fixing Violations
Use php-cs-fixer to auto-fix common issues:
composer require --dev friendsofphp/php-cs-fixer
./vendor/bin/php-cs-fixer fix --rules=@SandfoxDev/CodeStandard --dry-run
Blade Template Support
<exclude-pattern>*.blade.php</exclude-pattern>
Laravel Facade Conflicts
NoUnusedImports may flag Laravel facades (e.g., use Illuminate\Support\Facades\Log;). Fix:
<rule ref="Generic.Files.UnusedUses">
<exclude name="Illuminate\Support\Facades.*"/>
</rule>
Performance in Large Codebases
phpcs on app/ can be slow. Tip: Parallelize with --parallel or cache results:
./vendor/bin/phpcs --standard=SandfoxDev/CodeStandard --parallel=4 app/
Rule Overrides
.phpcs.xml may break updates. Best Practice: Document overrides and pin the package version:
"sandfox.dev/code-standard": "1.0.0" // Pin to a stable version
--verbose to debug rule application:
./vendor/bin/phpcs --standard=SandfoxDev/CodeStandard --verbose app/
./vendor/bin/phpcs --standard=SandfoxDev/CodeStandard --rules=SandfoxDev/CodeStandard.NamingConventions app/
./vendor/bin/phpcs --standard=SandfoxDev/CodeStandard --report=html > phpcs-report.html
Custom Rules Extend the standard by creating a child ruleset:
<ruleset>
<rule ref="SandfoxDev/CodeStandard"/>
<rule ref="Custom.Namespace">
<properties>
<property name="prefix" value="App\Rules"/>
</properties>
</rule>
</ruleset>
Laravel-Specific Additions
Add Laravel-aware rules (e.g., for Route::prefix usage):
<rule ref="Custom.Laravel.RoutePrefix">
<properties>
<property name="allowedPrefixes" value="admin,api"/>
</properties>
</rule>
CI-Specific Configurations Use environment variables to toggle strictness in CI:
- name: PHP_CodeSniffer (Strict)
if: github.ref == 'refs/heads/main'
run: ./vendor/bin/phpcs --standard=SandfoxDev/CodeStandard --warning-severity=5 app/
How can I help you explore Laravel packages today?