DEV Community

Cover image for What Is PHP CS Fixer and How It Can Help You Keep Your Code Cleaner
Andrew Savetchuk
Andrew Savetchuk

Posted on • Updated on • Originally published at blog.savetchuk.com

What Is PHP CS Fixer and How It Can Help You Keep Your Code Cleaner

What is PHP CS Fixer​

PHP CS Fixer stands for PHP Coding Standards Fixer. This is a tool that fixes your code to follow standards. There are various PHP coding standards that you can follow, such as the popular PSR-1, and PSR-12 published by the PHP Framework Interoperability Group (the full list of PSR standards can be found here).

There are also other community-driven standards like the Symphony.

With PHP CS Fixer, you can follow multiple coding standards at once, or you can also define your own or your team's own coding standard through configuration. If you have ever worked with JavaScript and Eslint in particular, think of PHP CS Fixer as an alternative to Eslint for PHP.

Manually fixing coding standards in your code is a very tedious process, so PHP CS Fixer is a great solution. It helps you and your team ensure that you all follow the same rules when writing code, thus keeping it cleaner.

Installation​

The recommended way to install PHP CS Fixer is to use Composer in a dedicated directory in your project, for example in the tools/php-cs-fixer.

mkdir --parents tools/php-cs-fixer
composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer
Enter fullscreen mode Exit fullscreen mode

For more details and other installation methods, see installation instructions.

Usage

Usage: Option 1​

Assuming you installed PHP CS Fixer as instructed above, you can run the following command to fix the files PHP files in the src directory:

tools/php-cs-fixer/vendor/bin/php-cs-fixer fix src
Enter fullscreen mode Exit fullscreen mode

See usage, list of built-in rules, list of rule sets, and configuration file documentation for more details. If you need to apply code styles that are not supported by the tool, you can create custom rules.

Usage: Option 2 (Editor Integration)​

However, executing a command manually does not seem to be a convenient approach. I personally prefer to run PHP CS Fixer by pressing a pre-configured keyboard shortcut in PHP Storm.

You can configure PHP CS Fixer for various code editors as described below:

Usage: Option 3 (Run PHP CS Fixer on every commit)​

As an alternative option, you can fix your code on every commit using Husky and lint-staged. Using these tools PHP CS Fixer will run on your staged files, in other words, on the files you want to commit. Here is a great article explaining how to set up these tools to work with PHP CS Fixer.

Easy way of configuring PHP CS Fixer​

If you are new to PHP Coding Standards and want to quickly configure PHP CS Fixer, I highly recommend using PHP CS Fixer Configurator by Michele Locati. It has a pre-defined set of rules that you can select and create your own configuration.

PHP CS Fixer Configurator

Most rules, when clicked, will display general information about the rule, available configuration options, and usage examples. The PHP CS Fixer configuration file generated by this tool can be exported to use in your project and then imported again in case you want to do some modifications.

PHP CS Fixer Configurator


The end, I hope this information was helpful, stay tuned for more content :)


Source

  1. FriendsOfPHP/PHP-CS-Fixer: A tool to automatically fix PHP Coding Standards issues
  2. PHP-CS-Fixer Configurator

Top comments (2)

Collapse
 
gbhorwood profile image
grant horwood

setting up cs fixer in vim is pretty straightforward:

install the phar locally

git clone https://github.com/stephpy/vim-php-cs-fixer.git ~/.vim/pack/plugins/start/vim-php-cs-fixer
curl -L https://cs.symfony.com/download/php-cs-fixer-v2.phar -o ~/.vim/php-cs-fixer.phar
chmod 755  ~/.vim/php-cs-fixer.phar
Enter fullscreen mode Exit fullscreen mode

then in your .vimrc

"-------------------------------
" php-cs-fixer config
" <F4> fix
let g:php_cs_fixer_path = "~/.vim/php-cs-fixer.phar"
let g:php_cs_fixer_rules = "@PSR2"
let g:php_cs_fixer_php_path = "/usr/bin/php"
map <F4> :call PhpCsFixerFixFile()<CR>
Enter fullscreen mode Exit fullscreen mode

obviously, you can change the hotkey or the ruleset.

Collapse
 
chuniversiteit profile image
Chun Fei Lung

PHP CS Fixer helps you keep your code clean, but more importantly it also helps you keep your pull requests clean from endless holy wars over coding styles.