automattic/phpcs-neutron-ruleset
Neutron ruleset for PHP_CodeSniffer from Automattic. Provides opinionated coding standards and sniffs to enforce consistent PHP style across projects, helping catch formatting and quality issues in CI and local development.
Installation Add the package via Composer in your Laravel project:
composer require --dev automattic/phpcs-neutron-ruleset
Ensure phpcs is installed globally or via Composer:
composer global require squizlabs/php_codesniffer
Basic Configuration
Create a phpcs.xml (or phpcs.xml.dist) in your project root with the Neutron ruleset:
<?xml version="1.0"?>
<ruleset>
<config name="installed_paths" value="./vendor/automattic/phpcs-neutron-ruleset"/>
<rule ref="Neutron"/>
</ruleset>
First Run
Run PHPCS on a Laravel file (e.g., app/Http/Controllers/ExampleController.php):
phpcs app/Http/Controllers/ExampleController.php
Focus on fixing WordPress-specific violations (e.g., WordPress.WP.CapitalPDOTOR for class naming).
If your Laravel app integrates with WordPress (e.g., REST API, plugins), use Neutron to enforce:
WP_ prefix for WordPress classes).add_action('init', [$this, 'method'])).WordPress.Security.ValidatedSanitizedInput).Example workflow:
phpcs wp-plugins/my-plugin/
WordPress.WP.PrefixAllGlobals (ensure $_GET vars use WP_ prefix).WordPress.WP.AlternativeFunctions (replace mysql_* with wpdb).Combine Neutron with Laravel-specific rules (e.g., phpcs-standards):
<ruleset>
<config name="installed_paths" value="./vendor/automattic/phpcs-neutron-ruleset,./vendor/squizlabs/php_codesniffer-standard"/>
<rule ref="Neutron"/>
<rule ref="Squiz"/>
<exclude-pattern>*/tests/*</exclude-pattern> <!-- Skip tests -->
</ruleset>
Add PHPCS to Laravel’s GitHub Actions (.github/workflows/phpcs.yml):
name: PHPCS
on: [push, pull_request]
jobs:
phpcs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: composer install
- run: phpcs --standard=./phpcs.xml --extensions=php app/
Disable specific rules for Laravel-specific code (e.g., WordPress.WP.PrefixAllGlobals for config files):
<rule ref="Neutron">
<exclude name="WordPress.WP.PrefixAllGlobals"/>
</rule>
Use husky to run PHPCS before commits:
# package.json
{
"scripts": {
"pre-commit": "phpcs --standard=./phpcs.xml --extensions=php app/"
}
}
Configure PHPStorm to use Neutron:
./phpcs.xml as the Configuration File.False Positives in Laravel
WordPress.WP.PrefixAllGlobals may flag Laravel’s $_ENV or config vars.config/ or bootstrap/ in phpcs.xml:
<exclude-pattern>config/*,bootstrap/*</exclude-pattern>
Outdated Rules
phpcs-standards/laravel or suppress irrelevant rules.Performance
phpcs --diff app/PreviousCommitHash app/CurrentCommitHash
WordPress-Specific Assumptions
WordPress.WP.AlternativeFunctions may conflict with Laravel’s DB layer.<exclude> for non-WP files:
<exclude name="WordPress.WP.AlternativeFunctions" from="Database/*"/>
-v to see skipped rules:
phpcs -v app/Http/Controllers/
--report=full to get details on violations.Custom Rules Extend Neutron by adding your own ruleset:
<rule ref="Neutron"/>
<rule ref="./ruleset.xml"/> <!-- Your custom rules -->
Sniffs for Laravel
Contribute to the package by adding Laravel-specific sniffs (e.g., for Illuminate\Support\Facades).
Dynamic Configuration
Use PHPCS’s <arg> to pass project-specific values:
<arg name="tab-width" value="4" />
WordPress.Security and WordPress.WP.CapitalPDOTOR for WordPress-Laravel hybrids.phpcs --ignore: Temporarily skip files during refactoring:
phpcs --ignore=app/OldCode.php app/
phpmd: Use PHPMD for additional complexity analysis:
phpmd app/ text cleancode,codesize,unusedcode --exclude tests/
How can I help you explore Laravel packages today?