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
?
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
?
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/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/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;