4

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?

durron597
  • 31,968
  • 17
  • 99
  • 158
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • I don't know anything about the beautifier, but 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? – Get Off My Lawn Dec 29 '12 at 18:26
  • Unfortunately, using a straight $this->oBeaut->addIndent(); keeps the "(" on the same line as "array" but gives me a long (6 spaces) indent between "array" and "("... without adding an indent method call, I don't get any indentation of the array values – Mark Baker Dec 29 '12 at 18:54
  • 1
    Though you've made me think: and looking again at what I've done, 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); } actually gives me what I need... post as an answer for the bounty – Mark Baker Dec 29 '12 at 19:05

1 Answers1

1

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); 
}
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338