Template::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace MaxBeckers\AmazonAlexa\Response\Directives\Display;
4
5
use MaxBeckers\AmazonAlexa\Helper\SerializeValueMapper;
6
7
/**
8
 * @author Maximilian Beckers <[email protected]>
9
 */
10
class Template implements \JsonSerializable
11
{
12
    use SerializeValueMapper;
13
14
    const BACK_BUTTON_MODE_HIDDEN  = 'HIDDEN';
15
    const BACK_BUTTON_MODE_VISIBLE = 'VISIBLE';
16
17
    /**
18
     * @var string|null
19
     */
20
    public $type;
21
22
    /**
23
     * @var string|null
24
     */
25
    public $token;
26
27
    /**
28
     * @var string|null
29
     */
30
    public $backButton;
31
32
    /**
33
     * @var Image|null
34
     */
35
    public $backgroundImage;
36
37
    /**
38
     * @var string|null
39
     */
40
    public $title;
41
42
    /**
43
     * @var TextContent|null
44
     */
45
    public $textContent;
46
47
    /**
48
     * @var Image|null
49
     */
50
    public $image;
51
52
    /**
53
     * @var ListItem[]
54
     */
55
    public $listItems = [];
56
57
    /**
58
     * @param ListItem $item
59
     */
60 1
    public function addListItem(ListItem $item)
61
    {
62 1
        $this->listItems[] = $item;
63
    }
64
65
    /**
66
     * @param string|null      $type
67
     * @param string|null      $token
68
     * @param string|null      $backButton
69
     * @param string|null      $backgroundImage
70
     * @param string|null      $title
71
     * @param TextContent|null $textContent
72
     * @param Image|null       $image
73
     * @param ListItem[]       $listItems
74
     *
75
     * @return Template
76
     */
77 4
    public static function create($type, $token, $backButton = self::BACK_BUTTON_MODE_VISIBLE, $backgroundImage = null, $title = null, $textContent = null, $image = null, $listItems = []): self
78
    {
79 4
        $template = new self();
80
81 4
        $template->type            = $type;
82 4
        $template->token           = $token;
83 4
        $template->backButton      = $backButton;
84 4
        $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...
85 4
        $template->title           = $title;
86 4
        $template->textContent     = $textContent;
87 4
        $template->image           = $image;
88 4
        $template->listItems       = $listItems;
89
90 4
        return $template;
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96 2
    public function jsonSerialize()
97
    {
98 2
        $data = new \ArrayObject();
99
100 2
        $this->valueToArrayIfSet($data, 'type');
101 2
        $this->valueToArrayIfSet($data, 'token');
102 2
        $this->valueToArrayIfSet($data, 'backButton');
103 2
        $this->valueToArrayIfSet($data, 'backgroundImage');
104 2
        $this->valueToArrayIfSet($data, 'title');
105 2
        $this->valueToArrayIfSet($data, 'textContent');
106 2
        $this->valueToArrayIfSet($data, 'image');
107
108 2
        if (!empty($this->listItems)) {
109 1
            $data['listItems'] = $this->listItems;
110
        }
111
112 2
        return $data;
113
    }
114
}
115