Sunday, September 29, 2013

HOWTO easy wrapper for php code beautifier / formatter

I wanted a tool that formatted php from the command line. phpstylist does what I want, but it is tedious to supply the options each time you run it. Below is a wrapper so you can set default options.

To install:

  1. Save phpstylist as /usr/local/bin/phpStylist.php
  2. Save the script below as /usr/local/bin/pstyle
  3. Edit /usr/local/bin/pstyle
    1. set MAKE_BACKUP='yes' , if you want to keep the original unformulated files
    2. set MAKE_HTML to a lower case 'yes or 'no' make an html copy of the formatted file
    3. un-comment the # STYLIST_OPTIONS+= lines that contain the php formatting options you want
  4. Be sure /usr/local/bin/ is in your $PATH

To run:

       pstyle phpFile.php
or:
       pstyle phpFile1.php phpFile2.php phpFile3.php 

#!/bin/bash
# wrapper for http://sourceforge.net/projects/phpstylist/
# by dan@omacneil.org

  set -u # halt on undefined BASH variables
  set -e # halt if command doesn't return 'no error' aka 0

STYLIST_CMD='/usr/local/bin/phpStylist.php'

MAKE_BACKUP='yes'  # lower case 'yes' or 'no', backup the php file before formatting it
MAKE_HTML='yes'    # lower case 'yes or 'no'   make an html copy of the formatted file

STYLIST_OPTIONS=""

# uncomment/edit options you want
# see php -f phpStylist.php for more options
# --------------------

# Indentation and General Formatting:

#   STYLIST_OPTIONS+=" --indent_size 4  "      # 4 characters per indentation level
#   STYLIST_OPTIONS+="--indent_with_tabs          "      # Indent with tabs instead of spaces
#   STYLIST_OPTIONS+="--keep_redundant_lines      "      # Keep redundant lines
#   STYLIST_OPTIONS+="--space_inside_parentheses  "      # Space inside parentheses
#   STYLIST_OPTIONS+="--space_outside_parentheses "      # Space outside parentheses
#   STYLIST_OPTIONS+="--space_after_comma         "      # Space after comma

# Operators:
#   STYLIST_OPTIONS+="--space_around_assignment     "    # Space around = .= += -= *= /= <<<
#   STYLIST_OPTIONS+="--align_var_assignment        "    # Align block +3 assigned variables
#   STYLIST_OPTIONS+="--space_around_comparison     "    # Space around == === != !== > >= < <=
#   STYLIST_OPTIONS+="--space_around_arithmetic     "    # Space around - + * / %
#   STYLIST_OPTIONS+="--space_around_logical        "    # Space around && || AND OR XOR << >>
#   STYLIST_OPTIONS+="--space_around_colon_question "    # Space around ? :
                                                        
#Functions, Classes and Objects:                        
#   STYLIST_OPTIONS+="--line_before_function         "   # Blank line before keyword
#   STYLIST_OPTIONS+="--line_before_curly_function   "   # Opening bracket on next line
#   STYLIST_OPTIONS+="--line_after_curly_function    "   # Blank line below opening bracket
#   STYLIST_OPTIONS+="--space_around_obj_operator    "   # Space around ->
#   STYLIST_OPTIONS+="--space_around_double_colon    "   # Space around ::

# Control Structures:
#   STYLIST_OPTIONS+="--space_after_if               "    # Space between keyword and opening parentheses
#   STYLIST_OPTIONS+="--else_along_curly             "    # Keep else/elseif along with bracket
#   STYLIST_OPTIONS+="--line_before_curly            "    # Opening bracket on next line
#   STYLIST_OPTIONS+="--add_missing_braces           "    # Add missing brackets to single line structs
#   STYLIST_OPTIONS+="--line_after_break             "    # Blank line after case "break"
#   STYLIST_OPTIONS+="--space_inside_for             "    # Space between "for" elements
#   STYLIST_OPTIONS+="--indent_case                  "    # Extra indent for "Case" and "Default"

#Arrays and Concatenation:
#   STYLIST_OPTIONS+="--line_before_array            "    # Opening array parentheses on next line
#   STYLIST_OPTIONS+="--vertical_array               "    # Non-empty arrays as vertical block
#   STYLIST_OPTIONS+="--align_array_assignment       "    # Align block +3 assigned array elements
#   STYLIST_OPTIONS+="--space_around_double_arrow    "    # Space around double arrow
#   STYLIST_OPTIONS+="--vertical_concat              "    # Concatenation as vertical block
#   STYLIST_OPTIONS+="--space_around_concat          "    # Space around concat elements

# Comments:
#   STYLIST_OPTIONS+="--line_before_comment_multi    "    # Blank line before multi-line comment (/*)
#   STYLIST_OPTIONS+="--line_after_comment_multi     "    # Blank line after multi-line comment (/*)
#   STYLIST_OPTIONS+="--line_before_comment          "    # Blank line before single line comments (//)
#   STYLIST_OPTIONS+="--line_after_comment           "    # Blank line after single line comments (//)


# provide help for bad args 
if [[ $# -eq 0  ]]; then
  echo '  Syntax is: pstyle <php file to style> [php file to style ] [..] '
  exit 1 
fi  

# did we copy phpStylist.php to right place
if [[ ! -r $STYLIST_CMD  ]]; then
  echo "$STYLIST_CMD is missing or unreadable"
  exit 1
fi

OUT_FILE="/tmp/$$_pstyle.tmp"

for file_to_style in "$@"
do
    MAKE_BACKUP=${MAKE_BACKUP,,}       # make it lower case
    if [[ $MAKE_BACKUP=='yes' ]]; then
        cp "$1" "$1.bak" 
    fi

    CMD="php  -f $STYLIST_CMD $file_to_style $STYLIST_OPTIONS"
    $CMD > "$OUT_FILE"

    mv "$OUT_FILE" "$file_to_style"
 
    if [[ $MAKE_HTML=='yes' ]]; then
      php -s "$file_to_style" > "$file_to_style.html"
    fi
done