php - Retrieve DocComment from array element using Reflection? -
given array this:
array( /** i'm foo! */ 'foo' => 1, /** i'm bar! */ 'bar' => 2, ); is possible retrieve doccomments array elements? far know, reflection api doesn't provide mechanism this. if possible, i'm guessing have pretty "creative" solution.
the reflection api not able (or not @ if it's not class). per example, code:
<?php $bar = array( /** i'm foo! */ 'foo' => 1, /** i'm bar! */ 'bar' => 2, ); the reflection api useless here (no classes, no functions). can still using tokenizer:
$code = file_get_contents('input.php'); $tokens = token_get_all($code); foreach ($tokens $key => $token) { if (is_array($token)) { if ($token[0] == t_doc_comment) { ($i=$key+1; $i<count($tokens); $i++) { if (is_array($tokens[$i]) && $tokens[$i][0] != t_whitespace) { echo $tokens[$i][2] . ' = '.$token[1].php_eol; break; } } } /* t_doc_comment */ } } this print:
'foo' = /** i'm foo! */ 'bar' = /** i'm bar! */ however, keep in mind done on small example. if want go parsing complete php file (with classes, functions, etc.), you'll in bumpy ride.
in conclusion, it's possible, involves lot of work , error-prone. wouldn't recommend it. there might actual php parser exists, never used 1 can't tell.
Comments
Post a Comment