PEAR::PHP_Beautifier

id:oppara:20070501

やっぱ気に入らないので適当に修正
PEAR::PHP_Beautifierのversionは、0.1.13

  • php_beautifierの修正

try{}内、$aFiltersDirectoryの値がなんでか消えてしまうので、$aFiltersのチェックの前に$aFiltersDirectoryをチェックするように修正。

  • おれおれフィルターの作成

My.filter.php

<?php
class PHP_Beautifier_Filter_My extends PHP_Beautifier_Filter
{
    protected $sDescription = 'My Filter for PHP_Beautifier';
    function t_parenthesis_open($sTag) 
    {
        $this->oBeaut->add($sTag . ' ');
    }
    function t_parenthesis_close($sTag) 
    {
        if ($this->oBeaut->getPreviousTokenConstant() != T_COMMENT) {
            $this->oBeaut->removeWhitespace();
        }
        $this->oBeaut->add(' ' . $sTag . ' ');
    }
    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();
        } 
    }
    function t_doc_comment($sTag) 
    {
// //         $this->oBeaut->removeWhiteSpace();
        $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();
        }
    }

    function comment_short($sTag) 
    {
        if ($this->oBeaut->getPreviousTokenConstant() != T_COMMENT) {
            $this->oBeaut->addNewLineIndent();
        }
        $this->oBeaut->add(trim($sTag));
        $this->oBeaut->addNewLineIndent();
    }
}
?>
  • php_beautifierのラッパー
#!/bin/sh

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

$pb -d $fd -l "Pear() NewLines(before=if:switch:T_CLASS:T_RETURN) My()" "$@"