Passed
Pull Request — master (#84)
by Maximilian
05:34
created

Template::jsonSerialize()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 30
rs 6.5222
cc 9
nc 256
nop 0
1
<?php
2
3
namespace MaxBeckers\AmazonAlexa\Response\Directives\Display;
4
5
/**
6
 * @author Maximilian Beckers <[email protected]>
7
 */
8
class Template implements \JsonSerializable
9
{
10
    const BACK_BUTTON_MODE_HIDDEN  = 'HIDDEN';
11
    const BACK_BUTTON_MODE_VISIBLE = 'VISIBLE';
12
13
    /**
14
     * @var string|null
15
     */
16
    public $type;
17
18
    /**
19
     * @var string|null
20
     */
21
    public $token;
22
23
    /**
24
     * @var string|null
25
     */
26
    public $backButton;
27
28
    /**
29
     * @var Image|null
30
     */
31
    public $backgroundImage;
32
33
    /**
34
     * @var string|null
35
     */
36
    public $title;
37
38
    /**
39
     * @var TextContent|null
40
     */
41
    public $textContent;
42
43
    /**
44
     * @var Image|null
45
     */
46
    public $image;
47
48
    /**
49
     * @var ListItem[]
50
     */
51
    public $listItems = [];
52
53
    /**
54
     * @param ListItem $item
55
     */
56
    public function addListItem(ListItem $item)
57
    {
58
        $this->listItems[] = $item;
59
    }
60
61
    /**
62
     * @param string|null      $type
63
     * @param string|null      $token
64
     * @param string|null      $backButton
65
     * @param string|null      $backgroundImage
66
     * @param string|null      $title
67
     * @param TextContent|null $textContent
68
     * @param Image|null       $image
69
     * @param ListItem[]       $listItems
70
     *
71
     * @return Template
72
     */
73
    public static function create($type, $token, $backButton = self::BACK_BUTTON_MODE_VISIBLE, $backgroundImage = null, $title = null, $textContent = null, $image = null, $listItems = []): self
74
    {
75
        $template = new self();
76
77
        $template->type            = $type;
78
        $template->token           = $token;
79
        $template->backButton      = $backButton;
80
        $template->backgroundImage = $backgroundImage;
0 ignored issues
show
Documentation Bug introduced by
It seems like $backgroundImage can also be of type string. However, the property $backgroundImage is declared as type MaxBeckers\AmazonAlexa\R...ives\Display\Image|null. 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...
81
        $template->title           = $title;
82
        $template->textContent     = $textContent;
83
        $template->image           = $image;
84
        $template->listItems       = $listItems;
85
86
        return $template;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function jsonSerialize()
93
    {
94
        $data = new \ArrayObject();
95
96
        if (null !== $this->type) {
97
            $data['type'] = $this->type;
98
        }
99
        if (null !== $this->token) {
100
            $data['token'] = $this->token;
101
        }
102
        if (null !== $this->backButton) {
103
            $data['backButton'] = $this->backButton;
104
        }
105
        if (null !== $this->backgroundImage) {
106
            $data['backgroundImage'] = $this->backgroundImage;
107
        }
108
        if (null !== $this->title) {
109
            $data['title'] = $this->title;
110
        }
111
        if (null !== $this->textContent) {
112
            $data['textContent'] = $this->textContent;
113
        }
114
        if (null !== $this->image) {
115
            $data['image'] = $this->image;
116
        }
117
        if (!empty($this->listItems)) {
118
            $data['listItems'] = $this->listItems;
119
        }
120
121
        return $data;
122
    }
123
}
124