🌐
GitHub
github.com › PHP-CS-Fixer › PHP-CS-Fixer
GitHub - PHP-CS-Fixer/PHP-CS-Fixer: A tool to automatically fix PHP Coding Standards issues · GitHub
A tool to automatically fix PHP Coding Standards issues - PHP-CS-Fixer/PHP-CS-Fixer
Starred by 13.5K users
Forked by 1.6K users
Languages   PHP
🌐
Medium
medium.com › @theihasan › how-i-use-php-cs-fixer-in-my-laravel-app-599cb51dc762
How I use PHP CS Fixer in my Laravel app | by Md. Abul Hassan | Medium
April 3, 2024 - PHP CS Fixer is a handy tool that plays a crucial role in the PHP ecosystem. Its main purpose is to automatically resolve coding standards problems in PHP code. The abbreviation “PHP Coding Standards Fixer” perfectly captures its essence.
Discussions

Fix indentation of PHP code files with php-cs-fixer - Stack Overflow
I have several hundreds of horribly indented PHP files with mixed tabs and spaces (and even mixed line endings, I suppose) I would like to fix them with php-cs-fixer v2+. I have configured php-cs-... More on stackoverflow.com
🌐 stackoverflow.com
What tools do you use to format code style in your company?
php-cs-fixer and phpcs, as mentioned, are probably going to cover what you need. I'm not sure what settings you need, but both of these tools are suitably configurable to easily handle a common base style (Symfony, PSR-12 for example) with some possible custom variations dependent on your needs. We use a dry run of php-cs-fixer backed up with phpcs in CI to prevent incorrectly-styled code from being merged. This works very well. More on reddit.com
🌐 r/PHP
29
31
February 25, 2022
Laravel with php-cs-fixer and IDE formatting
We run php-cs-fixer as a composer script in fix mode locally and then also run it in check mode as part of CI/CD, along with tests and phpmd. That means everyone, regardless of editor/IDE, can run all the checks pre push that the pipeline will run. I have no idea what the PHP Storm functionality you're talking about is but sounds like IDE-specific functionality that can't be used elsewhere, which makes it a non starter to me. More on reddit.com
🌐 r/laravel
10
5
October 10, 2021
What is the most common PHP code formatter?
php-cs-fixer https://github.com/PHP-CS-Fixer/PHP-CS-Fixer I do disagree on code standards being enforced in the IDE. My view is they absolutely need to be enforced at the project level to ensure consistency of code standard throughout that project. The IDE should be capable of configuring itself based on project owned configurations (this is the case when using php-cs-fixer). Edit: To your point of having an opinionated standard set out of the box. PSR12 https://www.php-fig.org/psr/psr-12/ . A rulset for this standard already exists in php-cs-fixer. Edit 2: For absolute transparency, there is also another popular capable of doing everything I mentioned above - https://github.com/squizlabs/PHP_CodeSniffer More on reddit.com
🌐 r/PHPhelp
19
7
August 1, 2024
🌐
JetBrains
jetbrains.com › help › phpstorm › using-php-cs-fixer.html
PHP CS Fixer | PhpStorm Documentation
To use PHP CS Fixer from PhpStorm instead of the command line, you need to register it in PhpStorm and configure it as a PhpStorm code inspection. Once installed and enabled in PhpStorm, the tool is available in any opened PHP file, and no ...
🌐
Symfony
cs.symfony.com › doc › usage.html
Usage - PHP Coding Standards Fixer
You can also exclude the rules you don’t want by placing a dash in front of the rule name, like -name_of_fixer. php php-cs-fixer.phar fix .
🌐
Phpqa
phpqa.io › projects › phpcbf.html
PHP Code Beautifier and Fixer | PHP Quality Assurance
PHP Code Beautifier and Fixer is part of a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations.
🌐
PHPHub
phphub.net › home › php fixer
PHP code fixer
March 2, 2025 - Click the "Fix PHP code" button—your code will be automatically corrected.
🌐
DEV Community
dev.to › hkp22 › mastering-code-consistency-a-guide-to-php-cs-fixer-integration-with-vscode-17cd
Mastering Code Consistency: A Guide to PHP-CS-Fixer Integration with VSCode - DEV Community
July 26, 2024 - PHP-CS-Fixer is a command-line tool for automatically fixing PHP code standards issues. It analyzes your PHP codebase and applies predefined rules to ensure conformity with coding standards such as PSR-1, PSR-2, and more.
Find elsewhere
🌐
BairesDev
bairesdev.com › tools › phpcodechecker
PHP Code Checker - Syntax Check for Common PHP Mistakes
An advanced, custom PHP code checker that searches your code for common, hard to find typos and mistakes; includes a syntax check.
🌐
Mlocati
mlocati.github.io › php-cs-fixer-configurator
PHP-CS-Fixer Configurator
A kind of personal list of notes for myself. Somebody found these notes quite useful, so I decided to share them.
Top answer
1 of 4
5

You should use braces fixer to force indentation.

The body of each structure MUST be enclosed by braces. Braces should be properly placed. Body of braces should be properly indented.

indentation_type simply enforces consistency.

But since both the fixers are already included in @PSR2 so the code should be fixed correctly.

See the relevant sections in the README.


Using your code php-cs-fixer 2.6 produces the following code

<?php
$test= "abc";
    $additional_studs = "";
    if (date('m') == 12 and $term='SP') {
        $yr_suffix = date('y') + 1;
    } else {
        $yr_suffix = date('y');
    }


    function dup_stud($id, $conn)
    {//...
    }

$i = 0;

where the indentation is only partly fixed.

I reduced it to the code below

<?php
echo "a";
    echo "b";
echo "c";

It looks like a bug in php-cs-fixer.

2 of 4
3

I will answer my own question based on the findings that led me to a resolution.

While the formatting basically worked, the catch for me was the indentation. If there were some leading spaces or tabs, certain lines kept sticking out after the fix.

Since neither php-cs-fixer nor phpcbf was able to fix the indentation properly I took desperate measures and trimmed every leading whitespace from each line as preparatory step with sed in a script like this:

sed "s/^[ \t]*//" -i test.php

Then I processed some prepped files again with php-cs-fixer and phpcbf to find out which one does a better job formatting the files according to PSR-2. It's shameful, but both fixers failed again - now showing some different shortcomings (i.e. bugs). To cut a long story short, I finally learnt that coupling the two tools leads to properly formatted code files. What a mess.

So, after sed, I run phpcbf

phpcbf --standard="PSR2" test.php

followed by

php-cs-fixer fix test.php --rules=@PSR2

And all the sudden I have beautifully PSR-2 formatted PHP files. Not the most efficient way, but it does the job.

Some additional comments:

  • If you would like to apply additional fixer rules, I would suggest to do this in a 4th step using a different, more complete php_cs configuration from a PSR-2 baseline formatting (because, you know, there are more fixer issues..).
  • I suggest to use 4 spaces as indent, as required by PSR-2. According to my experience things get even more complicated if you insist to have tabs.
  • The described procedure wouldn't be necessary if php-cs-fixer and phpcbf would not have so many issues. I will report them one after another, and hopefully, in the future the same can be achieved in one go.
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
php cs fixer - Visual Studio Marketplace
Extension for Visual Studio Code - PHP CS Fixer extension for VS Code, php formatter, php code beautify tool, format html
🌐
GitHub
github.com › squizlabs › PHP_CodeSniffer
GitHub - squizlabs/PHP_CodeSniffer: PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards. · GitHub
PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations.
Starred by 10.8K users
Forked by 1.5K users
Languages   PHP 98.1% | JavaScript 1.4%
🌐
Phptools
phptools.online
Php Tools Online
This online PHP code checker and fixer uses "PHP Code Style" (PHPCS) with some rules sets or "Phan" to analyze your code snippet.
🌐
The Valuable
thevaluable.dev › code-quality-check-tools-php
PHP Code Quality Tools to Check and Improve your Code
March 25, 2018 - Personally I don’t have any preferences regarding code formatting. What I care about is to have a consistent one: ... PHP-CS-fixer is a simple tools which allows you to format your code automatically.
🌐
Phpunit
phpunit.expert › home › articles › php_codesniffer or php-cs-fixer?
PHP_CodeSniffer or PHP-CS-Fixer?
June 25, 2024 - Over the years, for example, rules have been added that can calculate software metrics or determine the PHP version required to execute the code. In contrast, PHP-CS-Fixer makes it clear with its name alone that this tool not only wants to examine the code with its rules, but also wants to fix it: everything that this tool can report, it can also repair automatically.
🌐
GitHub
github.com › PHP-CS-Fixer
PHP CS Fixer · GitHub
PHP CS Fixer organization. PHP CS Fixer has 11 repositories available. Follow their code on GitHub.
🌐
Inspector
inspector.dev › home › php cs fixer with phpstorm
PHP CS Fixer with PHPStorm
February 4, 2025 - Automatically fix PHP coding standards in your IDE with PHP CS Fixer, ensuring consistent formatting and improving your development workflow.
🌐
Symfony
cs.symfony.com
PHP Coding Standards Fixer
The PHP Coding Standards Fixer (PHP CS Fixer) fixes your code to follow the standards.
🌐
Reddit
reddit.com › r/php › what tools do you use to format code style in your company?
r/PHP on Reddit: What tools do you use to format code style in your company?
February 25, 2022 -

In our company, we use the PhpStorm formatter. We have a codeStyle.xml file in our gitlab repository. Every PHP developer downloads it and configures PhpStorm with it. They run formatting locally before commit. We had a PhpStorm docker image that ran on CI and checked the code style.

PhpStorm has a great formatting tool, but:

  • it's hard to create a docker image with a license to use CI formatting

  • it's very slow compared to other formatting tools

  • some PHP developers prefer another IDE than PhpStorm

I have seen PHP-CS-Fixer and PHP_CodeSniffer but it doesn't have as many settings as PhpStorm.

What tools do you use to format code style in your company?