Doxygen and the "self" keyword in PHP documentation

I am trying to document my PHP library with doxygen, but I am unable to configure it to recognize the correct use of my class member function using the "self" keyword. For example, the following code:

Class myclass{
    public static function myfunc1(){
      return 10; }
    public static function myfunc2(){
      return self::myfunc1(); }
}

      

incorrectly documented. Doxygen renders two functions, but when it refers to an internal or external call to that function, it ignores myfunc1 being called by myfunc2.

My workaround for now is to change the code like this:

Class myclass{
    public static function myfunc1(){
      return 10; }
    public static function myfunc2(){
      return myclass::myfunc1(); }
}

      

In this case, doxygen is correctly referring to using myfunc1 linked to myfunc2. Of course, I don't really like this solution. How can I solve this problem? many thanks

+3


source to share


1 answer


Doxygen provides an input filter option that allows us to modify the source code during documentation generation. Ex. This gives us the ability to intercept code and change code self::myfunc1

on myclass::myfunc1

the fly, which Doxygen understands. This does not change the source code in any way.

I created a filter based on code from Doxygen PHP Filters with some modifications that might make the difference for you.

Create a file /path/to/selfFilter.php and put the code inside it:

<?php
//Create file /path/to/selfFilter.php

$source = file_get_contents($argv[1]);
$tokens = token_get_all($source);
$classes = array();
foreach($tokens as $key => $token)
{
    if($token[0] == T_CLASS)
        $classes[] = $tokens[$key+2][1];
}
if(!empty($classes))
{
    list($source, $tail) = explode('class ' . $classes[0], $source, 2);
    $class_code = '';
    for($i = 1; $i < count($classes); $i++)
    {
        list($class_code, $tail) = explode('class ' . $classes[$i], $tail, 2);
        $class_code = preg_replace('#\bself::#', $classes[$i-1].'::', $class_code);
        $source .= 'class ' . $classes[$i-1] . $class_code;
    }
    $class_code = preg_replace('#\bself::#', $classes[count($classes)-1].'::', $tail);
    $source .= 'class ' . $classes[count($classes)-1] . $class_code;

}

echo $source;

      

Update the following parameters in the doxygen config file.



INPUT_FILTER           = "php /path/to/selfFilter.php"
FILTER_PATTERNS        = 
FILTER_SOURCE_FILES    = YES
FILTER_SOURCE_PATTERNS =

      

Please make sure it /path/to/selfFilter.php

is executable and php is available in the path, otherwise use the full php path

INPUT_FILTER           = /usr/bin/php /path/to/selfFilter.php

      

If you run Doxygen, it should now work. Please let me know if you have any problem.

Note. The 'class' keyword must be defined in lower case for the above filters to work.

+2


source







All Articles