Source of file DocBlockGenerator.php
Size: 3,543 Bytes - Last Modified: 2022-01-13T10:01:07+00:00
/var/www/docs.ssmods.com/process/src/src/Generators/DocBlockGenerator.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 | <?php namespace SilverLeague\IDEAnnotator\Generators; use InvalidArgumentException; use LogicException; use phpDocumentor\Reflection\DocBlock; use phpDocumentor\Reflection\DocBlock\Serializer; use phpDocumentor\Reflection\DocBlock\Tag; use ReflectionClass; use ReflectionException; use SilverStripe\Control\Controller; /** * Class DocBlockGenerator * * @package IDEAnnotator/Generators */ class DocBlockGenerator { /** * The current class we are working with * @var string */ protected $className = ''; /** * @var ReflectionClass */ protected $reflector; /** * @var AbstractTagGenerator */ protected $tagGenerator; /** * DocBlockGenerator constructor. * * @param $className * @throws ReflectionException * @throws InvalidArgumentException */ public function __construct($className) { $this->className = $className; $this->reflector = new ReflectionClass($className); $generatorClass = $this->reflector->isSubclassOf(Controller::class) ? ControllerTagGenerator::class : OrmTagGenerator::class; $this->tagGenerator = new $generatorClass($className, $this->getExistingTags()); } /** * @return Tag[] * @throws InvalidArgumentException */ public function getExistingTags() { $docBlock = $this->getExistingDocBlock(); $docBlock = new DocBlock($docBlock); return $docBlock->getTags(); } /** * Not that in case there are multiple doblocks for a class, * the last one will be returned * * If we file old style generated docblocks we remove them * * @return bool|string */ public function getExistingDocBlock() { return $this->reflector->getDocComment(); } /** * @return DocBlock|string * @throws LogicException * @throws InvalidArgumentException */ public function getGeneratedDocBlock() { $docBlock = $this->getExistingDocBlock(); return $this->mergeGeneratedTagsIntoDocBlock($docBlock); } /** * @param string $existingDocBlock * @return string * @throws LogicException * @throws InvalidArgumentException */ protected function mergeGeneratedTagsIntoDocBlock($existingDocBlock) { $docBlock = new DocBlock($this->removeExistingSupportedTags($existingDocBlock)); if (!$docBlock->getText()) { $docBlock->setText('Class \\' . $this->className); } foreach ($this->getGeneratedTags() as $tag) { $docBlock->appendTag($tag); } $serializer = new Serializer(); $docBlock = $serializer->getDocComment($docBlock); return $docBlock; } /** * Remove all existing tags that are supported by this module. * * This will make sure that removed ORM properties and Extenions will not remain in the docblock, * while providing the option to manually add docblocks like @author etc. * * @param $docBlock * @return string */ public function removeExistingSupportedTags($docBlock) { $replacements = [ "/ \* @property ([\s\S]*?)\n/", "/ \* @method ([\s\S]*?)\n/", "/ \* @mixin ([\s\S]*?)\n/" ]; return (string)preg_replace($replacements, '', $docBlock); } /** * @return Tag[] */ public function getGeneratedTags() { return $this->tagGenerator->getTags(); } } |