Command line formatter for PHP and Javascript
Asked Answered
O

3

7

Are there any command line (not interactive, not web-based) tools that will reformat (pretty-print) PHP and Javascript code?

Overhaul answered 8/8, 2009 at 16:57 Comment(0)
M
8

PHP_ Beautifier is very outdated! For a more modern approach try php-cs-fixer

Example command: php-cs-fixer fix --rules=@PSR2 path/to/php/src

A second alternative is PHP_CodeSniffer

Example command: phpcbf --standard=PSR2 path/to/php/src

Maritzamariupol answered 18/7, 2018 at 15:52 Comment(2)
As of 2024, php-cs-fixer only handles basic things like indentation. If you have truly horrible code with things like inconsistent spacing around brackets, it won't fix that.Prepared
For PHP, there's also pretty-php -- quite modern and actively maintained.Subsume
N
3

For a PHP beautifier, you can also use this pear package PHP_Beautyfier, which can either be called from the command line or instantiated within your own PHP code. For the Javascript one instead, find an open discussion here

Nosing answered 8/8, 2009 at 17:13 Comment(1)
This is still beta, and hasn't been updated since 2010, with a number of unresolved bugs.Prepared
B
1

I wrote an article explaining how to build a command-line JavaScript beautifier implemented in JavaScript. The best part - it takes under 5 minutes to do it, YMMV.

Cheers!
Shonzilla

Blalock answered 31/10, 2010 at 22:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.