Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复对注释 var 类型的解析 #694

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions src/Bean/Parser/BeanParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
use Imi\Bean\ReflectionContainer;
use Imi\Server\Annotation\ServerInject;
use Imi\Util\DocBlock;
use Imi\Util\Imi;
use Yurun\Doctrine\Common\Annotations\PhpParser;
use phpDocumentor\Reflection\Types\Context;

class BeanParser extends BaseParser
{
Expand Down Expand Up @@ -62,40 +61,17 @@
$comment = $propRef->getDocComment();
if (false === $comment)
{
$tag = null;
throw new \RuntimeException(sprintf('@%s in %s::$%s must set name', $annotationClass, $className, $target));

Check warning on line 64 in src/Bean/Parser/BeanParser.php

View check run for this annotation

Codecov / codecov/patch

src/Bean/Parser/BeanParser.php#L64

Added line #L64 was not covered by tests
}
else
{
$docblock = DocBlock::getDocBlock($comment);
$tag = $docblock->getTagsByName('var')[0] ?? null;
}
if ($tag)
{
$name = trim($tag->__toString(), '\\ \t\n\r\0\x0B');
$phpParser = new PhpParser();
$uses = $phpParser->parseClass(ReflectionContainer::getClassReflection($className));
$lowerName = strtolower($name);
if (isset($uses[$lowerName]))
{
$annotation->name = $uses[$lowerName];
}
else
$docblock = DocBlock::getDocBlock($comment, new Context($propRef->getDeclaringClass()->getNamespaceName()));
$tag = $docblock->getTagsWithTypeByName('var')[0] ?? null;
if ($tag)
{
$tmpClassName = Imi::getClassNamespace($className) . '\\' . $name;
if (class_exists($tmpClassName))
{
$annotation->name = $tmpClassName;
}
else
{
$annotation->name = $name;
}
$annotation->name = $tag->getType()->__toString();
}
}
else
{
throw new \RuntimeException(sprintf('@%s in %s::$%s must set name', $annotationClass, $className, $target));
}
}
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Components/grpc/src/Util/GrpcInterfaceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Imi\Bean\ReflectionUtil;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Util\DocBlock;
use phpDocumentor\Reflection\Types\Context;

/**
* @Bean(name="GrpcInterfaceManager", recursion=false)
Expand Down Expand Up @@ -93,9 +94,8 @@ public function bind($interface, ?string $serviceName = null): void
}
else
{
$docblock = DocBlock::getDocBlock($docComment);
// @phpstan-ignore-next-line
$responseClass = (string) $docblock->getTagsByName('return')[0]->getType();
$docblock = DocBlock::getDocBlock($docComment, new Context($interface->getNamespaceName()));
$responseClass = (string) $docblock->getTagsWithTypeByName('return')[0]->getType();
}

$methods[$method->getName()] = [
Expand Down
Loading