Recreate phpdocs with official lib

This commit is contained in:
Barry vd. Heuvel
2024-02-18 11:12:10 +01:00
parent f8e28d390c
commit 7dcee6fdd1
+66 -64
View File
@@ -12,10 +12,6 @@
namespace Barryvdh\LaravelIdeHelper\Console; namespace Barryvdh\LaravelIdeHelper\Console;
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface; use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
use Barryvdh\Reflection\DocBlock;
use Barryvdh\Reflection\DocBlock\Context;
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
use Barryvdh\Reflection\DocBlock\Tag;
use Composer\ClassMapGenerator\ClassMapGenerator; use Composer\ClassMapGenerator\ClassMapGenerator;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Contracts\Database\Eloquent\Castable; use Illuminate\Contracts\Database\Eloquent\Castable;
@@ -42,8 +38,12 @@ use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use phpDocumentor\Reflection\DocBlock\Tags\Generic; use phpDocumentor\Reflection\DocBlock\Serializer;
use phpDocumentor\Reflection\DocBlock\StandardTagFactory;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\BaseTag;
use phpDocumentor\Reflection\DocBlockFactory; use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\FqsenResolver;
use phpDocumentor\Reflection\Types\ContextFactory; use phpDocumentor\Reflection\Types\ContextFactory;
use ReflectionClass; use ReflectionClass;
use ReflectionNamedType; use ReflectionNamedType;
@@ -888,25 +888,32 @@ class ModelsCommand extends Command
$reflection->getInterfaceNames(), $reflection->getInterfaceNames(),
$reflection->getParentClass()->getInterfaceNames() $reflection->getParentClass()->getInterfaceNames()
); );
$phpDocContext = (new ContextFactory())->createFromReflector($reflection);
if ($this->reset) { $fqsenResolver = new FqsenResolver();
$phpdoc = new DocBlock('', new Context($namespace)); $tagFactory = new StandardTagFactory($fqsenResolver);
if ($this->keep_text) {
$phpdoc->setText( $existingTags = [];
(new DocBlock($reflection, new Context($namespace)))->getText() $tags = [];
); $summary = $class;
$description = null;
if ($reflection->getDocComment()) {
$phpdoc = (DocBlockFactory::createInstance())->create($reflection, $phpDocContext);
$existingTags = $phpdoc->getTags();
if (!$this->reset || $this->keep_text) {
$summary = $phpdoc->getSummary();
$description = $phpdoc->getDescription();
} }
} else {
$phpdoc = new DocBlock($reflection, new Context($namespace));
}
if (!$phpdoc->getText()) { if (!$this->reset) {
$phpdoc->setText($class); $tags = $existingTags;
}
} }
$properties = []; $properties = [];
$methods = []; $methods = [];
foreach ($phpdoc->getTags() as $tag) { foreach ($tags as $tag) {
$name = $tag->getName(); $name = $tag->getName();
if ($name == 'property' || $name == 'property-read' || $name == 'property-write') { if ($name == 'property' || $name == 'property-read' || $name == 'property-write') {
$properties[] = $tag->getVariableName(); $properties[] = $tag->getVariableName();
@@ -934,8 +941,7 @@ class ModelsCommand extends Command
} }
$tagLine = trim("@{$attr} {$property['type']} {$name} {$property['comment']}"); $tagLine = trim("@{$attr} {$property['type']} {$name} {$property['comment']}");
$tag = Tag::createInstance($tagLine, $phpdoc); $tags[] = $tagFactory->create($tagLine, $phpDocContext);
$phpdoc->appendTag($tag);
} }
ksort($this->methods); ksort($this->methods);
@@ -949,21 +955,13 @@ class ModelsCommand extends Command
if ($method['comment'] !== '') { if ($method['comment'] !== '') {
$tagLine .= " {$method['comment']}"; $tagLine .= " {$method['comment']}";
} }
$tag = Tag::createInstance($tagLine, $phpdoc); $tags[] = $tagFactory->create($tagLine, $phpDocContext);
$phpdoc->appendTag($tag);
} }
if ($this->write) { if ($this->write) {
$eloquentClassNameInModel = $this->getClassNameInDestinationFile($reflection, 'Eloquent'); $eloquentClassNameInModel = $this->getClassNameInDestinationFile($reflection, 'Eloquent');
// remove the already existing tag to prevent duplicates $tags[] = $tagFactory->create('@mixin ' . $eloquentClassNameInModel, $phpDocContext);
foreach ($phpdoc->getTagsByName('mixin') as $tag) {
if ($tag->getContent() === $eloquentClassNameInModel) {
$phpdoc->deleteTag($tag);
}
}
$phpdoc->appendTag(Tag::createInstance('@mixin ' . $eloquentClassNameInModel, $phpdoc));
} }
if ($this->phpstorm_noinspections) { if ($this->phpstorm_noinspections) {
@@ -971,44 +969,50 @@ class ModelsCommand extends Command
* Facades, Eloquent API * Facades, Eloquent API
* @see https://www.jetbrains.com/help/phpstorm/php-fully-qualified-name-usage.html * @see https://www.jetbrains.com/help/phpstorm/php-fully-qualified-name-usage.html
*/ */
$phpdoc->appendTag(Tag::createInstance('@noinspection PhpFullyQualifiedNameUsageInspection', $phpdoc)); $tags[] = $tagFactory->create('@noinspection PhpFullyQualifiedNameUsageInspection', $phpDocContext);
/** /**
* Relations, other models in the same namespace * Relations, other models in the same namespace
* @see https://www.jetbrains.com/help/phpstorm/php-unnecessary-fully-qualified-name.html * @see https://www.jetbrains.com/help/phpstorm/php-unnecessary-fully-qualified-name.html
*/ */
$phpdoc->appendTag( $tags[] = $tagFactory->create('@noinspection PhpUnnecessaryFullyQualifiedNameInspection', $phpDocContext);
Tag::createInstance('@noinspection PhpUnnecessaryFullyQualifiedNameInspection', $phpdoc)
);
} }
$serializer = new DocBlockSerializer(); // $serializer = new DocBlockSerializer();
$docComment = $serializer->getDocComment($phpdoc); // $docComment = $serializer->getDocComment($phpdoc);
$serializer = new Serializer();
if ($this->write_mixin) { if ($this->write_mixin) {
$phpdocMixin = new DocBlock($reflection, new Context($namespace));
// remove all mixin tags prefixed with IdeHelper // remove all mixin tags prefixed with IdeHelper
foreach ($phpdocMixin->getTagsByName('mixin') as $tag) { $mixinTags = array_filter($existingTags, function(Tag $tag) {
if (Str::startsWith($tag->getContent(), 'IdeHelper')) { return !($tag instanceof BaseTag) || !Str::startsWith($tag->getDescription(), 'IdeHelper');
$phpdocMixin->deleteTag($tag); });
}
}
$mixinClassName = "IdeHelper{$classname}"; $mixinClassName = "IdeHelper{$classname}";
$phpdocMixin->appendTag(Tag::createInstance("@mixin {$mixinClassName}", $phpdocMixin)); $mixinTags[] = $tagFactory->create("@mixin {$mixinClassName}", $phpDocContext);
$tags = array_filter($tags, function(Tag $tag) {
return !($tag instanceof BaseTag) || !Str::startsWith($tag->getDescription(), 'IdeHelper');
});
$phpdocMixin = new \phpDocumentor\Reflection\DocBlock($summary ?: $class, $description, $mixinTags, $phpDocContext);
$mixinDocComment = $serializer->getDocComment($phpdocMixin); $mixinDocComment = $serializer->getDocComment($phpdocMixin);
// remove blank lines if there's no text // remove blank lines if there's no text
if (!$phpdocMixin->getText()) { if (!$phpdocMixin->getSummary()) {
$mixinDocComment = preg_replace("/\s\*\s*\n/", '', $mixinDocComment); $mixinDocComment = preg_replace("/\s\*\s*\n/", '', $mixinDocComment);
} }
foreach ($phpdoc->getTagsByName('mixin') as $tag) {
if (Str::startsWith($tag->getContent(), 'IdeHelper')) {
$phpdoc->deleteTag($tag);
}
}
$docComment = $serializer->getDocComment($phpdoc);
} }
$tags = collect($tags)->unique(function(Tag $tag) {
return (string) $tag;
})->toArray();
$phpdoc = new \phpDocumentor\Reflection\DocBlock($summary ?: '', $description, $tags, $phpDocContext);
$docComment = $serializer->getDocComment($phpdoc);
if ($this->write) { if ($this->write) {
$modelDocComment = $this->write_mixin ? $mixinDocComment : $docComment; $modelDocComment = $this->write_mixin ? $mixinDocComment : $docComment;
$filename = $reflection->getFileName(); $filename = $reflection->getFileName();
@@ -1221,13 +1225,13 @@ class ModelsCommand extends Command
} }
$phpDocContext = (new ContextFactory())->createFromReflector($reflection); $phpDocContext = (new ContextFactory())->createFromReflector($reflection);
$phpdoc = (DocBlockFactory::createInstance())->create($reflection->getDocComment(), $phpDocContext); $phpdoc = (DocBlockFactory::createInstance())->create($reflection, $phpDocContext);
if (!$phpdoc->hasTag('comment')) { foreach($phpdoc->getTagsByName('comment') as $tag) {
return ''; return $tag;
} }
return $phpdoc->getTagsByName('comment')[0] ?: ''; return '';
} }
/** /**
@@ -1237,21 +1241,19 @@ class ModelsCommand extends Command
* *
* @return null|string * @return null|string
*/ */
protected function getReturnTypeFromDocBlock(\ReflectionMethod $reflection, \Reflector $reflectorForContext = null) protected function getReturnTypeFromDocBlock(\ReflectionMethod $reflection, \Reflector $reflectorForContext = null) : ?string
{ {
if (!$reflection->getDocComment()) {
return null;
}
$phpDocContext = (new ContextFactory())->createFromReflector($reflectorForContext ?? $reflection); $phpDocContext = (new ContextFactory())->createFromReflector($reflectorForContext ?? $reflection);
$context = new Context( $phpdoc = (DocBlockFactory::createInstance())->create($reflection, $phpDocContext);
$phpDocContext->getNamespace(),
$phpDocContext->getNamespaceAliases()
);
$type = null;
$phpdoc = new DocBlock($reflection, $context);
if ($phpdoc->hasTag('return')) { foreach ($phpdoc->getTagsByName('return') as $tag) {
$type = $phpdoc->getTagsByName('return')[0]->getType(); return $tag;
} }
return $type; return null;
} }
protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ?string protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ?string