Skip to content

Commit

Permalink
修复对注释 var 类型的解析 (#694)
Browse files Browse the repository at this point in the history
* 修复对注释 var 类型的解析

* 修复

* 修复
  • Loading branch information
Yurunsoft authored May 7, 2024
1 parent f617a79 commit b7ae5e4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 33 deletions.
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 @@ public function parse(\Imi\Bean\Annotation\Base $annotation, string $className,
$comment = $propRef->getDocComment();
if (false === $comment)
{
$tag = null;
throw new \RuntimeException(sprintf('@%s in %s::$%s must set name', $annotationClass, $className, $target));
}
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

0 comments on commit b7ae5e4

Please sign in to comment.