15

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?

Beau Simensen
  • 4,558
  • 3
  • 38
  • 55

4 Answers4

25

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.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 2
    To use reflection, don't you somehow have to load the PHP script containing the class of interest? How do you accomplish that, for an arbitarily long list of PHP scripts? – Ira Baxter Apr 03 '10 at 15:32
  • 1
    @Ira See the example at http://php.net/manual/en/reflectionclass.getdoccomment.php - if you want to parse files for classes, you can use http://github.com/theseer/Autoload/blob/master/src/classfinder.php and to iterate over directories you can use http://php.net/manual/en/class.recursivedirectoryiterator.php – Gordon Apr 03 '10 at 16:00
6

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);
Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
5

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.

cvsguimaraes
  • 12,910
  • 9
  • 49
  • 73
Furgas
  • 2,729
  • 1
  • 18
  • 27
  • 2
    DocBlox has merged with phpDocumentor to become [phpDocumentor2](http://github.com/phpDocumentor/phpDocumentor2). – quantme Jul 04 '12 at 07:05
  • Although I agree that phpDocumentor is good, the OP explicitly asks for something that **isn't** phpDocumentor – icc97 Sep 23 '14 at 11:58
3

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'));
Community
  • 1
  • 1
iwat
  • 3,591
  • 2
  • 20
  • 24