While drupal/coder is primarily designed for Drupal projects, Laravel developers can leverage its PHP_CodeSniffer integration for enforcing Drupal-like coding standards in shared PHP logic (e.g., API clients, legacy integrations, or custom PHP modules). Here’s how to start:
Install via Composer
composer require --dev drupal/coder
Run a One-Time Check
Target a specific directory (e.g., app/Modules or app/Shared):
./vendor/bin/phpcs --standard=Drupal --extensions=php app/Modules
--report=full for detailed output.--warning-severity=3 to treat warnings as errors.First Use Case: Pre-Commit Hook
Integrate with Laravel’s pre-commit (via laravel-pint or custom scripts) to block non-compliant code:
./vendor/bin/phpcs --standard=Drupal --extensions=php app/Modules || exit 1
Drupal (coding standards) + DrupalPractice (best practices) in parallel.
./vendor/bin/phpcs --standard=Drupal,DrupalPractice app/Modules
--fix (where supported):
./vendor/bin/phpcs --standard=Drupal --extensions=php --fix app/Modules
Settings > Languages & Frameworks > PHP > Code Sniffer."intelephense.codeSnifferEnabled": true,
"intelephense.codeSnifferPath": "./vendor/bin/phpcs",
"intelephense.codeSnifferStandard": "Drupal,DrupalPractice"
- name: Run Coder
run: ./vendor/bin/phpcs --standard=Drupal,DrupalPractice --warning-severity=3 --extensions=php app/Modules
phpcs.xml.dist to override defaults or disable rules temporarily:
<ruleset>
<rule ref="Drupal">
<exclude name="Drupal.Commenting.Deprecated"/> <!-- Skip deprecated comments -->
</rule>
<rule ref="DrupalPractice">
<severity name="DrupalPractice.Functions.ValidFunctionName" value="warning"/>
</rule>
</ruleset>
Drupal.NamingConventions.ValidFunctionName for snake_case vs. Laravel’s camelCase).False Positives in Laravel Context
Drupal.NamingConventions.ValidVariableName expect snake_case but Laravel uses camelCase.phpcs.xml.dist:
<rule ref="Drupal.NamingConventions">
<exclude name="ValidVariableName"/>
</rule>
Performance with Large Codebases
app/Modules or app/Shared.--parallel (PHP 7.4+) for multi-core processing:
./vendor/bin/phpcs --parallel=4 --standard=Drupal app/Modules
--fix Limitations
DrupalPractice).YAML/Markdown Files
phpcs may scan non-PHP files (e.g., .md, .yml) if --extensions is misconfigured.--extensions=php,module,inc
--verbose to debug rule application:
./vendor/bin/phpcs --standard=Drupal --verbose app/Modules
./vendor/bin/phpcs --standard=Drupal app/Modules/MyModule.php
Custom Sniffs
drupal/coder by creating custom PHPCS sniffs (PHP classes) and referencing them in phpcs.xml.dist:
<rule ref="Custom.MySniff"/>
Combine with Other Tools
phpstan.neon for static analysis:
./vendor/bin/phpstan analyse --level=5 && ./vendor/bin/phpcs --standard=Drupal app/
./vendor/bin/pint && ./vendor/bin/phpcs --standard=Drupal app/
Git Hooks
# .git/hooks/pre-commit
#!/bin/sh
./vendor/bin/phpcs --standard=Drupal --extensions=php app/Modules || exit 1
git merge origin/main && ./vendor/bin/phpcs --standard=Drupal app/
hook_* prefixes).DrupalPractice.Functions.ValidFunctionName if using Laravel’s Str:: or Helper classes.app/Models if using Eloquent (conflicts with Drupal.Database rules).How can I help you explore Laravel packages today?