Passed
Pull Request — master (#18)
by Timon
08:58
created

Message::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkQL\Message;
5
6
use TBolier\RethinkQL\Query\Options;
7
use TBolier\RethinkQL\Query\OptionsInterface;
8
use TBolier\RethinkQL\Types\Query\QueryType;
9
10
class Message implements MessageInterface
11
{
12
    /**
13
     * @var int
14
     */
15
    private $queryType;
16
17
    /**
18
     * @var array
19
     */
20
    private $query;
21
22
    /**
23
     * @var OptionsInterface
24
     */
25
    private $options;
26
27
    /**
28
     * @param int $queryType
29
     * @param array $query
30
     * @param OptionsInterface $options
31
     */
32 21
    public function __construct(int $queryType = null, array $query = null, OptionsInterface $options = null)
33
    {
34 21
        $this->queryType = $queryType ?? QueryType::START;
35 21
        $this->query = $query ?? [];
36 21
        $this->options = $options ?? new Options();
37 21
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 1
    public function getQueryType(): int
43
    {
44 1
        return $this->queryType;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 1
    public function setCommand(int $queryType): MessageInterface
51
    {
52 1
        $this->queryType = $queryType;
53
54 1
        return $this;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 16
    public function setQuery($query): MessageInterface
61
    {
62 16
        $this->query = $query;
0 ignored issues
show
Documentation Bug introduced by
It seems like $query can also be of type string. However, the property $query is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
63
64 16
        return $this;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 17
    public function getOptions(): OptionsInterface
71
    {
72 17
        return $this->options;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 19
    public function setOptions(OptionsInterface $options): MessageInterface
79
    {
80 19
        $this->options = $options;
81
82 19
        return $this;
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 17
    public function toArray(): array
89
    {
90
        return [
91 17
            $this->queryType,
92 17
            $this->query,
93 17
            (object)$this->getOptions()
94
        ];
95
    }
96
97
    /**
98
     * @return array
99
     */
100 15
    public function jsonSerialize(): array
101
    {
102 15
        return $this->toArray();
103
    }
104
}
105