I wrote three filters for PHP Beautifier, a tool that reformats and tidies PHP scripts, which made it much more useful for me.
  • KeepEmptyLines keeps an empty line wherever one or more empty lines are encountered in the source.
  • BreakLongLists breaks argument lists and arrays into multiple lines, but only if on a single line they would be too long. It has one parameter, maxlen, with which this limit can be set; the default is 70 characters.

    For example, it can turn this:

    function dbexec($database_handle, $sql_query='', $bind_values=array(), $callback=false, $return_modified_rows=false){
      // Magic happens here
    }
    
    dbexec($my_database_handle, 'select Firstname from Users where Surname=? and Stamp>? and Active=? and ID in (?)',
       array('Saunders-Smith', $now-60*60*24*7*2, $searching_for_active, array(
          1,
          2,
          3,
          4,
          5
    )), function($row){ print $row['Firstname']."\n"; });
    

    into this:

    function dbexec(
       $database_handle,
       $sql_query = '',
       $bind_values = array(),
       $callback = false,
       $return_modified_rows = false
    ) {
       // Magic happens here
    
    }
    
    dbexec(
       $my_database_handle,
       'select Firstname from Users where Surname=? and Stamp>? and Active=? and ID in (?)',
       array(
          'Saunders-Smith',
          $now - 60 * 60 * 24 * 7 * 2,
          $searching_for_active,
          array(1, 2, 3, 4, 5)
       ),
       function ($row) {
          print $row['Firstname'] . "\n";
       }
    );
    
  • SpaceOpAssignments adds a single space before assignment operators that contain another operator like plus-equal or dot-equal.

Installation

The filters are now part of the PHP Beautifier GitHub repo - the master branch, but not the latest release. Simply install PHP Beautifier using the latest commit.

  • Alternatively, download the filters you'd like to use:
  • Copy the files into your PHP_Beautifier installation, into the Beautifier/Filter folder. This might be, for example, at /usr/share/php/PHP/Beautifier/Filter.
  • The filters are ready to use - simply include them in the php_beautifier command line as --filters="BreakLongLists(maxlen=70) KeepEmptyLines SpaceOpAssignments".