This repository provides a configuration file for PHP CS Fixer, a tool designed to automatically enforce PHP coding standards and fix code style issues.
To use this configuration in your project:
- Copy the
.php-cs-fixer.dist.php
file to the root of your project. - Install PHP CS Fixer via Composer:
composer require friendsofphp/php-cs-fixer
- Run PHP CS Fixer to apply the configuration:
vendor/bin/php-cs-fixer fix
The .php-cs-fixer.dist.php
configuration:
- Adheres to PSR-12 and Symfony coding standards.
- Enforces modern PHP practices, such as:
- Short array syntax (
[]
instead ofarray()
). - Strict types declaration.
- Arrow functions where applicable.
- Ordered class elements and imports.
- Short array syntax (
- Applies to the
src
andtests
directories. - Includes rules for clean code, such as removing superfluous PHPDoc tags and ensuring consistent spacing.
- For a complete list of rules, see the
.php-cs-fixer.dist.php
file.
Copy file data to .php-cs-fixer.dist.php in you project root folder or run:
mv ../repo/php-cs-fixer.dist.php .php-cs-fixer.dist.php
Run PHP CS Fixer with the provided configuration:
vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php
To check for issues without modifying files, use:
vendor/bin/php-cs-fixer fix --dry-run
-
Modern Best Practices
Includesdeclare_strict_types
,use_arrow_functions
,static_lambda
,strict_comparison
, andfinal_internal_class
— promoting clean, future-proof code. -
Refined Rule Selection
You explicitly enable or disable rules likeclass_definition
,phpdoc_to_comment
, andno_break_comment
, giving you full control rather than relying on blanket presets. -
Flexible
native_function_invocation
Unlike@PER-CS2.0
, your config usesstrict: false
, allowinguse function
imports and preventing unwanted global backslashes. -
Improved Readability
With fine-grained formatting control (cast_spaces
,trailing_comma_in_multiline
,ordered_imports
,concat_space
), the resulting code is more readable and consistent. -
Clean Class Structure
The customordered_class_elements
block ensures logical organization of class elements, which is often missing in default presets. -
Enhanced PHPUnit Style
Enforcingphp_unit_construct
andphp_unit_method_casing
ensures consistent test structure without relying on broader, less focused test presets. -
Comment and Docblock Hygiene
Your inclusion ofphpdoc_order
,phpdoc_align
, and trimming rules improves the clarity of PHPDoc without over-relying on auto-removal. -
No Useless Code
Rules likeno_useless_return
andno_useless_else
keep the codebase minimal and expressive.
Contributions are welcome! If you have suggestions for improving the configuration:
- Fork this repository.
- Create a new branch (
git checkout -b feature/your-feature
). - Make your changes and commit (
git commit -m "Add your feature"
). - Push to the branch (
git push origin feature/your-feature
). - Open a Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.