So recently I have seen many developers in the PHP community ordering their use statements by length. I always wondered how to do this in PhpStorm. I think I have finally found a solution but I guess I am doing something wrong here and it is not working.
So a tool called PHP-CS-Fixer has an option to do this. And I know I can use this tool with PhpStorm using Tools -> External Tools
and clicking on the +
sign and then configuring it.
Note: I pulled the tool using Composer like so composer global require friendsofphp/php-cs-fixer
So my configuration of the external tool in PhpStorm looks like so:
/Users/rohan0793/.composer/vendor/bin/php-cs-fixer
fix --rules='{"ordered_imports": {"sort_algorithm":"length"}}' $FileDir$/$FileName$
$ProjectFileDir$
But when I ran it, I saw this error:
The rules contain unknown fixers: "'{ordered_imports{sort_algorithm:length}}'".
So I guess I am doing something wrong here while configuring it but cannot seem to figure out what. Any idea anyone?
Arguments look valid, if one runs the following command from CLI, it works perfectly, thus indeed it is something about passing this configuration from IDE to CLI.
php-cs-fixer fix --rules='{"ordered_imports": {"sort_algorithm":"length"}}'
Let me propose how to do it differently. In general, best practice is to put configuration into config file, not into IDE settings, so config file can be shared along with the repository. Example here: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.12/.php_cs.dist
Also, you don't need to configure external tool for PHPStorm. It now has built-in support for PHP CS Fixer, see the blog post here: https://blog.jetbrains.com/phpstorm/2018/09/phpstorm-2018-3-early-access-program-is-open/