Search code examples
phpphpdoc

How to auto generate all phpdoc comments for a specific custom class?


This github file contains this auto-generated phpdoc comment

/**
 * ...
 * <autodoc generated by `composer phpdoc`>
 *
 * @property      int                 $year
 * @property      int                 $yearIso
 * @property      int                 $month
 * ...
*/

I try to install phpdoc(not with composer), and to execute it, but it generate a documentation outside of the file(it does not write any comments). How can I auto-generate a phpdoc comment for a specific class ?


Solution

  • You are confusing the phpDocumentor project with something custom that resides within the Carbon repo. The custom composer script/command adds a docblock to all methods, while phpDocumentor generates a documentation site based on existing docblocks.

    The command composer phpdoc is an alias that is defined within the composer.json file of the repository:

    "scripts": {
        "phpdoc": "php phpdoc.php"
        
        ... more scripts
    }
    

    The command runs this file in the repository, which essentially loops over all the methods and adds a docblock to the all methods:

    foreach ($factories as $file => $methods) {
        $autoDoc = compileDoc($methods, $file);
        $content = file_get_contents($file);
        $files->$file = preg_replace_callback('/(<autodoc[\s\S]*>)([\s\S]+)(<\/autodoc>)/mU', function ($matches) use ($file, $autoDoc) {
            return $matches[1]."\n *$autoDoc\n *\n * ".$matches[3];
        }, $content, 1);
    }
    
    foreach ($files as $file => $contents) {
        file_put_contents($file, $contents);
    }