phpindentationphp-beautifier

Suppressing next token in PHP_Beautify


I'm using the PEAR PHP_Beautifier to try and format a large volume of legacy code, and to achieve a specific formatting of array elements.

I've written a custom filter:

class PHP_Beautifier_Filter_ArrayIndent extends PHP_Beautifier_Filter
{
    public function t_array($sTag)
    {
        if (($this->oBeaut->getNextTokenContent(1) === '(') &&
            ($this->oBeaut->getNextTokenContent(2) !== ')')) {
            //    Don't use for Array type hinting
            //    Don't use for Empty array definition
            $this->oBeaut->add($sTag);
            $this->oBeaut->addNewlineIndent();
        } elseif ($this->oBeaut->getNextTokenContent(2) !== ')') {
             //    Ensure a space after type hinted array before argument name
             $this->oBeaut->add($sTag . ' ');
        } else {
             //  Empty array definition
             $this->oBeaut->add($sTag);
        }
    }
}

I'm trying to get the following format (with the opening brace on the same line as the "array"):

public function doSomething(array $params = array()) {
    $dummy1 = array();
    $dummy2 = array (
        'Hello' => 'World',
        'Goodnight' => 'Vienna'
    );
}

but I'm getting:

public function doSomething(array $params = array()) {
    $dummy1 = array();
    $dummy2 = array 
    (
        'Hello' => 'World',
        'Goodnight' => 'Vienna'
    );
}

Is there any way of modifying/suppressing the next token after "t_array", or skipping it so that I can handle the opening brace within my filter?


Solution

  • Wouldn't this line $this->oBeaut->addNewlineIndent(); be what is causing the new line, shouldn't it be removed or changed to a different method?

    Suggestion:

    <?php
    if (($this->oBeaut->getNextTokenContent(1) === '(') && ($this->oBeaut->getNextTokenContent(2) !== ')')) { 
        $this->oBeaut->add($sTag); 
    } elseif ($this->oBeaut->getNextTokenContent(2) !== ')') {
        // Ensure a space after type hinted array before argument name 
        $this->oBeaut->add($sTag . ' '); 
    } else {
        // Empty array definition 
        $this->oBeaut->add($sTag); 
    }