Completed
Push — master ( 693596...23a82f )
by Michał
06:07
created

Field   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 146
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A setAttributes() 0 16 4
A getTitle() 0 4 1
A setTitle() 0 6 1
A getValue() 0 4 1
A setValue() 0 6 1
A isShort() 0 4 1
A setIsShort() 0 6 1
A unserialize() 0 4 1
A toArray() 0 8 1
1
<?php namespace nyx\notify\transports\slack\message\attachment;
2
3
// External dependencies
4
use nyx\core;
5
6
/**
7
 * Slack Attachment Field
8
 *
9
 * See {@see https://api.slack.com/docs/message-attachments} and
10
 * {@see https://api.slack.com/docs/message-buttons#attachment_fields} for Slack's documentation on using
11
 * Messages with Attachments.
12
 *
13
 * @package     Nyx\Notify
14
 * @version     0.1.0
15
 * @author      Michal Chojnacki <[email protected]>
16
 * @copyright   2012-2017 Nyx Dev Team
17
 * @link        https://github.com/unyx/nyx
18
 * @todo        PHP7.1 pass for nullable return types (missing ?string on most methods).
19
 */
20
class Field implements core\interfaces\Serializable
21
{
22
    /**
23
     * The traits of a Field.
24
     */
25
    use core\traits\Serializable;
26
27
    /**
28
     * @var string  Required. The title of the Field.
29
     */
30
    protected $title;
31
32
    /**
33
     * @var string  Required. The value of the Field.
34
     */
35
    protected $value;
36
37
    /**
38
     * @var bool    Whether the value of the Field is short enough to fit side by side other Fields.
39
     */
40
    protected $isShort = true;
41
42
    /**
43
     * Creates a new Field instance.
44
     *
45
     * @param   array   $attributes
46
     */
47
    public function __construct(array $attributes)
48
    {
49
        if (!empty($attributes)) {
50
            $this->setAttributes($attributes);
51
        }
52
    }
53
54
    /**
55
     * Sets the attributes of this Field.
56
     *
57
     * @param   array   $attributes
58
     * @return  $this
59
     */
60
    public function setAttributes(array $attributes) : Field
61
    {
62
        if (isset($attributes['title'])) {
63
            $this->setTitle($attributes['title']);
64
        }
65
66
        if (isset($attributes['value'])) {
67
            $this->setValue($attributes['value']);
68
        }
69
70
        if (isset($attributes['short'])) {
71
            $this->setIsShort($attributes['short']);
72
        }
73
74
        return $this;
75
    }
76
77
    /**
78
     * Returns the title of the Field.
79
     *
80
     * @return  string
81
     */
82
    public function getTitle()
83
    {
84
        return $this->title;
85
    }
86
87
    /**
88
     * Sets the title of the Field.
89
     *
90
     * @param   string  $title
91
     * @return  $this
92
     */
93
    public function setTitle(string $title) : Field
94
    {
95
        $this->title = $title;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Returns the value of the Field.
102
     *
103
     * @return  string
104
     */
105
    public function getValue()
106
    {
107
        return $this->value;
108
    }
109
110
    /**
111
     * Sets the value of the Field.
112
     *
113
     * @param   string  $value
114
     * @return  $this
115
     */
116
    public function setValue(string $value) : Field
117
    {
118
        $this->value = $value;
119
120
        return $this;
121
    }
122
123
    /**
124
     * Returns whether the value of the Field is short enough to fit side by side other Fields.
125
     *
126
     * @return  bool
127
     */
128
    public function isShort() : bool
129
    {
130
        return $this->isShort === true;
131
    }
132
133
    /**
134
     * Sets whether the value of the Field is short enough to fit side by side other Fields.
135
     *
136
     * @param   string  $value
137
     * @return  $this
138
     */
139
    public function setIsShort(bool $value) : Field
140
    {
141
        $this->isShort = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value can also be of type string. However, the property $isShort is declared as type boolean. 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...
142
143
        return $this;
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149
    public function unserialize($data)
150
    {
151
        $this->setAttributes(unserialize($data));
152
    }
153
154
    /**
155
     * {@inheritDoc}
156
     */
157
    public function toArray() : array
158
    {
159
        return [
160
            'title' => $this->getTitle(),
161
            'value' => $this->getValue(),
162
            'short' => $this->isShort(),
163
        ];
164
    }
165
}
166