Are there any PHP DocBlock parser tools available? [closed]

杀马特。学长 韩版系。学妹 提交于 2019-12-17 18:37:24

问题


I would like to build some smaller scale but hightly customized documentation sites for a few projects. PhpDocumentor is pretty great but it is very heavy. I thought about trying to tweak the templates for that but after spending only a couple of minutes looking into it I decided that it would be too much work.

Ideally I'd like to see something to which I could pass a bunch of files and have it return all of the files, classes and properties and methods, along with their meta data, so that I could build out some simple templates based on the data.

Are there any DocBlock parser-only projects that will help me in this task, or am I stuck reinventing that wheel?


回答1:


You can do this easily yourself with the Reflection API:

/**
 * This is an Example class
 */
class Example
{
    /**
     * This is an example function
     */
    public function fn() 
    {
        // void
    }
}

$reflector = new ReflectionClass('Example');

// to get the Class DocBlock
echo $reflector->getDocComment()

// to get the Method DocBlock
$reflector->getMethod('fn')->getDocComment();

See this tutorial: http://www.phpriot.com/articles/reflection-api

There is also a PEAR package that can parse DocBlocks.




回答2:


In case someone needs a regular expression (xdazz suggested to try this and student310 commented it works for her/his needs)

if (preg_match_all('/@(\w+)\s+(.*)\r?\n/m', $str, $matches)){
  $result = array_combine($matches[1], $matches[2]);
}

Example (Demo):

<?php
$str ='
/**    
 * @param   integer  $int  An integer
 * @return  boolean
 */
';
if (preg_match_all('/@(\w+)\s+(.*)\r?\n/m', $str, $matches)){
  $result = array_combine($matches[1], $matches[2]);
}

var_dump($result);



回答3:


Just to update the answers. You may also want to check out the phpDocumentor2 project. I think that it's PHP DocBlock parser can be easily extracted as standalone solution.




回答4:


As furgas pointed out, I've been using phpDocumentor for years as a standalone project and it works fine.

<?php
$class = new ReflectionClass('MyClass');
$phpdoc = new \phpDocumentor\Reflection\DocBlock($class);

var_dump($phpdoc->getShortDescription());
var_dump($phpdoc->getLongDescription()->getContents());
var_dump($phpdoc->getTags());
var_dump($phpdoc->hasTag('author'));
var_dump($phpdoc->hasTag('copyright'));



回答5:


You appear to want a PHP parser that can export particular details of what has been parsed.

Our Semantic Designs PHP Front End provides a full PHP 4/5 parser (as of 2016 includes PHP 7). It parses PHP source code, builds abstract syntax trees, stamping each node with precise location information, and makes them available for further use. One general thing it can do is to print out any subtree back as the original text.

In your case, you'd want to scan over the trees, find the classes/methods/properties (as tree nodes) and print their source text, along with the location information. I think that would deliver exactly what you want.



来源:https://stackoverflow.com/questions/2531085/are-there-any-php-docblock-parser-tools-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!