What Is PHP CS Fixer and How It Can Help You Keep Your Code Cleaner
Tags: php, backend, tools
Published: 2022-05-05 12:38:09

---
title: What Is PHP CS Fixer and How It Can Help You Keep Your Code Cleaner
published: true
description: 
tags: #php, #backend, #tools
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wsxvl7otg8epzbpysg7q.jpg
canonical_url: https://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,](https://www.php-fig.org/psr/psr-1/) and [PSR-12](https://www.php-fig.org/psr/psr-12/) published by the PHP Framework Interoperability Group (the full list of PSR standards can be found [here](https://www.php-fig.org/psr/)). 

There are also other community-driven standards like the [Symphony](https://symfony.com/doc/current/contributing/code/standards.html).

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](https://getcomposer.org/download/) in a dedicated directory in your project, for example in the `tools/php-cs-fixer`.

```bash
mkdir --parents tools/php-cs-fixer
composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer
```

For more details and other installation methods, see [installation instructions](https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/installation.rst).

## 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:

```bash
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix src
```

See [usage](https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/usage.rst), list of [built-in rules](https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/rules/index.rst), list of [rule sets](https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/ruleSets/index.rst), and [configuration file](https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/config.rst) documentation for more details. If you need to apply code styles that are not supported by the tool, you can [create custom rules](https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/custom_rules.rst).

### 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:
* [Atom](https://github.com/Glavin001/atom-beautify)
* [NetBeans](https://plugins.netbeans.apache.org/catalogue/?id=36)
* [PhpStorm](https://www.jetbrains.com/help/phpstorm/using-php-cs-fixer.html)
* [Sublime Text](https://github.com/benmatselby/sublime-phpcs)
* [Vim](https://github.com/stephpy/vim-php-cs-fixer)
* [VS Code](https://github.com/junstyle/vscode-php-cs-fixer)

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

As an alternative option, you can fix your code on every commit using [Husky](https://www.npmjs.com/package/husky) and [lint-staged](https://www.npmjs.com/package/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](https://sebastiandedeyne.com/running-php-cs-fixer-on-every-commit-with-husky-and-lint-staged/) 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](https://mlocati.github.io/php-cs-fixer-configurator/) by [Michele Locati](https://github.com/mlocati). It has a pre-defined set of rules that you can select and create your own configuration.

![PHP CS Fixer Configurator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s8wo0hu8wciq6140et50.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7g7xrsl2o8sngb98i65.png)

---

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](https://github.com/FriendsOfPHP/PHP-CS-Fixer)
1. [PHP-CS-Fixer Configurator](https://mlocati.github.io/php-cs-fixer-configurator/)