nikic / PHP-Parser

A PHP parser written in PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

v4.7.1 When parsing the array, the numeric key name was lost

jiuwuxiaohun opened this issue · comments

The number key 95 is missing from the AST structure, the character key str_key_95 is correct.

function testSessionAndCookie()
{
    $temp_array = array(95 => 'this is 95.','str_key_95'=>'this is str_key_95.');
    echo $temp_array[95]."\n";
    echo $temp_array['str_key_95']."\n";
}
testSessionAndCookie();

AST CODE:

object(PhpParser\Node\Stmt\Function_)#1214 (8) {
  ["attributes":protected]=>
  array(4) {
    ["startLine"]=>
    int(3)
    ["endLine"]=>
    int(8)
    ["dyc_not_encrypt_flag"]=>
    bool(false)
    ["comments"]=>
    array(1) {
      [0]=>
      object(PhpParser\Comment\Doc)#1248 (7) {
        ["text":protected]=>
        string(16) "//@dyc_code_func"
        ["startLine":protected]=>
        int(-1)
        ["startFilePos":protected]=>
        int(-1)
        ["startTokenPos":protected]=>
        int(-1)
        ["endLine":protected]=>
        int(-1)
        ["endFilePos":protected]=>
        int(-1)
        ["endTokenPos":protected]=>
        int(-1)
      }
    }
  }
  ["byRef"]=>
  bool(false)
  ["name"]=>
  object(PhpParser\Node\Identifier)#1191 (2) {
    ["attributes":protected]=>
    array(2) {
      ["startLine"]=>
      int(3)
      ["endLine"]=>
      int(3)
    }
    ["name"]=>
    string(20) "testSessionAndCookie"
  }
  ["params"]=>
  array(0) {
  }
  ["returnType"]=>
  NULL
  ["stmts"]=>
  array(3) {
    [0]=>
    object(PhpParser\Node\Stmt\Expression)#1201 (2) {
      ["attributes":protected]=>
      array(2) {
        ["startLine"]=>
        int(5)
        ["endLine"]=>
        int(5)
      }
      ["expr"]=>
      object(PhpParser\Node\Expr\Assign)#1200 (3) {
        ["attributes":protected]=>
        array(2) {
          ["startLine"]=>
          int(5)
          ["endLine"]=>
          int(5)
        }
        ["var"]=>
        object(PhpParser\Node\Expr\Variable)#1192 (2) {
          ["attributes":protected]=>
          array(2) {
            ["startLine"]=>
            int(5)
            ["endLine"]=>
            int(5)
          }
          ["name"]=>
          string(10) "temp_array"
        }
        ["expr"]=>
        object(PhpParser\Node\Expr\Array_)#1199 (2) {
          ["attributes":protected]=>
          array(0) {
          }
          ["items"]=>
          array(2) {
            [0]=>
            object(PhpParser\Node\Expr\ArrayItem)#1209 (5) {
              ["attributes":protected]=>
              array(0) {
              }
              ["key"]=>
              NULL
              ["value"]=>
              object(PhpParser\Node\Scalar\String_)#1194 (2) {
                ["attributes":protected]=>
                array(0) {
                }
                ["value"]=>
                string(11) "this is 95."
              }
              ["byRef"]=>
              bool(false)
              ["unpack"]=>
              bool(false)
            }
            [1]=>
            object(PhpParser\Node\Expr\ArrayItem)#1240 (5) {
              ["attributes":protected]=>
              array(0) {
              }
              ["key"]=>
              object(PhpParser\Node\Scalar\String_)#1242 (2) {
                ["attributes":protected]=>
                array(0) {
                }
                ["value"]=>
                string(10) "str_key_95"
              }
              ["value"]=>
              object(PhpParser\Node\Scalar\String_)#1241 (2) {
                ["attributes":protected]=>
                array(0) {
                }
                ["value"]=>
                string(19) "this is str_key_95."
              }
              ["byRef"]=>
              bool(false)
              ["unpack"]=>
              bool(false)
            }
          }
        }
      }
    }
    [1]=>
    object(PhpParser\Node\Stmt\Echo_)#1207 (2) {
      ["attributes":protected]=>
      array(2) {
        ["startLine"]=>
        int(6)
        ["endLine"]=>
        int(6)
      }
      ["exprs"]=>
      array(1) {
        [0]=>
        object(PhpParser\Node\Expr\BinaryOp\Concat)#1206 (3) {
          ["attributes":protected]=>
          array(2) {
            ["startLine"]=>
            int(6)
            ["endLine"]=>
            int(6)
          }
          ["left"]=>
          object(PhpParser\Node\Expr\ArrayDimFetch)#1204 (3) {
            ["attributes":protected]=>
            array(2) {
              ["startLine"]=>
              int(6)
              ["endLine"]=>
              int(6)
            }
            ["var"]=>
            object(PhpParser\Node\Expr\Variable)#1202 (2) {
              ["attributes":protected]=>
              array(2) {
                ["startLine"]=>
                int(6)
                ["endLine"]=>
                int(6)
              }
              ["name"]=>
              string(10) "temp_array"
            }
            ["dim"]=>
            object(PhpParser\Node\Scalar\LNumber)#1239 (2) {
              ["attributes":protected]=>
              array(0) {
              }
              ["value"]=>
              int(95)
            }
          }
          ["right"]=>
          object(PhpParser\Node\Scalar\String_)#1195 (2) {
            ["attributes":protected]=>
            array(0) {
            }
            ["value"]=>
            string(1) "
"
          }
        }
      }
    }
    [2]=>
    object(PhpParser\Node\Stmt\Echo_)#1213 (2) {
      ["attributes":protected]=>
      array(2) {
        ["startLine"]=>
        int(7)
        ["endLine"]=>
        int(7)
      }
      ["exprs"]=>
      array(1) {
        [0]=>
        object(PhpParser\Node\Expr\BinaryOp\Concat)#1212 (3) {
          ["attributes":protected]=>
          array(2) {
            ["startLine"]=>
            int(7)
            ["endLine"]=>
            int(7)
          }
          ["left"]=>
          object(PhpParser\Node\Expr\ArrayDimFetch)#1210 (3) {
            ["attributes":protected]=>
            array(2) {
              ["startLine"]=>
              int(7)
              ["endLine"]=>
              int(7)
            }
            ["var"]=>
            object(PhpParser\Node\Expr\Variable)#1208 (2) {
              ["attributes":protected]=>
              array(2) {
                ["startLine"]=>
                int(7)
                ["endLine"]=>
                int(7)
              }
              ["name"]=>
              string(10) "temp_array"
            }
            ["dim"]=>
            object(PhpParser\Node\Scalar\String_)#1243 (2) {
              ["attributes":protected]=>
              array(0) {
              }
              ["value"]=>
              string(10) "str_key_95"
            }
          }
          ["right"]=>
          object(PhpParser\Node\Scalar\String_)#1203 (2) {
            ["attributes":protected]=>
            array(0) {
            }
            ["value"]=>
            string(1) "
"
          }
        }
      }
    }
  }
  ["attrGroups"]=>
  array(0) {
  }
  ["namespacedName"]=>
  NULL
}

Using php-parse tests is correct again. What's going on?

(base) ➜ app git:(master) ✗ ../vendor/bin/php-parse test.php

==> Node dump:

array(
    0: Stmt_Function(
        attrGroups: array(
        )
        byRef: false
        name: Identifier(
            name: testSessionAndCookie
        )
        params: array(
        )
        returnType: null
        stmts: array(
            0: Stmt_Expression(
                expr: Expr_Assign(
                    var: Expr_Variable(
                        name: temp_array
                    )
                    expr: Expr_Array(
                        items: array(
                            0: Expr_ArrayItem(
                                key: Scalar_LNumber(
                                    value: 95
                                )
                                value: Scalar_String(
                                    value: this is 95.
                                )
                                byRef: false
                                unpack: false
                            )
                            1: Expr_ArrayItem(
                                key: Scalar_String(
                                    value: str_key_95
                                )
                                value: Scalar_String(
                                    value: this is str_key_95.
                                )
                                byRef: false
                                unpack: false
                            )
                        )
                    )
                )
            )
            1: Stmt_Echo(
                exprs: array(
                    0: Expr_BinaryOp_Concat(
                        left: Expr_ArrayDimFetch(
                            var: Expr_Variable(
                                name: temp_array
                            )
                            dim: Scalar_LNumber(
                                value: 95
                            )
                        )
                        right: Scalar_String(
                            value: 
                        
                        )
                    )
                )
            )
            2: Stmt_Echo(
                exprs: array(
                    0: Expr_BinaryOp_Concat(
                        left: Expr_ArrayDimFetch(
                            var: Expr_Variable(
                                name: temp_array
                            )
                            dim: Scalar_String(
                                value: str_key_95
                            )
                        )
                        right: Scalar_String(
                            value: 
                        
                        )
                    )
                )
            )
        )
    )
    1: Stmt_Expression(
        expr: Expr_FuncCall(
            name: Name(
                parts: array(
                    0: testSessionAndCookie
                )
            )
            args: array(
            )
        )
    )
)

Thanks for providing this library, it is very useful.

Sorry, it's my fault. My previous code cleaned up the key for this number.