Skip to content

Instantly share code, notes, and snippets.

@fabiocicerchia
Created October 4, 2013 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fabiocicerchia/6822633 to your computer and use it in GitHub Desktop.
Save fabiocicerchia/6822633 to your computer and use it in GitHub Desktop.
PHP - DocBlock Parser
<?php
/**
* Title
*
* Description
* Description
* Description
*
* @author Fabio
* @copyright MIT
*/
class Example {
/**
* Title
*
* Description
* Description
* Description
*
* @author Fabio
* @copyright MIT
*/
public function test() {
}
}
function extractInfo($docBlock) {
$info = array();
if (preg_match_all('/@(\w+)\s+(.*)\r?\n/m', $docBlock, $matches)) {
$info = array_combine($matches[1], $matches[2]);
}
if (preg_match_all('/\*[ \t]+(.+)\r?\n/', $docBlock, $matches)) {
$description = preg_grep('/^@/', $matches[1], PREG_GREP_INVERT);
$info['title'] = array_pop($description);
$info['description'] = implode(PHP_EOL, $description);
}
return $info;
}
$reflector = new ReflectionClass('Example');
$classDocBlock = $reflector->getDocComment();
$methodDocBlock = $reflector->getMethod('test')->getDocComment();
$classInfo = extractInfo($classDocBlock);
$methodInfo = extractInfo($methodDocBlock);
print_r($classInfo);
print_r($methodInfo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment