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 |
||
5 | class HipChatMessage |
||
6 | { |
||
7 | /** |
||
8 | * The HipChat room identifier. |
||
9 | * |
||
10 | * @var string |
||
11 | */ |
||
12 | public $room = ''; |
||
13 | |||
14 | /** |
||
15 | * A label to be shown in addition to the sender's name. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | public $from = ''; |
||
20 | |||
21 | /** |
||
22 | * The format of the notification (text, html). |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | public $format = 'text'; |
||
27 | |||
28 | /** |
||
29 | * Should a message trigger a user notification in a HipChat client. |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | public $notify = false; |
||
34 | |||
35 | /** |
||
36 | * The "level" of the notification (info, success, error). |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | public $level = 'info'; |
||
41 | |||
42 | /** |
||
43 | * The color of the notification (yellow, green, red, purple, gray, random). |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | public $color = 'gray'; |
||
48 | |||
49 | /** |
||
50 | * The text content of the message. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | public $content = ''; |
||
55 | |||
56 | /** |
||
57 | * An instance of Card object. |
||
58 | * |
||
59 | * @var Card |
||
60 | */ |
||
61 | public $card; |
||
62 | |||
63 | /** |
||
64 | * The message id to to attach this notification to, for example if this notification is |
||
65 | * in response to a particular message. For supported clients, this will display the |
||
66 | * notification in the context of the referenced message specified by attach_to parameter. |
||
67 | * If this is not possible to attach the notification, it will be rendered as an unattached |
||
68 | * notification. The message must be in the same room as that the notification is sent to. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | public $attachTo; |
||
73 | |||
74 | /** |
||
75 | * Create a new instance of HipChatMessage. |
||
76 | * |
||
77 | * @param string $content |
||
78 | * @return static |
||
79 | */ |
||
80 | 19 | public static function create($content = '') |
|
84 | |||
85 | /** |
||
86 | * Create a new instance of HipChatMessage. |
||
87 | * |
||
88 | * @param $content |
||
89 | */ |
||
90 | 22 | public function __construct($content = '') |
|
94 | |||
95 | /** |
||
96 | * Set the HipChat room to send message to. |
||
97 | * |
||
98 | * @param int|string $room |
||
99 | * @return $this |
||
100 | */ |
||
101 | 1 | public function room($room) |
|
107 | |||
108 | /** |
||
109 | * Indicate that the notification gives general information. |
||
110 | * |
||
111 | * @return $this |
||
112 | */ |
||
113 | 1 | public function info() |
|
120 | |||
121 | /** |
||
122 | * Indicate that the notification gives information about a successful operation. |
||
123 | * |
||
124 | * @return $this |
||
125 | */ |
||
126 | 1 | public function success() |
|
133 | |||
134 | /** |
||
135 | * Indicate that the notification gives information about an error. |
||
136 | * |
||
137 | * @return $this |
||
138 | */ |
||
139 | 2 | public function error() |
|
146 | |||
147 | /** |
||
148 | * Set the from label of the HipChat message. |
||
149 | * |
||
150 | * @param string $from |
||
151 | * @return $this |
||
152 | */ |
||
153 | 2 | public function from($from) |
|
159 | |||
160 | /** |
||
161 | * Set HTML format and optionally the content. |
||
162 | * |
||
163 | * @param string $content |
||
164 | * @return $this |
||
165 | */ |
||
166 | 3 | public function html($content = '') |
|
176 | |||
177 | /** |
||
178 | * Set text format and optionally the content. |
||
179 | * |
||
180 | * @param string $content |
||
181 | * @return $this |
||
182 | */ |
||
183 | 2 | public function text($content = '') |
|
193 | |||
194 | /** |
||
195 | * Should a message trigger a user notification in a HipChat client. |
||
196 | * |
||
197 | * @param bool $notify |
||
198 | * @return $this |
||
199 | */ |
||
200 | 2 | public function notify($notify = true) |
|
206 | |||
207 | /** |
||
208 | * Set the content of the message. |
||
209 | * Allowed HTML tags: a, b, i, strong, em, br, img, pre, code, lists, tables. |
||
210 | * |
||
211 | * @param string $content |
||
212 | * @return $this |
||
213 | */ |
||
214 | 22 | public function content($content) |
|
220 | |||
221 | /** |
||
222 | * Set the color for the message. |
||
223 | * Allowed values: yellow, green, red, purple, gray, random. |
||
224 | * |
||
225 | * @param $color |
||
226 | * @return string |
||
227 | */ |
||
228 | 1 | public function color($color) |
|
234 | |||
235 | /** |
||
236 | * Sets the Card. |
||
237 | * |
||
238 | * @param Card|\Closure|null $card |
||
239 | * @return $this |
||
240 | */ |
||
241 | 3 | View Code Duplication | public function card($card) |
258 | |||
259 | /** |
||
260 | * Sets the id of the "parent" message to attach this notification to. |
||
261 | * |
||
262 | * @param $id |
||
263 | * @return $this |
||
264 | */ |
||
265 | 2 | public function attachTo($id) |
|
271 | |||
272 | /** |
||
273 | * Get an array representation of the HipChatMessage. |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | 2 | public function toArray() |
|
294 | } |
||
295 |
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 theid
property of an instance of theAccount
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.