Configure PHP-CS-Fixer for Laravel on Visual Code
Tags: laravel, php, phpcsfixer, vscode
Published: 2019-12-06 18:15:43

---
title: Configure PHP-CS-Fixer for Laravel on Visual Code
published: true
description: Configuration PHP-CS-Fixer for Laravel on Visual Studio Code
tags: #laravel #php #phpcsfixer #vscode
---

Hi, developers. :smile:

I will teach you how to configure *php-cs-fixer* for laravel in the visual code editor.

Often, when I write PHP code, I can make mistakes, such as lack of spaces, structure of conditions, etc. :sweat_smile:

# What is PHP-CS-Fixer?

PHP-CS-Fixer is a standard for ordened your code and apply code style guide PSR for PHP.

PHP-CS-Fixer It is a tool that helps to organize the application code, following the PSR standards.

# Installation PHP-CS-Fixer.

[Github PHP-CS-Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer)

```cmd
$ brew install php-cs-fixer
```

# Installation VSCode

```
brew update
brew tap caskroom/cask
brew cask search visual-studio-code
```

# Configuration

- Open VSCode editor
- Add extension [php cs fixer](https://marketplace.visualstudio.com/items?itemName=junstyle.php-cs-f) to VSCode
- Add to `settings.json` file

```js
{
  "editor.formatOnSave": true,
  
  "php-cs-fixer.onsave": true,
  "php-cs-fixer.executablePath": "${extensionPath}/php-cs-fixer.phar",
  "php-cs-fixer.config": "~/.vscode/.php_cs;",
}
```

- Add config file `.php_cs`
```
$ cd ~/.vscode
$ touch .php_cs
```
- Open config file `.php_cs` and add PHPCSFixer configuration.

{% gist https://gist.github.com/zaratedev/9220085c73c37b0921457bce0f51334a %}

Ok, The configuration that I use to work PHP-CS-Fixer with Laravel applications is optional. :smile:

I will begin to explain the changes that I have made in the configuration file `.php_cs`.

#### concat_space:
`'concat_space' => array('spacing' => 'none'),`

Example
```php
  $this->load(__DIR__.'/Commands');
```
Spacing to apply around concatenation operator, I like it with no space.

#### unsend imports:
`'no_unused_imports' => true,`

Remove imports that are not used in a class.

#### ordered_imports
`'ordered_imports' => array('sort_algorithm' => 'alpha'),`


Currently in Laravel 6, the order of imports was included alphabetically, Here the [Pull Request](https://github.com/laravel/framework/pull/29933) of the discussion for this change. :grimacing:

Personally, I liked the order by length. :smile:

#### return_type_declaration

`'return_type_declaration' => array('space_before' => 'one'),`

There should be one or no space before colon, and one space after it in return type declarations, according to configuration.

example of my configuration.

```php
    /**
     * Check if the user is a admin.
     *
     * @return bool
     */
    public function isAdmin() : bool
    {
        return $this->roles->contains('name', Role::ADMIN);
    }
```

Add a space before the colon.

#### not_operator_with_successor_space

`'not_operator_with_successor_space' => true,`

Logical NOT operators (!) should have one trailing whitespace.

Example.

Input
```php
if (!$foo) {
    echo "Help!";
}
```

Output
```php
if (! $foo) {
    echo "Help!";
}
```

# Conclusion.

**PHP-CS-Fixer** is a very useful tool when writing PHP code in Laravel applications. :nerd_face: 

I hope you like this post, if you have questions about me you can write a comment below or send a message to my twitter. :wave:

Thanks.