Check class_exists before class definition / Doxygen problem

Currently when I create a class for my WordPress, I start the class with

if ( ! class_exist( 'class_i_want_create' ) ) :
    .... 
    class_definition
    ....
endif;

But now I want to create a documentation with Doxygen. But Doxygen doesn't find the class when it's surrounded with the class_exist.

So is it a bad practice when I don't surround it with the class_exist? Or does anybody know how I can configure Doxygen to see the class even when it's surrounded with the class_exist?

Topic oop php documentation customization Wordpress

Category Web


It looks like doxygen has some problems with such code, but... There are some solutions...

You can use INPUT_FILTERs for that. After doxygen docs:

The INPUT_FILTER tag can be used to specify a program that doxygen should invoke to filter for each input file. Doxygen will invoke the filter program by executing (via popen()) the command:

So you can use this script as a filter (as suggested in this answer: https://stackoverflow.com/a/26206860/217040):

<?php
    $source = file_get_contents($argv[1]);
    $regexp = '#(<\?php[\s]+)(if\(!class_exists\([^\)]+\)\)\{)([\s\S]*)(\})([\s]*\?>)#';
    $replace = '$1 $3 $5';
    $source = preg_replace($regexp, $replace, $source);
    echo $source;
?>

Or another one (https://stackoverflow.com/a/25655189/217040):

// input
$source = file_get_contents($argv[1]);

// removes the whole line
list($head,$tail) = preg_split('/.*if\(!class_exists\(.+/', $source, 2);   

$openingBracePos = strpos($tail,'{');
$closingBracePos = strrpos($tail,'}');

if($openingBracePos !== false && $closingBracePos !== false)
    $source = $head . substr($tail,$openingBracePos+1,
                             $closingBracePos-$openingBracePos-1);

echo $source;

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.