Passed
Pull Request — master (#190)
by Dmitriy
02:42
created

EnvelopeTrait::withData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
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
trait EnvelopeTrait
8
{
9
    private MessageInterface $message;
10
11 4
    public function getMessage(): MessageInterface
12
    {
13 4
        return $this->message;
14
    }
15
16
    public function withMessage(MessageInterface $message): self
17
    {
18
        $instance = clone $this;
19
        $instance->message = $message;
20
21
        return $instance;
22
    }
23
24
    /**
25
     * @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...
26
     */
27
    public function getHandler(): string
28
    {
29
        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

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

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