DEV Community

Jonathan Zarate
Jonathan Zarate

Posted on • Updated on

Configure PHP-CS-Fixer for Laravel on Visual Code

Hi, developers. πŸ˜„

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. πŸ˜…

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

$ brew install php-cs-fixer
Enter fullscreen mode Exit fullscreen mode

Installation VSCode

brew update
brew tap caskroom/cask
brew cask search visual-studio-code
Enter fullscreen mode Exit fullscreen mode

Configuration

  • Open VSCode editor
  • Add extension php cs fixer to VSCode
  • Add to settings.json file
{
  "editor.formatOnSave": true,

  "php-cs-fixer.onsave": true,
  "php-cs-fixer.executablePath": "${extensionPath}/php-cs-fixer.phar",
  "php-cs-fixer.config": "~/.vscode/.php_cs;",
}
Enter fullscreen mode Exit fullscreen mode
  • Add config file .php_cs
$ cd ~/.vscode
$ touch .php_cs
Enter fullscreen mode Exit fullscreen mode
  • Open config file .php_cs and add PHPCSFixer configuration.

Ok, The configuration that I use to work PHP-CS-Fixer with Laravel applications is optional. πŸ˜„

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

  $this->load(__DIR__.'/Commands');
Enter fullscreen mode Exit fullscreen mode

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 of the discussion for this change. 😬

Personally, I liked the order by length. πŸ˜„

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.

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

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

if (!$foo) {
    echo "Help!";
}
Enter fullscreen mode Exit fullscreen mode

Output

if (! $foo) {
    echo "Help!";
}
Enter fullscreen mode Exit fullscreen mode

Conclusion.

PHP-CS-Fixer is a very useful tool when writing PHP code in Laravel applications. πŸ€“

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. πŸ‘‹

Thanks.

Top comments (3)

Collapse
 
erickzh profile image
Erick Zarate Hernandez

Nice!!

Collapse
 
zaratedev profile image
Jonathan Zarate

Thanks Dude!

Collapse
 
balduinofernando profile image
Balduino Fernando

Great Article.... Thanks, it helped me understand some stuff around.