Giter Club home page Giter Club logo

anodoc's Introduction

Anodoc

Anodoc is a lightweight doc comment/block parser written in PHP. Doc comments are usually written in the style

<?php
/**
 * A test summary
 *
 * And here's a longer description that explains in detail
 * what this section is all about.
 *
 * @param  foo bar
 * @param  boo far
 * @return baz
 */
?>

and are created to document source code. Anodoc parses these comments so you can access the description, the short description, the long description, and the tags.

Usage

For the example doc comment before, you can get the data like:

<?php
$parser = new Anodoc\Parser;

// This returns a new Anodoc\DocComment object.
$doc = $parser->parse(
	"/**\n" .
	" * A test summary\n" .
	" *\n" .
	" * And here's a longer description that explains in detail\n" .
	" * what this section is all about.\n" .
	" *\n" .
	" * @param  foo bar\n" .
	" * @param  boo far\n" .
	" * @return baz\n" .
	" */"
);

echo $doc->getShortDescription();
// "A test summary"

echo $doc->getLongDescription();
// And here's a longer description that explains in detail
// what this section is all about.

echo $doc->getTagValue('return');
// baz

$doc->getTag('return');
// A Tag object with value 'baz'

echo $doc->getTagValue('param'); // this will return the last defined value
// boo far

var_dump($doc->getTags('param'));
// a dump of a TagGroup that has 2 values

?>

You can also get all the docComments on a class by using Anodoc

<?php

// Factory instantiates a new Anodoc object
// With a Parser
$anodoc = Anodoc::getNew();

$classDoc = $anodoc->getDoc('FooClass');

// Get the main class doc comment description
echo $classDoc->getMainDoc();

// Get the doc comment for method FooClass::fooMethod()
$classDoc->getMethodDoc('fooMethod')

// Get the doc comment for the attribute $bar
$classDoc->getAttributeDoc('bar');
?>

Custom Tags

Anodoc does not define any syntactic customizations for tag values. By default, each value retrieved from the parser returns an instance of Anodoc\Tags\GenericTag. You can register a new Tag type by creating a class that extends the abstract class 'Anodoc\Tags\Tag' and registering it through the parser or Anodoc object. Then you can set your custom syntax there.

For example, Anodoc comes with a custom tag for handling params named "Anodoc\Tags\ParamTag". The definition is:

<?php

namespace Anodoc\Tags;

class ParamTag extends Tag {

  private $tag_name, $value;

  function __construct($tag_name, $value) {
    preg_match('/(\w+)\s+\$(\w+)\s+(.+)/ms', $value, $matches);
    $this->value = array(
      'type' => $matches[1],
      'name' => $matches[2],
      'description' => $matches[3]
    );
    $this->tag_name = $tag_name;
  }

  function getValue() {
    return $this->value;
  }

  function getTagName() {
    return $this->tag_name;
  }

  function __toString() {
    return (string) $this->value;
  }

}
?>

This is not registered by default so you must register it yourself to be useful.

<?php

$anodoc = Anodoc::getNew();
$anodoc->registerTag('param', 'Anodoc\Tags\ParamTag');
$classDoc = $anodoc->getDoc('FooClass');

$param = $classDoc->getMethodDoc('fooMethod')->getTag('param');
// returns an 'Anodoc\Tags\ParamTag' object

$param->getValue();
// if the param tag looks like this:
//     @param string $firstName the first name of the person
// The value would be:
// array(
//     'type' => 'string', 'name' => 'firstName',
//     'description' => 'the first name of the person'
// )

?>

Limitations

Anodoc doesn't parse inline tags as of the moment.

anodoc's People

Contributors

asartalo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

anodoc's Issues

Tabulators in anotations

Hi,

I noticed that the library does not handle well tabulators. The code below returns prints NULL.

$docBlock = <<<STR
/**
\t* Adds set of radio button controls to the form.
\t* @param  string  control name
\t* @param  string  label
\t* @param  array   options from which to choose
\t* @return Nette\Forms\Controls\RadioList
\t*/
STR;

$parser = new \Anodoc\Parser;
$doc = $parser->parse($docBlock);
var_dump($doc->getTagValue('return'));

Have a nice day!

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.