Created
September 25, 2012 18:17
-
-
Save Naatan/3783533 to your computer and use it in GitHub Desktop.
Very Basic PHP Comment Parser
This file contains 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 | |
class Parser | |
{ | |
function parse($data) | |
{ | |
$result = array(); | |
preg_match_all('/\/\*\*\s*?\n(.*?)\*\/\s*?\n\s*?(?:public)\s*?function\s*?([a-z0-9_-]*?)\(/si', $data, $matches, PREG_SET_ORDER); | |
foreach ($matches AS $match) | |
{ | |
$result[$match[2]] = $this->parseDoc($match[1]); | |
} | |
return $result; | |
} | |
function parseDoc($doc) | |
{ | |
$result = array('description' => '', 'params' => array()); | |
$result['description'] = trim(preg_replace('/(?:[ \t]*\*[ \t]*\@(.*?)\n|[ \t]*\*[ \t]*)/si', '', $doc)); | |
preg_match_all('/@([a-z0-9_-]+)\s*(.*?)\n/si', $doc, $matches, PREG_SET_ORDER); | |
foreach ($matches AS $match) | |
{ | |
$result['params'][] = array('name' => $match[1], 'value' => $match[2]); | |
} | |
return $result; | |
} | |
} | |
$data = '<?php | |
class FooBar | |
{ | |
/** | |
* Short description about class | |
* | |
* Foo Foo Foo | |
* | |
* @param1 value1 | |
* @param2 value2 | |
* @param3 Complex Value !@#$%^&*()_+-= Foo! | |
*/ | |
public function foo() | |
{ | |
// Bogus | |
} | |
/** | |
* Another description | |
* | |
* Lorem Ipsum etc. | |
* | |
* @param1b foo 1 | |
* @param2b foo 2 | |
* @param3b Complex Value !@#$%^&*()_+-= Bar! | |
*/ | |
public function bar() | |
{ | |
// Bogus | |
} | |
}'; | |
$p = new Parser; | |
$x = $p->parse($data); | |
var_export($x); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment