PEAR::PHP_Beautifier

id:oppara:20070604

現実逃避中

before:

<?php
CLASS Hoge{

    FUNCTION foo($bar) {
        $a= array('ky'=>array('orz','www'));
        if ((true)||(1)) { return false; }
    }
    function baz() {
        foreach (func_get_args() as $param) {
            //aa
            if (!is_array($param)) {
                return false;
            }else { return true;}
        }
    }       
}

after:

<?php
class Hoge 
{           
     function foo( $bar ) {
        $a = array( 
            'ky' => array(
                'orz',
                'www'
             )
        );
         if ( ( true )  || ( 1 ) ) {
            
             return false;
        }
    }
     function baz() {
         foreach ( func_get_args() as $param ) {
            //aa
             if ( !is_array( $param ) ) {
              
                 return false;
            }
             else {

                 return true;
            }
        }
    }
}

php_beautifier

#!/bin/sh

pb="/path/to/php_beautifier"
fd="/path/to/filter_dir"

$pb  --filters "Lowercase()" -d $fd -l "Pear() NewLines(before=T_RETURN) My()" "$@" 2>/dev/null


My.filter.php

<?php
 class PHP_Beautifier_Filter_My extends PHP_Beautifier_Filter {
    protected $sDescription = 'My Filter for PHP_Beautifier';
    public  function off() {
    }
    public  function t_access( $sTag ) {
        $this->oBeaut->add( $sTag . ' ' );
    }
    public  function t_end_heredoc( $sTag ) {
        $this->oBeaut->add( trim( $sTag ) );
        $this->oBeaut->addNewLineIndent();
    }
    public  function t_open_tag( $sTag ) {
        $this->oBeaut->add( trim( $sTag ) );
        preg_match( "/([\s\r\n\t]+)$/", $sTag, $aMatch );
        $aNextToken = $this->oBeaut->getToken( $this->oBeaut->iCount+1 );
        $sNextWhitespace = ( $aNextToken[0] == T_WHITESPACE ) ? $aNextToken[1] : '';
        $sWhitespace = @$aMatch[1] . $sNextWhitespace;
         if ( preg_match( "/[\r\n]+/", $sWhitespace ) ) {
            $this->oBeaut->addNewLineIndent();
        }
         else {
            $this->oBeaut->add( " " );
        }
    }
     public  function t_close_tag( $sTag ) {
        $this->oBeaut->removeWhitespace();
         if ( preg_match( "/\r|\n/", $this->oBeaut->getPreviousWhitespace() ) ) {
            $this->oBeaut->addNewLine();
            $this->oBeaut->add( $sTag );
        }
         else {
            $this->oBeaut->add( " " . $sTag );
        }
    }
    // PHP_Beautifier_Filter_ArrayNesteのソースを追加
    public  function t_parenthesis_open( $sTag ) {
        $token = $this->oBeaut->getNextTokenConstant();
         if ( $token == ')' ) {
            $this->oBeaut->add( $sTag . '' );
        }
         else {
            $this->oBeaut->add( $sTag . ' ' );
        }
         if ( $this->oBeaut->getControlParenthesis() == T_ARRAY && $token != ')' ) {
            $this->oBeaut->addNewLine();
            $this->oBeaut->incIndent();
            $this->oBeaut->addIndent();
        }
    }
    public  function t_parenthesis_close( $sTag ) {
        $token = $this->oBeaut->getPreviousTokenConstant();
         if ( $this->oBeaut->getControlParenthesis() == T_ARRAY && $token != '(' ) {
            $this->oBeaut->decIndent();
             if ( $this->oBeaut->getPreviousTokenContent() != '(' ) {
                $this->oBeaut->addNewLine();
                $this->oBeaut->addIndent();
            }
        }
         if ( $token == '(' or $token == ')' ) {
            $this->oBeaut->add( $sTag . ' ' );
        }
         else {
            $this->oBeaut->add( ' ' . $sTag . ' ' );
        }
    }
    public  function t_comma( $sTag ) {
         if ( $this->oBeaut->getControlParenthesis() != T_ARRAY ) {
            $this->oBeaut->add( $sTag . ' ' );
        }
         else {
            $this->oBeaut->add( $sTag );
            $this->oBeaut->addNewLine();
            $this->oBeaut->addIndent();
        }
    }
     public  function t_open_brace( $sTag ) {
         if ( $this->oBeaut->isPreviousTokenConstant( T_VARIABLE )
                or $this->oBeaut->isPreviousTokenConstant( T_OBJECT_OPERATOR )
                or ( $this->oBeaut->isPreviousTokenConstant( T_STRING )
                        and $this->oBeaut->getPreviousTokenConstant( 2 ) == T_OBJECT_OPERATOR )
                or $this->oBeaut->getMode( 'double_quote' ) ) {
            $this->add( $sTag );
        }
         else {
            $token = $this->oBeaut->isPreviousTokenConstant( T_STRING );
            $token = $this->oBeaut->getPreviousTokenConstant( 2 );
            // class, interface, abstract BSD style
             if ( $token == T_CLASS or $token == T_INTERFACE or $token == T_ABSTRACT ) {
                $this->oBeaut->addNewLineIndent();
                $this->oBeaut->add( $sTag );
                $this->oBeaut->incIndent();
                $this->oBeaut->addNewLineIndent();
            }
             else {
                 if ( $this->oBeaut->removeWhiteSpace() ) {
                    $this->oBeaut->add( ' ' . $sTag );
                }
                 else {
                    $this->oBeaut->add( $sTag );
                }
                $this->oBeaut->incIndent();
                 if ( $this->oBeaut->getControlSeq() == T_SWITCH ) {
                    $this->oBeaut->incIndent();
                }
                $this->oBeaut->addNewLineIndent();
            }
        }
    }
    public   function t_close_brace( $sTag ) {
         if ( $this->oBeaut->getMode( 'string_index' )
                or $this->oBeaut->getMode( 'double_quote' ) ) {
            $this->oBeaut->add( $sTag );
        }
         else {
            $this->oBeaut->removeWhitespace();
            $this->oBeaut->decIndent();
             if ( $this->oBeaut->getControlSeq() == T_SWITCH ) {
                $this->oBeaut->decIndent();
            }
            $this->oBeaut->addNewLineIndent();
            $this->oBeaut->add( $sTag );
            $this->oBeaut->addNewLineIndent();
        }
    }
    public   function t_whitespace( $sTag ) {
         if ( preg_match( "/\r|\n/", $sTag )  
                && preg_match( "/\r|\n/", $this->oBeaut->getPreviousWhitespace() )  
                && $this->oBeaut->getPreviousTokenConstant() == T_COMMENT ) {
            $this->oBeaut->addNewLine();
        }
    }
    public   function t_doc_comment( $sTag ) {
        $this->oBeaut->addNewLineIndent();
        // process doc
        preg_match( "/(\/\*\*[^\r\n]*)(.*?)(\*\/)/sm", $sTag, $aMatch );
        $sDoc = $aMatch[2];
        // if is a one-line-doc, leave as-is
         if ( !preg_match( "/\r\n|\r|\n/", $sDoc ) ) {
            $this->add( $sTag );
            $this->oBeaut->addNewLineIndent();
        }
         else { // is a multi line doc...
            $aLines = preg_split( "/\r\n|\r|\n/", $sDoc );
            $this->oBeaut->add( $aMatch[1] );
             foreach ( $aLines as $sLine ) {
                 if ( $sLine = trim( $sLine ) ) {
                    $this->oBeaut->addNewLineIndent();
                    $this->oBeaut->add( " " . $sLine );
                }
            }
            $this->oBeaut->addNewLineIndent();
            $this->oBeaut->add( " " . $aMatch[3] );
            $this->oBeaut->addNewLineIndent();
        }
    }
}
?>