Passed
Pull Request — master (#190)
by Dmitriy
13:22
created

EnvelopeTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 28
c 3
b 1
f 0
dl 0
loc 80
ccs 24
cts 40
cp 0.6
rs 10
wmc 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A withData() 0 6 1
A withMessage() 0 6 1
A getData() 0 3 1
A withQueue() 0 6 1
A getMessage() 0 7 2
A getHandler() 0 3 1
A withMetadata() 0 6 1
A getMetadata() 0 11 1
A getEnvelopeMetadata() 0 3 1
A fromMessage() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Queue\Message;
6
7
use Yiisoft\Queue\QueueInterface;
8
9
trait EnvelopeTrait
10
{
11
    private MessageInterface $message;
12
13 18
    public function getMessage(): MessageInterface
14
    {
15 18
        $message = $this->message;
16 18
        while ($message instanceof EnvelopeInterface) {
17 3
            $message = $message->getMessage();
18
        }
19 18
        return $message;
20
    }
21
22
    public function withMessage(MessageInterface $message): self
23
    {
24
        $instance = clone $this;
25
        $instance->message = $message;
26
27
        return $instance;
28
    }
29
30
    /**
31
     * @return class-string<MessageHandlerInterface>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<MessageHandlerInterface> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<MessageHandlerInterface>.
Loading history...
32
     */
33
    public function getHandler(): string
34
    {
35
        return $this->message->getHandler();
0 ignored issues
show
Bug introduced by
The method getHandler() does not exist on Yiisoft\Queue\Message\MessageInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Yiisoft\Queue\Message\EnvelopeInterface or Yiisoft\Queue\Message\Message. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        return $this->message->/** @scrutinizer ignore-call */ getHandler();
Loading history...
36
    }
37
38 13
    public function getData(): mixed
39
    {
40 13
        return $this->message->getData();
41
    }
42
43 8
    public static function fromMessage(MessageInterface $message): self
44
    {
45 8
        return new static($message);
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Queue\Message\EnvelopeTrait::__construct() has too many arguments starting with $message. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        return /** @scrutinizer ignore-call */ new static($message);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
46
    }
47
48 13
    public function getMetadata(): array
49
    {
50 13
        return array_merge(
51 13
            $this->message->getMetadata(),
52 13
            [
53 13
                self::ENVELOPE_STACK_KEY => array_merge(
0 ignored issues
show
Bug introduced by
The constant Yiisoft\Queue\Message\En...ait::ENVELOPE_STACK_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
54 13
                    $this->message->getMetadata()[self::ENVELOPE_STACK_KEY] ?? [],
55 13
                    [self::class],
56 13
                ),
57 13
            ],
58 13
            $this->getEnvelopeMetadata(),
59 13
        );
60
    }
61
62
    public function getEnvelopeMetadata(): array
63
    {
64
        return [];
65
    }
66
67 1
    public function withData(mixed $data): self
68
    {
69 1
        $instance = clone $this;
70 1
        $instance->message = $instance->message->withData($data);
71
72 1
        return $instance;
73
    }
74
75
    public function withMetadata(array $metadata): self
76
    {
77
        $instance = clone $this;
78
        $instance->message = $instance->message->withMetadata($metadata);
79
80
        return $instance;
81
    }
82
83
    public function withQueue(QueueInterface $queue): self
84
    {
85
        $instance = clone $this;
86
        $instance->message = $instance->message->withQueue($queue);
0 ignored issues
show
Bug introduced by
The method withQueue() does not exist on Yiisoft\Queue\Message\MessageInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Yiisoft\Queue\Message\EnvelopeInterface or Yiisoft\Queue\Message\Message. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        /** @scrutinizer ignore-call */ 
87
        $instance->message = $instance->message->withQueue($queue);
Loading history...
87
88
        return $instance;
89
    }
90
}
91