diff --git a/src/Visitor/AttributeRewriteVisitor.php b/src/Visitor/AttributeRewriteVisitor.php index 95217ec..65552ac 100644 --- a/src/Visitor/AttributeRewriteVisitor.php +++ b/src/Visitor/AttributeRewriteVisitor.php @@ -289,6 +289,7 @@ protected function refactorCommentsProperty(array $comments, array $props): ?str $propsMap[$prop->var->name] = $prop; } + $commentPropMap = []; foreach ($comments as $comment) { if ('property' !== $comment['kind']) { $classComments[] = $comment['raw']; @@ -300,13 +301,23 @@ protected function refactorCommentsProperty(array $comments, array $props): ?str $classComments[] = $comment['raw']; continue; } + $commentPropMap[$meta['name']] = $comment; + } + + foreach ($propsMap as $name => $prop) { + $meta = $commentPropMap[$name]['meta'] ?? null; $methodComments = []; - if ($meta['comment']) { + if (!empty($meta['comment'])) { $methodComments[] = '* ' . $meta['comment']; - } + }; if (empty($prop->type)) { // 使用文档注解类型 $methodComments[] = "* @var {$meta['type']}"; + } elseif (\str_contains($propRawType = $this->generator->getPrinter()->prettyPrint([$prop->type]), 'callable') || \str_contains($meta['type'], 'callable')) { + // callable 不能作为属性类型 + $propRawType = $propRawType ?: $meta['type']; + $methodComments[] = "* @var {$propRawType}"; + $prop->type = null; } if (!empty($methodComments)) { $prop->setDocComment(new Doc("/**\n " . implode("\n ", $methodComments) . "\n */")); diff --git a/tests/AttributeRewriteTest.php b/tests/AttributeRewriteTest.php index 9facfba..958fb53 100644 --- a/tests/AttributeRewriteTest.php +++ b/tests/AttributeRewriteTest.php @@ -101,7 +101,15 @@ public function __construct( * immediate标志位;当immediate标志位设置为true时,如果exchange在将消息route到queue(s)时发现对应的queue上没有消费者,那么这条消息不会放入队列中。当与消息routeKey关联的所有queue(一个或多个)都没有消费者时,该消息会通过basic.return方法返还给生产者。 */ public bool \$immediate = false, - public ?int \$ticket = null + public ?int \$ticket = null, + /** + * @var ?callable + */ + public \$call1 = null, + /** + * @var callable + */ + public \$call2 = null ) { parent::__construct(...\\func_get_args()); diff --git a/tests/StubAttribute/TestConsumer.php b/tests/StubAttribute/TestConsumer.php index 2f82605..ba724bd 100644 --- a/tests/StubAttribute/TestConsumer.php +++ b/tests/StubAttribute/TestConsumer.php @@ -21,6 +21,7 @@ * @property bool $mandatory mandatory标志位;当mandatory标志位设置为true时,如果exchange根据自身类型和消息routeKey无法找到一个符合条件的queue,那么会调用basic.return方法将消息返还给生产者;当mandatory设为false时,出现上述情形broker会直接将消息扔掉。 * @property bool $immediate immediate标志位;当immediate标志位设置为true时,如果exchange在将消息route到queue(s)时发现对应的queue上没有消费者,那么这条消息不会放入队列中。当与消息routeKey关联的所有queue(一个或多个)都没有消费者时,该消息会通过basic.return方法返还给生产者。 * @property int|null $ticket + * @property callable $call2 */ #[\Attribute(\Attribute::TARGET_CLASS)] class TestConsumer extends Base @@ -34,7 +35,7 @@ class TestConsumer extends Base * @param string|array $queue * @param string|string[]|null $exchange */ - public function __construct(?array $__data = null, string $tag = '', public $queue = '', $exchange = null, string $routingKey = '', string $message = \Imi\AMQP\Message::class, bool $mandatory = false, bool $immediate = false, ?int $ticket = null) + public function __construct(?array $__data = null, string $tag = '', public $queue = '', $exchange = null, string $routingKey = '', string $message = \Imi\AMQP\Message::class, bool $mandatory = false, bool $immediate = false, ?int $ticket = null, ?callable $call1 = null, callable $call2 = null) { parent::__construct(...\func_get_args()); }