Passed
Pull Request — master (#190)
by Alexander
05:09 queued 02:32
created

EnvelopeTrait::fromMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 4
    public function getMessage(): MessageInterface
14
    {
15 4
        return $this->message;
16
    }
17
18
    public function withMessage(MessageInterface $message): self
19
    {
20
        $instance = clone $this;
21
        $instance->message = $message;
22
23
        return $instance;
24
    }
25
26
    /**
27
     * @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...
28
     */
29
    public function getHandler(): string
30
    {
31
        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

31
        return $this->message->/** @scrutinizer ignore-call */ getHandler();
Loading history...
32
    }
33
34 13
    public function getData(): mixed
35
    {
36 13
        return $this->message->getData();
37
    }
38
39 2
    public static function fromMessage(MessageInterface $message): self
40
    {
41 2
        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

41
        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...
42
    }
43
44 12
    public function getMetadata(): array
45
    {
46 12
        return array_merge(
47 12
            $this->message->getMetadata(),
48 12
            [
49 12
                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...
50 12
                    $this->message->getMetadata()[self::ENVELOPE_STACK_KEY] ?? [],
51 12
                    [self::class],
52 12
                ),
53 12
            ],
54 12
            $this->getEnvelopeMetadata(),
55 12
        );
56
    }
57
58
    public function getEnvelopeMetadata(): array
59
    {
60
        return [];
61
    }
62
63 1
    public function withData(mixed $data): self
64
    {
65 1
        $instance = clone $this;
66 1
        $instance->message = $instance->message->withData($data);
67
68 1
        return $instance;
69
    }
70
71
    public function withMetadata(array $metadata): self
72
    {
73
        $instance = clone $this;
74
        $instance->message = $instance->message->withMetadata($metadata);
75
76
        return $instance;
77
    }
78
79
    public function withQueue(QueueInterface $queue): self
80
    {
81
        $instance = clone $this;
82
        $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

82
        /** @scrutinizer ignore-call */ 
83
        $instance->message = $instance->message->withQueue($queue);
Loading history...
83
84
        return $instance;
85
    }
86
}
87