How to create a new line above one being catched?

67 Views Asked by At

I have this

$last_data=date("Y-m-d",strtotime(mysql_result($res,0,0)));

I need to do a first step of refactoring

$tmp_date_string=mysql_result($res,0,0));
$last_data=date("Y-m-d",strtotime($tmp_date_string);

Can rector create a new line above the one being considered? If yes, how ?

1

There are 1 best solutions below

0
On BEST ANSWER

Found !

asking node collector to add a new node before the one being parsed, actually is smart enough to appende into a new line before the line containing the node being parsed !

public function refactor(Node $node)
{
    if (!$this->isName($node->name, 'strtotime')) {
        return null;
    }

    if (!isset($node->args)) {
        return null;
    }

    if (count($node->args) != 1) {
        return null;
    }

    $arg_0 = $node->args[0];
    if (!$arg_0->value instanceof FuncCall) {
        // echo "Arg 0 value is not a FuncCall :" . PHP_EOL . get_class($arg_0) . PHP_EOL;
        return null;
    }

    $internal_func_call = $arg_0->value;

    if (!$this->isName($internal_func_call, 'mysql_result')) {
        return null;
    }

    $new_argo_0 =  new Arg(
        new Variable('date_as_string')
    );
    $node->args[0] = $new_argo_0;

    $date_str_variable = new Variable('date_as_string');

    $new_assign_node = new Assign($date_str_variable, $internal_func_call);

    $this->nodesToAddCollector->addNodeBeforeNode(
        $new_assign_node,
        $node,
    );

    return $node;
}