-
-
Save emdeeeks/eeae407617f6b887bec2a9e77814192b to your computer and use it in GitHub Desktop.
PHP - DocBlock Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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