z7zmey / php-parser

PHP parser written in Go

Home Page:https://php-parser.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHPDoc is attributed to a wrong node

YuriyNasretdinov opened this issue · comments

PHPDoc is sometimes attributed to a wrong node. Example:

<?php
/** Some phpdoc */
$a = 1;
function wrong_phpdoc() {}

It yields the following structure:

[*node.Root]
  "Stmts":
    [*stmt.Expression]
      "Expr":
        [*assign.Assign]
          "Variable":
            [*expr.Variable]
              "VarName":
                [*node.Identifier]
                  "Value": a;
          "Expression":
            [*scalar.Lnumber]
              "Value": 1;
    [*stmt.Function]
      "NamespacedName": wrong_phpdoc;
      "ReturnsRef": false;
      "PhpDocComment": /** Some phpdoc */;
      "FunctionName":
        [*node.Identifier]
          "Value": wrong_phpdoc;
      "Stmts":

As you can see, PHPDoc here is attributed to a function while the correct position would be the assignment.

Yes it's weird, but it works same as in PHP sources

<?php
/** Some phpdoc */
$a = 1;
function wrong_phpdoc() {}

$refFunc = new ReflectionFunction('wrong_phpdoc');
var_dump($refFunc->getDocComment());

// output:
// string(18) "/** Some phpdoc */"

I am working at saving free floating comments, but it still in progress
If you checkout to dev branch, you can call {SomeNode}.GetMeta().FindBy(meta.TypeFilter(meta.CommentType)) to get node comments.
But in the example, comment /** Some phpdoc */ will be attached to *expr.Variable node not to *stmt.Expression, I want to implement attaching comments to top-level nodes.

Nice :). I did not try it on vanilla PHP :). Comments parsing works properly even in master branch, so perhaps no need to fix it then :). Thank you!