Without purchasing Zend Studio or another commercial PHP IDE, Eclipse with PDT and PDT Tools seems to be the compelling solution. PDT Tools (pdt_tools) is a separate package that provides utilities like code formatting for PHP. The pdt_tools package for Eclipse is still available for download. However, according the project page on SourceForge JP, the pdt_tools formatter subproject is "concluded". Some of the code formatting was not aligned with my preferences, so I took a closer look at the plugin.
The first step was accessing the source code. Two options are located below, including the actual source used to create the package zip file. The "another developer" version appears to be for a previous version of Eclipse, so the "original developer" code is probably what you will need. As I dug into the code, I finally figured out that the module does not actually contain very much of the formatting logic. Basically, it leverages the Eclipse CodeFormatter classes (see references). This module appears to tokenize the PHP and map it to standard C-style constructs that the Eclipse CodeFormatter can then format. The result is that the majority of the module is simply UI for configurations and that adding new formatting options is comparatively quite difficult. The only formatting logic that appears to be directly implemented relates to alignment.
One of my reasons for researching is that I prefer two divergent styles of code formatting for array initializers. In the end, I concluded that I could not have it both ways without significant customization:
// One-line for ordered arrays $arr = array(1, 2, 3); // Multi-line for hashes $hash = array( 'key' => 'value', );
Regardless, I hope that these preliminary notes help anyone else looking to customize the code formatter.
- From the original developer: CodeFormatter (git) - Tools for PDT (PHP Development Tools) - SourceForge.JP
- From another developer that forked an old version: Eclipse-PHP-Formatter/src/jp/sourceforge/pdt_tools/formatter at master · dmeybohm/Eclipse-PHP-Formatter · GitHub
- Leverages the underlying Eclipse CodeFormatter: Eclipse Platform Package Browser