After more than 200 merged pull requests since mid December 2017, we're happy to announce the first alpha release of Prettier for PHP. In this blog post, we'd like to give a short overview of how the plugin works, its philosophy, and what to expect in the future.
How does it work?
Adding support for a new language to Prettier requires two things:
A printer, that turns the AST into a special format called
docthat Prettier requires to do its magic. It is basically your source code plus some information about where linebreaks can be added, how it should be indented, and so on.
While we could benefit from existing work on the parsing side, the printer had to be developed from scratch to support all of PHP's various AST node types. Our first alpha release today means that we are confident that the printer can re-print your code without breaking it.
If you'd like to read more about the plugin API, see the docs.
When building a code formatter, it can be tempting to add many options to account for all the different code styles there are. The PHP plugin sticks to Prettier's opinionated philosophy and doesn't add any new options. Instead, we identified PSR-1, PSR-2 and PSR-12 as one of the most popular coding styles in the PHP community, and decided to use this as our guide when it came to formatting our re-printed PHP code.
For our first stable release, the following milestones still have to be reached:
100% safety that we're not breaking code during formatting.
We're quite close to this goal already today.
The plugin doesn't deserve its name if the output isn't pretty. While it is already quite good today, handling all the edge cases will require some more work.
Today, you can try the plugin from the command line by following the instructions in the README. Plugin support in your favorite editor might not work yet, though (see the related issues for VS Code, Atom and Vim). Also, the PHP plugin is not integrated in Prettier's playground yet.
Are you excited about Prettier for PHP? Then we'd love you to help us reach our first stable release by