

- #Webstorm prettier on save how to#
- #Webstorm prettier on save install#
- #Webstorm prettier on save code#
Prettier inherently changes the way you code, and it is coming to many more languages other than PHP (e.g.
#Webstorm prettier on save how to#
It even knows how to write YAML files correctly, a piece of knowledge long lost in the Symfony Wars of 2009.

You can use various tools for that, such as GrumPHP, but the most straightforward way remains the following to your composer.json file: "scripts": ' which formats most files in your codebase including READMEs and fixture files. Reformat on save and on commit aren’t incompatible, but the latter is the safest way. However because we often follow some Symfony rules that would clash with the way Prettier reformats, since it does so according to PSR2, I instead recommend to add Prettier to your pre-commit pipeline/CI/etc.
#Webstorm prettier on save code#
How do you use it without having to type shit in the terminal?īecause Prettier is such a widely adopted tool, wherever Prettier is supported you can use it to format PHP code there – as long as it correctly uses the prettier where you’ve installed. So you might disagree with the final output file, but that would be missing the point which is to stop caring about whether you agree or disagree in the first place. It doesn’t fully know what it is formatting, just where it can add breaks and newlines. Prettier doesn’t do that when it formats the code it barely even knows that it is formatting PHP code because it purely translates the input into an abstract tree and formats it based on how readable it expects the output to be. This is a key difference with the formatting PHPStorm or PCS would give you because they follow very consistent rules: how to break down arguments, methods, classes, et al depending on their width, etc. If there were a third method call or if the configured max width was different, it would format the code differently. Where it gets interesting is that because Prettier rewrites the code, it continually adapts. Return $this->callSomeMethod('foo')->andThenOtherMethod( You could then pass the -write option to write the result directly to the file. If you run prettier b ad.php it will return the formatted code. Return $this->callSomeMethod('foo')->andThenOtherMethod( Bar::withSome("arguments"), An example with some badly formatted code: firstName }, prettier somefile.php or prettier src/**.php).
#Webstorm prettier on save install#
Next, as the PHP support is not yet stable, we’ll have to add that functionality into Prettier specifically: npm install -global prettier Prettier is installed, you can quickly try it out on one or more files by invoking it directly (e.g. Most of the time Prettier would be added to the project’s package.json, but since we want to use it in a PHP project we’ll install it globally through NPM (or Yarn if you want).

Most of the time, it will fix the latter, in very PHP specific ways, but will do very little for the former besides a few blank lines and indentation rules. PCS conflates code format (how the code is formatted, how pretty it is) and coding style (how the code is written, how good it is). However, a glance at the list of the things it fixes shines a light on the main reason I still use Prettier in addition to it. What is this? Ruby?Īt madewithlove we currently use (mainly but not only) PHP CS Fixer. So ok, Javascript and a bunch of other front-end languages (CSS, HTML, GraphQL, etc.) have this tool, but this is Serious Enterprise PHP™ we have Serious Enterprise Tools™ to take care of our code style and PSRs and RFCs and all that – we don’t just type code until it’s pretty. Once you go without them though, you will realise how much time they take up. These distractions take up much more of your day than people realise because those are micro interruptions that are scattered and as such feel inconsequential. You stop thinking about indentation, manually adding commas, or placing things for maximum readability and such. You can type code in one disgusting line, press save, and the result is nicely formatted code. The more you trust Prettier, the more you can stop worrying about formatting altogether. This may sound counter-intuitive but it tremendously reduces friction when writing code. You can pass a few basic options to Prettier (indentation, max width, the basics) but other than that it takes your whole code and reformats it from scratch, disregarding any formatting decision you may have previously taken. Prettier, however, is currently one of the most popular code formatters out there, and it has spread to a lot of different languages already for one simple reason: it gives zero fucks about how you think your code should be formatted. There are code formatters for many languages even Javascript has quite a few of them. Originally from the Javascript ecosystem, Prettier is a code formatter which means it takes your code and makes it – as its name suggests – prettier.
