Passed
Pull Request — master (#190)
by Dmitriy
04:22 queued 01:36
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
trait EnvelopeTrait
8
{
9
    private MessageInterface $message;
10
11 13
    public function getMessage(): MessageInterface
12
    {
13 13
        $message = $this->message;
14 13
        while ($message instanceof EnvelopeInterface) {
15 1
            $message = $message->getMessage();
16
        }
17 13
        return $message;
18
    }
19
20
    public function withMessage(MessageInterface $message): self
21
    {
22
        $instance = clone $this;
23
        $instance->message = $message;
24
25
        return $instance;
26
    }
27
28 11
    public function getData(): mixed
29
    {
30 11
        return $this->message->getData();
31
    }
32
33 8
    public static function fromMessage(MessageInterface $message): EnvelopeInterface
34
    {
35 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

35
        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...
Bug Best Practice introduced by
The expression return new static($message) returns the type Yiisoft\Queue\Message\EnvelopeTrait which is incompatible with the type-hinted return Yiisoft\Queue\Message\EnvelopeInterface.
Loading history...
36
    }
37
38 13
    public function getMetadata(): array
39
    {
40 13
        return array_merge(
41 13
            $this->message->getMetadata(),
42 13
            [
43 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...
44 13
                    $this->message->getMetadata()[self::ENVELOPE_STACK_KEY] ?? [],
45 13
                    [self::class],
46 13
                ),
47 13
            ],
48 13
            $this->getEnvelopeMetadata(),
49 13
        );
50
    }
51
52
    public function getEnvelopeMetadata(): array
53
    {
54
        return [];
55
    }
56
57
    public function withData(mixed $data): self
58
    {
59
        $instance = clone $this;
60
        $instance->message = $instance->message->withData($data);
61
62
        return $instance;
63
    }
64
65
    public function withMetadata(array $metadata): self
66
    {
67
        $instance = clone $this;
68
        $instance->message = $instance->message->withMetadata($metadata);
69
70
        return $instance;
71
    }
72
}
73