rector: how to return a define using a $_SERVER element?

37 views Asked by At

I want to change

define('USER','pippo');

with

define('USER', $_SERVER["DB_USER"]);

I am not able to create a new FuncCall with a string as first argument and a array element from "DB_USER" index of $_SERVER ...

Also, I was trying to workaround the problem, but it not works

    if ($node->args[0]->value->value == "USER") {
        if (!$node->args[1]->value instanceof Node\Scalar\String_) {
            return null;
        }
        $node->args[1]->value->value = "\$_SERVER[\"_DB_USER\"]";
        return $node;
    }

What I must reconstuct is this

        expr: Expr_FuncCall(                             
            name: Name(                                  
                parts: array(                            
                    0: define                            
                )                                        
            )                                            
            args: array(                                 
                0: Arg(                                  
                    name: null                           
                    value: Scalar_String(                
                        value: USER                      
                    )                                    
                    byRef: false                         
                    unpack: false                        
                )                                        
                1: Arg(                                  
                    name: null                           
                    value: Expr_ArrayDimFetch(           
                        var: Expr_Variable(              
                            name: _SERVER                
                        )                                
                        dim: Scalar_String(              
                            value: CLAL_ENV_DB_USER      
                        )                                
                    )                                    
                    byRef: false                         
                    unpack: false                        
                )                                        
            )                                            
        )                                                
   

                                             
1

There are 1 answers

0
realtebo On BEST ANSWER

Finally I found the way!!

       return new FuncCall(
            new Node\Name('define'),
            [
                new Node\Arg(
                    new Node\Scalar\String_("USER")
                ),
                new Node\Arg(
                    new Node\Expr\ArrayDimFetch(
                        new Node\Expr\Variable(new Node\Name("_SERVER")),
                        new Node\Scalar\String_("DB_USER")
                    )
                ),
            ]
        );

it does this

-DEFINE("USER","root");
+define('USER', $_SERVER['DB_USER']);