1

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:

  • Program: /Users/rohan0793/.composer/vendor/bin/php-cs-fixer
  • Arguments: fix --rules='{"ordered_imports": {"sort_algorithm":"length"}}' $FileDir$/$FileName$
  • Working directory: $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?

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Rohan
  • 13,308
  • 21
  • 81
  • 154
  • 1
    1) Does the same command executes fine when typed manually in console? 2) What is the exact command that is run when running as an External Tool? 3) Order of arguments? (unlikely, but in their examples it's `fix filename --rules...`) 4) Have you tried alt option -- via config file? – LazyOne Oct 06 '18 at 08:46
  • So PHPStorm is running: `...fix --rules='{ordered_imports: {sort_algorithm:length}}'...` and when I took that whole command and ran it in the terminal, it gave me JSON invalid error and as soon as I fixed the quotes in the JSON, it ran just fine. So I guess I need to tell PHPStorm to consider those quotes, I am not sure how to. – Rohan Oct 08 '18 at 00:51
  • For reference purposes: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360001113440-How-to-escape-quotes-in-PHP-Storm-while-building-argument-list-for-an-external-tool . Try `"\"` where you need to get `"`. I mean: `fix --rules='{"\"ordered_imports"\": {"\"sort_algorithm"\":"\"length"\"}}' $FileDir$/$FileName$` – LazyOne Oct 08 '18 at 11:14
  • Yeah the phpstorm support also said the same thing, but when I tried that, the command becomes `... --rules='{\"ordered_imports\":{\"sort_algorithm\":\"length\"}}' ...` So when I ran this command in terminal manually again, still the same invalid JSON error :( – Rohan Oct 08 '18 at 19:54
  • OK. What about just `""` then? Other than that (and if you do not wish to use intermediate shell script) just file a new ticket to the [Issue Tracker](https://youtrack.jetbrains.com/issues/WI) providing all the details so the devs/support can try to reproduce exactly that. – LazyOne Oct 08 '18 at 20:28
  • Yeah, they also recommend a shell script. I guess I will just do that. Thanks for your help @LazyOne – Rohan Oct 09 '18 at 04:46
  • Are you referring to the forum post? It was me who replied there. Or perhaps you have created separate Support ticket? Then it's private. – LazyOne Oct 09 '18 at 11:51
  • Oh you're Andriy Bazanov? I didn't know. I will use a script instead. Thanks again for the help :) – Rohan Oct 10 '18 at 20:37

1 Answers1

2

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/

keradus
  • 534
  • 2
  • 5