Skip to content

Instantly share code, notes, and snippets.

@mpalourdio
Created November 12, 2014 21:24
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save mpalourdio/46f792347cf9d46b121c to your computer and use it in GitHub Desktop.
Save mpalourdio/46f792347cf9d46b121c to your computer and use it in GitHub Desktop.
@fabpot php-cs-fixer config for PhpStorm
parameters
--level=psr2 --verbose fix $FileDir$/$FileName$
working directory
$ProjectFileDir$
@soullivaneuh
Copy link

Looks interesting.

Where should I put this 4 code lines? Is will files be automatically fixed on save?

Thanks.

@mpalourdio
Copy link
Author

Go to settings, and configure a new external tool. Fill Parameters and working directory as specified.

No 'fix on save', just run the tool when needed, or configure a pre-commit hook.

@teameh
Copy link

teameh commented Oct 2, 2015

Works like a charm, thnx

image

@bvarent
Copy link

bvarent commented Jan 6, 2016

Use $FilePath$ instead of $FileDir$/$FileName$ to support directories properly.

@trunda
Copy link

trunda commented Apr 3, 2016

'Fix on save' can be achieved by macro, if you are interested. Thank you for the gist, which was helpful for me.

@ksassnowski
Copy link

ksassnowski commented May 25, 2016

You can also use a File Watcher to enable fix on save. The configuration stays exactly the same.

bildschirmfoto 2016-05-25 um 10 37 59

Thanks to simonneaves below for pointing out that you probably want to untick 'Immediate file synchronization'. Otherwise it will run the command on every key stroke which is definitely not what you want!

@kiisodj
Copy link

kiisodj commented Jun 4, 2016

Thanks kssassnowski. For fix on save, rather than 'fix as you type', I unticked 'Immediate file synchronization'.

@sergiodebcn
Copy link

Thanks for the info, very useful

@mcfedr
Copy link

mcfedr commented Feb 8, 2017

Excellent, my changes, just as a note, as I am using a config file:
Arguments:
fix -v --config .php_cs.dist --path-mode=intersection -- $FilePath$

@skoop
Copy link

skoop commented Apr 24, 2017

thank you @ksassnowski, I've just used that to set it up for me. works like a charm

Copy link

ghost commented May 3, 2017

--level doesn't exist anymore, for an updated config take a look at : https://hackernoon.com/how-to-configure-phpstorm-to-use-php-cs-fixer-1844991e521f

@mfour-merickson
Copy link

@mcfedr Perhaps you would not mind sharing your config file? I have pretty sensible defaults but always intrigued by what others are using.

@unglud
Copy link

unglud commented Dec 1, 2017

If I put it to After Commit Run Tool how can I fix all commited files instead only one currently opened?

@j3rrey
Copy link

j3rrey commented Nov 20, 2018

Step by step:

composer global require friendsofphp/php-cs-fixer

paste this export PATH="$PATH:$HOME/.composer/vendor/bin" into your .zshrc or .bashrc or .profile depending on your system usually located at your HOME directory
Linux: .bashrc
Mac: .profile
Mac with Oh my zsh : .zshrc

Save this code as watcher.xml on your harddrive.
You can further edit the arguments aka add rules:
<option name="arguments" value="fix $FileDir$/$FileName$ --allow-risky=yes rules=@PSR2, @Symfony" />
Various rules can be found here : Rules By default the cs Fixer uses PSR2

<TaskOptions>
  <TaskOptions>
    <option name="arguments" value="fix $FileDir$/$FileName$ --allow-risky=true" />
    <option name="checkSyntaxErrors" value="true" />
    <option name="description" />
    <option name="exitCodeBehavior" value="ERROR" />
    <option name="fileExtension" value="php" />
    <option name="immediateSync" value="false" />
    <option name="name" value="PHP CS Fixer" />
    <option name="output" value="" />
    <option name="outputFilters">
      <array />
    </option>
    <option name="outputFromStdout" value="false" />
    <option name="program" value="$USER_HOME$/.composer/vendor/bin/php-cs-fixer" />
    <option name="runOnExternalChanges" value="false" />
    <option name="scopeName" value="Project Files" />
    <option name="trackOnlyRoot" value="false" />
    <option name="workingDir" value="$ProjectFileDir$" />
    <envs />
  </TaskOptions>
</TaskOptions>

Import it in phpstorm:
PHPstorm->preferences->tools->File Watchers
Import

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment