Beautify Your Code Thanks To newPHPbeautifier


I spend a lot of time on developing PHP source code and have to deal with various kind of code from other developers, too. One thing that always drives me bananas are badly formatted code – it is just not readable. While I use the NetBeans IDE for development, there is no possibility to format code automatically. For this reason I created my own PHP beautifier and released it as phpbeautifier package for Composer.

By the way: A blog around the phpbeautifier package with a comparison to other PHP formatters can be found at newPHPbeautifier.

newPHPbeautifier is a PHP beautifier which formats your code – it indents, adds new lines, aligns operators and so on; the most important feature is that it makes your code readable. You can easily see if the code you are reading is written in a good style or not.

newPHPbeautifier is written in Perl and beautifies PHP 4 and PHP 5 sourcecode using a set of rules that can be configured separately.

The blog compares this project with other PHP formatters like PEAR PHP_Beautifier and PHPCodeSniffer – I like the latter to check my code against best practices (and I already use the CSS one for CSS-Files).

There are plenty of PHP source code formatters. newPHPbeautifier comes with an own user interface, but you can also use it with your IDE. In this blog I will present the features of newPHPbeautifier and compare it to other PHP formatters.

If you have any questions, just ask them in the comments – I would be happy to answer.

newPHPbeautifier is a fork of the old PHPbeautifier. The primary aim of PHPbeautifier is to provide a consistent formatting style for PHP code. This makes it easier to read and understand, and it also helps in detecting errors. It also has some added features like adding PHPDoc comments or generating auto-completion code for IDEs.

newPHPbeautifier offers the following features:

* Indenting and aligning of explicit line breaks and block openers (braces)

* Support for modern PHP syntax such as namespaces, closures, etc.

* Support for coding standards like the PEAR one or Zend Framework

* Customizeable formatting with an option to write your own filter

* Automatic generation of PHPDoc comments from @comments in the code and from specially formatted PHPDoc comments in functions’ parameter lists (optional)

* Automatic generation of auto-completion code for IDEs (optional)

You have probably noticed that some of your PHP code is sometimes not easy to read as it is:

-too long lines,

-too many tabs or spaces

-bad indentation.

PHPBeautifier will help you to beautify your code so that it is easy to read. Other similar tools exist, but here I give you the best of the best.

Let’s compare them:

phpbeautifier is a command line tool to beautify PHP code automatically. It is written in PHP and released under the GPL license.

What does it do?

It reindents your code according to some coding style guidelines (PSR1, PSR2, Zend) and adds brackets to control structures.

PHP Beautifier is a tool for beautifying PHP source code. It allows you to configure many aspects of the output and is designed to assist in copying and pasting from the formatted source.

Features


Leave a Reply

Your email address will not be published. Required fields are marked *