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.
$ 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 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;",
}
- Add config file
.php_cs
$ cd ~/.vscode
$ touch .php_cs
- 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');
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);
}
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!";
}
Output
if (! $foo) {
echo "Help!";
}
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)
Nice!!
Thanks Dude!
Great Article.... Thanks, it helped me understand some stuff around.