My own PHP CS Fixer setup: A practical example
In my previous article, PHP CS Fixer: A Complete Guide to Installation, Usage, and Integration, I covered the fundamentals of PHP CS Fixer, including installation, usage, IDE integration, and GitLab CI/CD setup. Now, I want to share my personal PHP CS Fixer configuration, which ensures clean and consistent code across my private Symfony project.
My PHP CS Fixer Configuration
Here’s the .php-cs-fixer.php
configuration file I use in my Symfony projects:
<?php
$finder = PhpCsFixer\Finder::create()->in([
__DIR__ . '/src',
__DIR__ . '/tests',
__DIR__ . '/migrations'
]);
return (new PhpCsFixer\Config())
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
->setRules([
'@Symfony' => true,
'@PSR12' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'combine_consecutive_unsets' => true,
'single_quote' => true,
'ordered_imports' => [
'sort_algorithm' => 'alpha',
'imports_order' => ['const', 'class', 'function'],
],
'no_useless_else' => true,
'phpdoc_order' => true,
'header_comment' => [
'header' => "This file is part of the proprietary project.\n\nThis file and its contents are confidential and protected by copyright law.\nUnauthorized copying, distribution, or disclosure of this content\nis strictly prohibited without prior written consent from the author or\ncopyright owner.\n\nFor the full copyright and license information, please view the LICENSE.md\nfile that was distributed with this source code.",
'separate' => 'both'
]
])
->setFinder($finder);
Why This Setup?
1. Targeted Codebase Scanning
- The
Finder
is configured to scan only thesrc
,tests
, andmigrations
directories. - This avoids unnecessary formatting of vendor files or other non-essential directories.
2. Coding Standards Enforcement
- The
@Symfony
and@PSR12
rulesets ensure compliance with widely accepted PHP coding standards. array_syntax
enforces short array syntax ([]
instead ofarray()
).single_quote
ensures single quotes are used where possible, making string handling more consistent.
3. Code Readability Improvements
array_indentation
ensures arrays are properly indented.ordered_imports
alphabetically organizes import statements and follows a specific import order:const
,class
,function
.phpdoc_order
ensures PHPDoc comments are structured consistently.
4. Unnecessary Code Removal
combine_consecutive_unsets
merges multipleunset()
calls into a single statement.no_useless_else
removes unnecessaryelse
statements when the precedingif
block already returns or exits.
5. Proprietary Code Protection
header_comment
automatically adds a proprietary notice to the top of each file, ensuring copyright and license details are included in every source file.
Running PHP CS Fixer via Composer
To simplify running PHP CS Fixer, I have added a custom script in composer.json
:
"scripts": {
"cs-fix": "./vendor/bin/php-cs-fixer fix"
}
Now, instead of typing the full command every time, I can simply run:
composer cs-fix
This makes it easier to enforce coding standards without remembering the full path to the binary.
How to Apply This Configuration
To use this setup in your Symfony project:
1. Copy the above configuration into .php-cs-fixer.php
in your project's root directory.
2. Run PHP CS Fixer to apply the coding standards:
php-cs-fixer fix
3. Alternatively, use the Composer script for convenience:
composer cs-fix
4. Optionally, integrate it into a Git pre-commit hook or CI/CD pipeline to enforce consistent coding standards automatically.
Conclusion
This PHP CS Fixer setup has been a game-changer in my Symfony projects, ensuring a consistent coding style and reducing manual formatting efforts. By applying these rules, I can focus on writing clean and maintainable PHP code without worrying about style inconsistencies.
Have you customized your PHP CS Fixer configuration? Let me know your favorite rules and settings in the comments!