php - Check class_exists before class definitionDoxygen problem

admin2025-06-05  2

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?

Share Improve this question edited Nov 16, 2018 at 7:43 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 16, 2018 at 7:42 Carsten SchmittCarsten Schmitt 7711 bronze badges 2
  • Is the class you're creating likely to be defined already? – Jacob Peattie Commented Nov 16, 2018 at 7:52
  • I think not, because I start all my classes with a small prefix. But as I started to develop a wordpress plugin, I learned that it's good practice to use the class_exist. – Carsten Schmitt Commented Nov 16, 2018 at 7:54
Add a comment  | 

1 Answer 1

Reset to default 1

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;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749094811a316318.html

最新回复(0)