获取类方法和注释

// 这是一个获取类方法和注释的方法
public function generateMethod()
{
    $r = new \ReflectionClass('\think\Build');
    $methods = $r->getMethods();
 
    $string = '';
    foreach ($methods as $m) {
        $limit = 'public';
        if ($m->isPrivate()) {
            $limit = 'private';
        } elseif ($m->isProtected()) {
            $limit = 'protected';
        }
 
        $methodDefinition = '### ' . $limit . ' function ' . $m->getName() . '(';
        $methodParam = $m->getParameters();
        foreach ($methodParam as $mp) {
            $type = '';
            if ($mp->hasType()) {
                $type = $mp->getType()->getName() . ' ';
            }
            $name = $mp->getName();
 
            $value = '';
            if ($mp->isDefaultValueAvailable()) {
                $defaultValue = $mp->getDefaultValue();
                if (is_array($defaultValue)) {
                    $value = ' = []';
                } elseif (is_bool($defaultValue)) {
                    $value = $defaultValue ? ' = true ' : ' = false ';
                } else {
                    $value = ' = \'' . $mp->getDefaultValue() . '\'';
                }
            }
            $methodDefinition .= $type . '$' . $name . $value . ',';
        }
        $methodDefinition = rtrim($methodDefinition, ',');
        $methodDefinition .= ')';
 
        $methodDefinition .= PHP_EOL;
        $methodDefinition .= "\t" . str_replace(['/', '*', '/'], '', $m->getDocComment());
        $methodDefinition .= PHP_EOL;
        $methodDefinition .= PHP_EOL;
 
        $string .= $methodDefinition;
    }
 
    file_put_contents('./classMethodList.txt', $string);
}

posted on 2019-07-05 15:36  果然朝辉  阅读(891)  评论(0编辑  收藏  举报

导航