Code Monkey home page Code Monkey logo

phpfunctionparser's Introduction

PHPFunctionParser

本项目中提供了一个PHP类,用于静态解析PHP源代码文件,将源代码中所定义的所有函数或类方法解析出来,以数组的形式输出。解析出的内容包括函数名(方法名),起始行号,终止行号。可以很方便的运用在项目中。

使用方法

在test目录下的test.php脚本中给出了使用的例子。该类可能会抛出RuntimeException,所以使用时记得将其包含在try catch语句中。

本例中,test.php内容如下

<?php
    require_once("../PHPFuncParser.php");

    $content = file_get_contents("sourcefile.php");
    
    try {
        $parser = new PHPFuncParser($content);
        $result = $parser -> process();
        print_r($result);
    } catch (RuntimeException $e) {
        print($e->getMessage());
        exit(1);
    }

运行test.php可以得到如下结果

Array
(
    [A::__construct] => Array
        (
            [0] => 9
            [1] => 9
        )

    [A::func1] => Array
        (
            [0] => 11
            [1] => 13
        )

    [A::func2] => Array
        (
            [0] => 15
            [1] => 19
        )

    [add] => Array
        (
            [0] => 25
            [1] => 27
        )
)

如果是全局函数,则直接显示为函数名,如果定义在类中,则显示为 类名::函数名 的格式。 注意,interfaceabstract function不计算为函数定义,因为其只有声明,没有函数实现。上面的结果可以解释为,该源文件定义了类A,其中有类方法__construct在第9行定义;有类方法func1func2,分别在第11至第13行,第15至第19行定义。源文件还定义了全局函数add,它在第25至27行定义。

原理

利用PHP tokenizer 将源代码转为token数组,并依次读取每一个token。借鉴有限状态机的原理实现函数识别。读取token过程中根据条件实时转换状态。状态共有7种:

  • 初始状态 (init)
  • 碰到函数定义 (meet function)
  • 在函数内部 (in function)
  • 碰到类定义 (meet class)
  • 在类内部 (in class)
  • 碰到类方法定义 (meet method)
  • 在类方法内部 (in method)

本项目中的PHP类主要实现了这一状态机,并通过状态转变来判断函数(方法)定义和函数(方法)位置。

注意事项

  • 目前暂不支持namespace,默认在全局命名空间下。
  • 本工具只负责解析源代码中定义的函数,但不负责语法检查。使用前需保证待检查的源代码是没有语法错误的,否则可能会抛出RuntimeException或其他错误。

phpfunctionparser's People

Contributors

jinqiangshou avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.