codeat3/phpcs-styles
codeat3/phpcs-styles provides PHP_CodeSniffer rulesets and coding style configurations to standardize formatting and code quality across PHP projects, helping teams enforce consistent conventions in CI and local development.
Install the Package
composer require --dev codeat3/phpcs-styles
Configure PHP-CS-Fixer
Update .php-cs-fixer.dist.php to include the ruleset:
<?php
return (\Codeat3\PhpcsStyles\Config::getRules())->merge([
// Customize rules as needed
'line_ending' => "\n",
]);
First Run Apply fixes to your codebase:
vendor/bin/php-cs-fixer fix
Where to Look First
vendor/codeat3/phpcs-styles/src/Config.php for default rules.laravel-pint (built on PHP-CS-Fixer) for Laravel-specific optimizations.Team-Wide Standardization Use the package as a shared baseline for PHP-CS-Fixer across all Laravel projects. Extend with project-specific rules:
return (\Codeat3\PhpcsStyles\Config::getRules())->merge([
'php_unit_method_casing' => true,
'php_unit_test_class_requires_covers' => false,
]);
CI/CD Enforcement
Integrate into GitHub Actions (.github/workflows/php-cs-fixer.yml):
name: PHP-CS-Fixer
on: [push, pull_request]
jobs:
fix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: vendor/bin/php-cs-fixer fix --rules=@codeat3/phpcs-styles --dry-run
IDE Integration
Settings > Tools > PHP > Code Sniffer.Custom Ruleset to vendor/codeat3/phpcs-styles/ruleset.xml."php-cs-fixer.executablePath": "vendor/bin/php-cs-fixer",
"php-cs-fixer.rules": "@codeat3/phpcs-styles"
Laravel-Specific Workflows
laravel-pint, extend pint.json:
{
"preset": "laravel",
"rules": {
"@codeat3/phpcs-styles": true
}
}
.git/hooks/pre-commit:
#!/bin/sh
vendor/bin/php-cs-fixer fix --rules=@codeat3/phpcs-styles --dry-run
Rule Conflicts with Laravel Defaults
pint or php-cs-fixer configurations (e.g., line length, array syntax).return (\Codeat3\PhpcsStyles\Config::getRules())->merge([
'array_syntax' => ['syntax' => 'short'],
'line_length' => ['line_ending' => "\n", 'allow_parentheses_on_control_structures' => true],
]);
Performance in Large Codebases
- name: Cache PHP-CS-Fixer
uses: actions/cache@v3
with:
path: ~/.cache/php-cs-fixer
key: ${{ runner.os }}-php-cs-fixer
bootstrap/cache):
$finder->exclude(['bootstrap/cache', 'storage/framework']);
Missing Ruleset File
ruleset.xml for direct PHPCS use.vendor/bin/php-cs-fixer dump-ruleset --rules=@codeat3/phpcs-styles > ruleset.xml
IDE Misconfiguration
ruleset.xml or use the CLI path:
vendor/bin/php-cs-fixer --rules=@codeat3/phpcs-styles
Dry Run with Diff Preview changes before applying:
vendor/bin/php-cs-fixer fix --rules=@codeat3/phpcs-styles --dry-run --diff
Rule-Specific Debugging Isolate problematic rules:
vendor/bin/php-cs-fixer fix --rules=@codeat3/phpcs-styles --rules-to-fix=line_ending
Logging Enable verbose output for troubleshooting:
vendor/bin/php-cs-fixer fix --rules=@codeat3/phpcs-styles -v
Custom Rule Overrides Temporarily disable rules to debug:
$rules = (\Codeat3\PhpcsStyles\Config::getRules())->getRules();
unset($rules['no_unused_imports']); // Disable temporarily
Fork and Extend Fork the package to add custom rules:
git clone https://github.com/codeat3/phpcs-styles.git
composer require your-fork/phpcs-styles --dev
Composer Scripts
Automate rule application in composer.json:
"scripts": {
"cs-fix": "php-cs-fixer fix --rules=@codeat3/phpcs-styles"
}
Run with:
composer cs-fix
Dynamic Rule Loading Load rules conditionally based on environment:
$rules = (\Codeat3\PhpcsStyles\Config::getRules());
if (app()->environment('local')) {
$rules->setRiskyAllowed(true);
}
Pint Integration
If using laravel-pint, ensure compatibility:
composer require --dev laravel/pint
pint --preset=laravel --rules=@codeat3/phpcs-styles
Artisan Command Create a custom Artisan command for quick fixes:
// app/Console/Commands/FixCodeStyle.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class FixCodeStyle extends Command
{
protected $signature = 'code:fix';
public function handle()
{
$this->call('php-cs-fixer', ['--rules=@codeat3/phpcs-styles']);
}
}
Register in app/Console/Kernel.php:
protected $commands = [
\App\Console\Commands\FixCodeStyle::class,
];
Run with:
php artisan code:fix
Testing Add PHP-CS-Fixer checks to Laravel’s test suite:
// tests/Feature/CodeStyleTest.php
public function test_code_style_compliance()
{
$this->artisan('code:fix')
->expectsOutputToContain('Everything looks good!')
->assertExitCode(0);
}
How can I help you explore Laravel packages today?