首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PHP-Parser中隐藏属性

如何在PHP-Parser中隐藏属性
EN

Stack Overflow用户
提问于 2020-04-17 02:46:42
回答 1查看 66关注 0票数 1

我正在使用PHP-Parser来解析PHP代码,并获得Json格式的代码。我想问一下如何获得没有属性( startLine,endLine,comment等)的结果。

EN

回答 1

Stack Overflow用户

发布于 2020-10-13 11:17:15

虽然在计算上非常昂贵,但您可以递归遍历输出并删除这些属性。

另一种可能更简单的方法是扩展一个已实现的Php Parser类,并覆盖protected function createCommentNopAttributes(array $comments) 以返回所需的属性,或者在本例中不返回任何属性。

例如,假设我们打算解析Php7代码,我们可以创建自己的自定义解析器

代码语言:javascript
复制
class MyCustomPhp7Parser extends \PhpParser\Parser\Php7 {
   //no need to implement the other methods as we are inheriting from a concrete class
   
   //override method inherited from https://github.com/nikic/PHP-Parser/blob/master/lib/PhpParser/ParserAbstract.php
   protected function createCommentNopAttributes(array $comments) {
       $attributes = ['comments' => []]; //return data as desired
       return $attributes;
   }
} 

然后,您可以使用自定义解析器实现

代码语言:javascript
复制
<?php

use PhpParser\ParserFactory;

$code = <<<'CODE'
<?php

/** @param string $msg */
function printLine($msg) {
    echo $msg, "\n";
}
CODE;

$parser = new MyCustomPhp7Parser(
   new \PhpParser\Lexer\Emulative(), //desired lexer
   [] //additional options
);

try {
    $stmts = $parser->parse($code);

    echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
} catch (PhpParser\Error $e) {
    echo 'Parse Error: ', $e->getMessage();
}

检索

代码语言:javascript
复制
[
    {
        "nodeType": "Stmt_Function",
        "byRef": false,
        "name": {
            "nodeType": "Identifier",
            "name": "printLine",
            "attributes": {
                "startLine": 4,
                "endLine": 4
            }
        },
        "params": [
            {
                "nodeType": "Param",
                "type": null,
                "byRef": false,
                "variadic": false,
                "var": {
                    "nodeType": "Expr_Variable",
                    "name": "msg",
                    "attributes": {
                        "startLine": 4,
                        "endLine": 4
                    }
                },
                "default": null,
                "attributes": {
                    "startLine": 4,
                    "endLine": 4
                }
            }
        ],
        "returnType": null,
        "stmts": [
            {
                "nodeType": "Stmt_Echo",
                "exprs": [
                    {
                        "nodeType": "Expr_Variable",
                        "name": "msg",
                        "attributes": {
                            "startLine": 5,
                            "endLine": 5
                        }
                    },
                    {
                        "nodeType": "Scalar_String",
                        "value": "\n",
                        "attributes": {
                            "startLine": 5,
                            "endLine": 5,
                            "kind": 2
                        }
                    }
                ],
                "attributes": {
                    "startLine": 5,
                    "endLine": 5
                }
            }
        ],
        "attributes": {
            "comments": [
                
            ],
   
        }
    }
]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61257751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档