One of the biggest hurdles that I’ve experienced when working with pull requests in some of the projects I have on GitHub is the use of tabs and spaces and how they impact how code appears on the landing page.

Naturally, code should be well-formatted and easy to read not only for those who are simply investigating the project but also who are contributing to it.

Though I’ve shared a few plugins specifically for Coda on how to automate this, but we all use different editors, have different formatting techniques, and so on. The thing is, there’s not much we can do about how others configure their editor.

But here’s the thing: I think that if you’re the one in charge of a project, you have the responsibility of making sure that all merged code is easy to read and easy to manage.

Just as we have linters for various languages, we also have code formatters for different languages. Last week, I discovered PHP Formatter which is a really solid web application that serves as a solid, ahem, PHP code formatter.

PHP Code Formatter

PHP Formatter is really easy to use as the application basically offers three main features:

  1. Input
  2. Style
  3. Output
PHP Formatter

The PHP Formatter Input Screen

Input is clear enough, isn’t it? Simply paste in your PHP source code that you want to format.

PHP Formatter Style

Selecting various styles and configurations for formatting.

The Style tab allows you to select from a set of predefined indentation styles. Namely:

  1. K&R
  2. BSD
  3. GNU
  4. PEAR

You can also set how many spaces are considered for an indentation, as well as specific settings for improvement, brackets, and operators.

PHP Formatter

The result of processing input with PHP Formatter.

Once the processing is done, you’re presented with the formatted code. If you don’t like what you see, you can easily go back and tweak the styles then reprocess the source code.

From this point, you have the ability to copy the contents of the formatter to your clipboard and/or download the source code.

What’s The Advantage?

I actually stumbled across this application whenever I was working on a project last week. I was merging a number of different pull requests into a project, and it was evident that several of us had our IDE’s configured differently.

I didn’t want to have the public display of the code look bad, so I was looking for an easy way to format the code. Thus, I found this utility.

Ultimately, if you’re responsible for maintaining a project, you’re responsible for overseeing that the conventions are being followed. Sure, you may miss a few things here or there (what are bug reports or issues for? :)), but I think that we have a responsibility as much as we can to make sure our code is cleanly formatted.

So if you’re in the business of trying to maintaing coding conventions for yourself and/or a team based project – be it an open source project or something for work – PHP Formatter may be useful.

Category:
Resources

Join the conversation! 2 Comments

  1. You have to trust that two guys if you put your code inside their hands!

    • I don’t think that running our code through a formatter absolves us from double-checking to make sure that things are clean once they are returned to us.

      Secondly, you’re right in that you gotta be careful if you’re going to be sending your code across the wire. I didn’t see anything that said they didn’t keep the code, so that’s a call we’re all responsible for making.

      Good point, Somtam.

Leave a Reply