Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doxygen - PHP traits

We have grown fond of Doxygen, it certainly appears to create the best looking documentation.

However, we use PHP and Traits are simply ignored / absent from generated documentation.

Are we missing a trick here, or are Traits simply not compatible?

like image 582
Gavin Avatar asked Feb 21 '26 14:02

Gavin


1 Answers

Traits are just not supported by doxygen.

Since PHP does not support inheritance from multiple classes, traits are the way to extend a class by functions of multiple "classes".

C++ does support inheritance from multiple classes, so try this filter:

// make traits to classes
$regexp = '#trait([\s]+[\S]+[\s]*){#';
$replace = 'class$1{';
$source = preg_replace($regexp, $replace, $source);

// use traits by extending them (classes that not extending a class)
$regexp = '#class([\s]+[\S]+[\s]*)(implements[\s]+[\S]+[\s]*)?{[\s]+use([^;]+);#';
$replace = 'class$1 extends $3 $2 {';
$source = preg_replace($regexp, $replace, $source);

// use traits by extending them (classes that already extending a class)
$regexp = '#class([\s]+[\S]+[\s]+extends[\s]+[\S]+[\s]*)(implements[\s]+[\S]+[\s]*){[\s]+use([^;]+);#';
$replace = 'class$1, $3 $2{';
$source = preg_replace($regexp, $replace, $source);

This filter does:

  1. Transforms traits to classes

    trait MyTrait{ ... }
    

    becomes

    class MyTrait{ ... }
    
  2. Transform "using" traits into "extending" traits

    class MyClass{
        use MyTrait1, MyTrait2;
        ...
    }
    

    becomes

    class MyClass extends MyTrait, MyTrait2{
        ...
    }
    

Doxygen documents this as a multiple inheritance. Maybe this works for you.

You can find this and some more doxygen filters for PHP in my repository on GitHub.

like image 138
AbcAeffchen Avatar answered Feb 24 '26 02:02

AbcAeffchen