Installation Add the package to your project via Composer:
composer require --dev azuyalabs/php-cs-fixer-config
Configuration Copy the default config file to your project’s root:
cp vendor/azuyalabs/php-cs-fixer-config/php-cs-fixer.dist.php .php-cs-fixer.dist.php
Ensure it’s included in .gitignore if not already.
First Use Case Run PHP CS Fixer on your project:
vendor/bin/php-cs-fixer fix --rules=@azuyalabs
Verify changes with a dry run:
vendor/bin/php-cs-fixer fix --dry-run --rules=@azuyalabs
CI/CD Integration
Add a script to your composer.json:
"scripts": {
"cs-fix": "php-cs-fixer fix --rules=@azuyalabs --allow-risky=yes"
}
Run it in your CI pipeline (e.g., GitHub Actions) to enforce consistency.
Team Onboarding
php-cs-fixer.dist.php) in your team’s documentation or repo’s CONTRIBUTING.md.# .phpstorm.meta.php
rules:
- name: PHP CS Fixer
level: error
paths:
- "."
executable: vendor/bin/php-cs-fixer
arguments: "--rules=@azuyalabs --dry-run"
Project-Specific Customization Extend the base config by merging rules:
// .php-cs-fixer.dist.php
return (new PhpCsFixer\Config())
->setRules([
'@azuyalabs' => true,
'no_unused_imports' => true, // Add custom rules
'concat_space' => ['spacing' => 'one'], // Override existing
]);
Laravel-Specific Integration
// app/Console/Commands/FixCs.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class FixCs extends Command {
protected $signature = 'cs:fix';
public function handle() {
$this->call('vendor:publish', ['--tag' => 'php-cs-fixer-config']);
$this->call('php-cs-fixer', ['fix', '--rules=@azuyalabs']);
}
}
app/Console/Kernel.php:
protected $commands = [
Commands\FixCs::class,
];
Pre-Commit Hook
Use husky or pre-commit to run checks before commits:
echo 'vendor/bin/php-cs-fixer fix --rules=@azuyalabs --dry-run --diff' >> .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
Rule Conflicts
array_syntax, class_attributes) may conflict with Laravel’s coding standards.'array_syntax' => ['syntax' => 'short'], // Force short syntax
Performance
--parallel for multi-core processing:
vendor/bin/php-cs-fixer fix --rules=@azuyalabs --parallel
IDE Integration Issues
.php-cs-fixer.dist.php and placed in the project root.Risky Rules
no_unused_imports or simplified_null_return can break functionality.--allow-risky=yes cautiously and review changes:
vendor/bin/php-cs-fixer fix --rules=@azuyalabs --allow-risky=yes --diff
Dry Run Mismatches
If changes appear in dry-run but not in fix, check:
.gitignore exclusions (e.g., vendor/, node_modules/).Rule-Specific Errors
--verbose to debug rule application:
vendor/bin/php-cs-fixer fix --rules=@azuyalabs --verbose
Custom Rule Sets Create a project-specific rule set by extending the base config:
// php-cs-fixer.dist.php
return (new PhpCsFixer\Config())
->setRules([
'@azuyalabs' => true,
'@PhpCsFixer' => true, // Include default rules
'risky_nullable_type_declaration_for_php7' => false, // Disable risky rules
]);
Path Filtering Target specific directories or files:
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('tests')
->name('*.php')
->notName('*.blade.php')
)
CI-Specific Configs Use environment variables to switch configs:
$rules = getenv('CI') ? ['@azuyalabs', 'no_unused_imports'] : ['@azuyalabs'];
return (new PhpCsFixer\Config())->setRules($rules);
Laravel Mix/Webpack
Add a script to auto-fix during npm run dev:
// webpack.mix.js
mix.webpackConfig({
plugins: [
new FixCsPlugin() // Hypothetical plugin; use a custom script instead
]
});
Alternative: Use a post-build hook in package.json:
"scripts": {
"dev": "mix && composer cs-fix",
"cs-fix": "php-cs-fixer fix --rules=@azuyalabs"
}
How can I help you explore Laravel packages today?