Completed
Push — master ( 5b0ae6...08f84c )
by Peter
07:37
created

CardAttribute   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 149
Duplicated Lines 6.71 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
c 2
b 0
f 1
lcom 1
cbo 0
dl 10
loc 149
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A value() 0 5 1
A label() 0 5 1
A url() 0 5 1
A style() 0 5 1
A icon() 10 10 2
A __construct() 0 10 3
A create() 0 4 1
A toArray() 0 23 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace NotificationChannels\HipChat;
4
5
class CardAttribute
6
{
7
    /**
8
     * @var string
9
     */
10
    public $value;
11
12
    /**
13
     * @var string
14
     */
15
    public $label;
16
17
    /**
18
     * @var string
19
     */
20
    public $url;
21
22
    /**
23
     * @var string
24
     */
25
    public $style;
26
27
    /**
28
     * @var string
29
     */
30
    public $icon;
31
32
    /**
33
     * @var string
34
     */
35
    public $icon2;
36
37
    /**
38
     * Sets the textual value of the attribute.
39
     *
40
     * @param string $value
41
     * @return $this
42
     */
43
    public function value($value)
44
    {
45
        $this->value = trim($value);
46
        return $this;
47
    }
48
49
    /**
50
     * Sets the label of the attribute.
51
     *
52
     * @param string $label
53
     * @return $this
54
     */
55
    public function label($label)
56
    {
57
        $this->label = trim($label);
58
        return $this;
59
    }
60
61
    /**
62
     * @param string $url
63
     * @return $this
64
     */
65
    public function url($url)
66
    {
67
        $this->url = trim($url);
68
        return $this;
69
    }
70
71
    /**
72
     * Sets the style for the attribute.
73
     *
74
     * @param string $style
75
     * @return $this
76
     */
77
    public function style($style)
78
    {
79
        $this->style = trim($style);
80
        return $this;
81
    }
82
83
    /**
84
     * Sets the icon for the atttribute
85
     *
86
     * @param string $icon
87
     * @param string|null $icon2
88
     * @return $this
89
     */
90 View Code Duplication
    public function icon($icon, $icon2 = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $this->icon = trim($icon);
93
94
        if (! empty($icon2)) {
95
            $this->icon2 = trim($icon2);
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * Create a new instance of the attribute.
103
     *
104
     * @param string $label
105
     * @param string $value
106
     */
107
    public function __construct($value = null, $label = null)
108
    {
109
        if (! empty($value)) {
110
            $this->value($value);
111
        }
112
113
        if (! empty($label)) {
114
            $this->label($label);
115
        }
116
    }
117
118
    /**
119
     * Create a new instance of the attribute.
120
     *
121
     * @param string $value
122
     * @param string $label
123
     * @return static
124
     */
125
    public static function create($value = null, $label = null)
126
    {
127
        return new static($value, $label);
128
    }
129
130
    public function toArray()
131
    {
132
        $attribute = [
133
            'value' => array_filter([
134
                'label' => $this->value,
135
                'url' => $this->url,
136
                'style' => $this->style,
137
            ]),
138
        ];
139
        
140
        if (! empty($this->icon)) {
141
            $attribute['value']['icon'] = array_filter([
142
                'url' => $this->icon,
143
                'url@2x' => $this->icon2,
144
            ]);
145
        }
146
147
        if (! empty($this->label)) {
148
            $attribute['label'] = $this->label;
149
        }
150
151
        return $attribute;
152
    }
153
}
154