10

I'm looking for some method of converting a PHP Docblock (as used for generating documentation by tools like Doxygen) into a structure I can inspect in PHP.

For example, I want to parse

/**
 * Multiply two values
 * @CHECKME
 *
 * @author someone
 * @created eons ago
 *
 * @param integer $x
 * @param integer $x
 *
 * @return integer
 */
function multiply($x, $y)
{
    return $x * $y;
}

into something similar to:

array(
     'author'  => 'someone'
    ,'created' => 'eons ago'
    ,'param'   => array(
                      'integer $x'
                     ,'integer $y'
                  )
    ,'_flags'  => array(
                     '@CHECKME'
                  )
);

I explicitly cannot use PEAR or any such library, it has to be relatively standalone. Any given solution that is better than using a bunch of regular expressions after stripping away comment outline would be awesome.

14
  • 1
    possible duplicate of Are there any php docblock parser tools available? Commented Jan 30, 2011 at 21:43
  • 1
    @Gordon. Not a duplicate, I was not looking to merely generate a bunch of static documentation files but to provide enhanced reflection at runtime. Generating documentation is an extra benefit. Commented May 2, 2012 at 10:46
  • possible duplicate of Parsing PHP Doc comments into a data structure Commented May 2, 2012 at 11:04
  • and a couple more in the Related section to the right. Commented May 2, 2012 at 11:04
  • @Gordon, Look again, that's a newer question. As are the ones to the right that are actually similar that I just checked. I did actually use search before I posted. Commented May 2, 2012 at 11:54

4 Answers 4

0

I wonder if you couldn't just use the phpdocumentor classes. They already parse the data. I suspect you could write your own interim parser to process a file but then handle the raw objects in any way you want.

Sign up to request clarification or add additional context in comments.

3 Comments

I've been unable to get into any source for phpDocumentor for the last couple hours, but my assumptions based on the website/faq are that it is php4 code that depends on PEAR (need PEAR to install it). Using it would not be less work than writing it myself by the look of things. (I could be wrong, unable to verify atm)
The classes are available as *.inc files within the phpDocumentor directory upon download. I'm not sure if you saw that or not.
Yes I did but by the time I have familiarized mysql with the old fashioned style and structure I can have written my own implementation that follows my actual spec and i'd still have to re-qrite the php4 code by that time. Standalone != complete project
0

Look at using the Reflection class [1] as a starting point, particularly its getDocComment() method [2]. Also, Matthew's blog post about his foray into using it [3] may provide further insights.

[1] -- http://www.php.net/manual/en/class.reflectionclass.php

[2] -- http://www.php.net/manual/en/reflectionclass.getdoccomment.php

[3] -- http://mwop.net/blog/125-PHP-5s-Reflection-API

2 Comments

That's always been how I got the doccomment, I was looking for some way to parse annotations from it that would fit in with my existing codebase.
Unfortunately it requires you to actually include the file where the docblocks are contained.
-1

PHPDocumentor IS a standalone docblock parser.

1 Comment

I just downloaded a zip and looked at the source, but phpDocumentors code is terrible for my use case. It will take days to rework that instead of hours to roll my own. I need one or two classes, not an intermingled project with dependencies all over the place, and certainly no php4 code. I might be able to glean something from it if i run something through it in an xdebug session, but after seeing the code I still think its unusable.
-1

I have written a runtime PHP documentation utility that contains a PHPDoc parser class (Doqumentor). I would think this will do what you want if you want to take a look. It is also available on github if you have any improvements or bug fixes.

1 Comment

The site (and, I think, the project) is not available. Giving this -1.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.