|
|
Dans la rubrique
Nombre de visites : 845 Mise en ligne : 05/2008 Dernière modif : 03/2009 Using Pear PHP_Parser
When I needed to parse PHP code (see Parsing PHP code), I tried PHP_Parser, version 0.2.1.
I first removed the dependencies with PEAR (very few), and used it as an autonom class. The code is quite easy to use : in Parser.php, there is class PHP_Parser ; I used static function parse($string), $string being the contents of a php file to parse.
It return a PHP_Parser_Core object, called $r ; it contains :
- $r->lex, a PHP_Parser_Tokenizer object, containing the token array.
- Fields like $r->classes, $r->functions ... contain more elaborate structures that make life easier to retrieve code elements.
- $r->data, which contains a copy of the preceeding arrays.
I used $r->data to make quick tests, and found the following problems :
Difficult to retrieve constantsConstants (not class constants, constants defined outside of a class) appear in none of these arrays (in particular, I was expecting to retrieve them in$r->data or $r->constants) ; the only place I see constants is in $r->lex.
Bug with commentsLike with the reflection API, PHP_Parser bugs to retreive doc comments, as this code illustrates :1 : Contents of Class1.php :
<?php
/** Function defined outside of a class */
function function1(){}
/** Class comment for Class1. */
class Class1{}
?>
2 : Code to test the parser :
$r = PHP_Parser::staticParseFile('Class1.php');
for($i=0; $i < count($r->data); $i++){
echo "<br/>=====================";
echo "<br/>type : " . $r->data[$i]['type'];
echo "<br/>name : " . $r->data[$i]['name'];
echo "<br/>doc : " . $r->data[$i]['doc'];
}
3 : Result :
===================== type : function name : function1 doc : /** Class comment for Class1. */ ===================== type : class name : Class1 doc :PHP_Parser associated the comment of Class1 to function1
Tolerates illegal code(This was not a problem to write a code documentor)Most of the times, when PHP_Parser finds illegal code, it throws an exception, but in some cases, illegal code is tolerated :
|