Skip to content

Commit

Permalink
update: 修复 callable 类型兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
NHZEX committed Oct 31, 2023
1 parent ffa629c commit 39110ab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/Visitor/AttributeRewriteVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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 */"));
Expand Down
10 changes: 9 additions & 1 deletion tests/AttributeRewriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
3 changes: 2 additions & 1 deletion tests/StubAttribute/TestConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
Expand Down

0 comments on commit 39110ab

Please sign in to comment.