From baadf6b4104c0de1d9ffb3ccd58f293ae17fbb9f Mon Sep 17 00:00:00 2001 From: Ed Chipman Date: Mon, 15 Apr 2024 19:51:25 -0300 Subject: [PATCH] Fixed regression where use_short_name would be prefixed with a backslash (fixes #159) (#160) --- composer.json | 2 +- src/Generators/AbstractTagGenerator.php | 65 ++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 2609e78..e39b428 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "require": { "php": "^8.0", "silverstripe/framework": "^4 || ^5", - "phpdocumentor/reflection-docblock": "^5" + "phpdocumentor/reflection-docblock": "^5.4" }, "require-dev": { "phpunit/phpunit": "^9.5", diff --git a/src/Generators/AbstractTagGenerator.php b/src/Generators/AbstractTagGenerator.php index 05bacb4..d9b0763 100644 --- a/src/Generators/AbstractTagGenerator.php +++ b/src/Generators/AbstractTagGenerator.php @@ -3,19 +3,27 @@ namespace SilverLeague\IDEAnnotator\Generators; use phpDocumentor\Reflection\DocBlock\DescriptionFactory; +use phpDocumentor\Reflection\DocBlock\StandardTagFactory; use phpDocumentor\Reflection\DocBlock\Tag; +use phpDocumentor\Reflection\DocBlock\Tags\Factory\AbstractPHPStanFactory; +use phpDocumentor\Reflection\DocBlock\Tags\Factory\MethodFactory; +use phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory; +use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory; +use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory; +use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory; +use phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory; +use phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory; use phpDocumentor\Reflection\DocBlockFactory; -use phpDocumentor\Reflection\FqsenResolver; +use phpDocumentor\Reflection\DocBlockFactoryInterface; use phpDocumentor\Reflection\TypeResolver; use SilverLeague\IDEAnnotator\DataObjectAnnotator; +use SilverLeague\IDEAnnotator\Reflection\ShortNameResolver; use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Extension; -use SilverStripe\Core\Injector\Injector; use Generator; use ReflectionClass; use ReflectionException; -use SilverLeague\IDEAnnotator\Reflection\ShortNameResolver; /** * AbstractTagGenerator @@ -41,6 +49,11 @@ abstract class AbstractTagGenerator */ protected $reflector; + /** + * @var DocBlockFactory + */ + protected $docBlockFactory; + /** * List all the generated tags form the various generateSomeORMProperies methods * @see $this->getSupportedTagTypes(); @@ -66,9 +79,9 @@ public function __construct($className, $existingTags) //Init the tag factory if (DataObjectAnnotator::config()->get('use_short_name')) { - $fqsenResolver = new ShortNameResolver(); + $this->docBlockFactory = $this->createShortNameFactory(); } else { - $fqsenResolver = new FqsenResolver(); + $this->docBlockFactory = DocBlockFactory::createInstance(); } $this->generateTags(); @@ -161,7 +174,7 @@ protected function pushTagWithExistingComment($type, $tagString) $tagString = sprintf('@%s %s', $type, $tagString); $tagString .= $this->getExistingTagCommentByTagString($tagString); - $tmpBlock = DocBlockFactory::createInstance()->create("/**\n* " . $tagString . "\n*/"); + $tmpBlock = $this->docBlockFactory->create("/**\n* " . $tagString . "\n*/"); return $tmpBlock->getTagsByName($type)[0]; } @@ -261,4 +274,44 @@ protected function pushMethodTag($methodName, $tagString) $this->tags['methods'][$tagString] = $this->pushTagWithExistingComment('method', $tagString); } } + + /** + * Factory method for easy instantiation. + * @param array|Factory> $additionalTags + * @return DocBlockFactoryInterface + */ + protected function createShortNameFactory(array $additionalTags = []): DocBlockFactory + { + $fqsenResolver = new ShortNameResolver(); + $tagFactory = new StandardTagFactory($fqsenResolver); + $descriptionFactory = new DescriptionFactory($tagFactory); + $typeResolver = new TypeResolver($fqsenResolver); + + $phpstanTagFactory = new AbstractPHPStanFactory( + new ParamFactory($typeResolver, $descriptionFactory), + new VarFactory($typeResolver, $descriptionFactory), + new ReturnFactory($typeResolver, $descriptionFactory), + new PropertyFactory($typeResolver, $descriptionFactory), + new PropertyReadFactory($typeResolver, $descriptionFactory), + new PropertyWriteFactory($typeResolver, $descriptionFactory), + new MethodFactory($typeResolver, $descriptionFactory) + ); + + $tagFactory->addService($descriptionFactory); + $tagFactory->addService($typeResolver); + $tagFactory->registerTagHandler('param', $phpstanTagFactory); + $tagFactory->registerTagHandler('var', $phpstanTagFactory); + $tagFactory->registerTagHandler('return', $phpstanTagFactory); + $tagFactory->registerTagHandler('property', $phpstanTagFactory); + $tagFactory->registerTagHandler('property-read', $phpstanTagFactory); + $tagFactory->registerTagHandler('property-write', $phpstanTagFactory); + $tagFactory->registerTagHandler('method', $phpstanTagFactory); + + $docBlockFactory = new DocBlockFactory($descriptionFactory, $tagFactory); + foreach ($additionalTags as $tagName => $tagHandler) { + $docBlockFactory->registerTagHandler($tagName, $tagHandler); + } + + return $docBlockFactory; + } }