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 |
||
8 | class PusherMessage |
||
9 | { |
||
10 | /** |
||
11 | * The device platform (iOS/Android). |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $platform = 'iOS'; |
||
16 | |||
17 | /** |
||
18 | * The message title. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $title; |
||
23 | |||
24 | /** |
||
25 | * The message body. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $body; |
||
30 | |||
31 | /** |
||
32 | * The phone number the message should be sent from. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $sound = 'default'; |
||
37 | |||
38 | /** |
||
39 | * The message icon (Android). |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $icon; |
||
44 | |||
45 | /** |
||
46 | * The number to display next to the push notification (iOS). |
||
47 | * |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $badge; |
||
51 | |||
52 | /** |
||
53 | * Extra options that will get added to the message. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $options = []; |
||
58 | |||
59 | /** |
||
60 | * An extra message to the other platform. |
||
61 | * |
||
62 | * @var |
||
63 | */ |
||
64 | protected $extraMessage; |
||
65 | |||
66 | /** |
||
67 | * @param string $body |
||
68 | * |
||
69 | * @return static |
||
70 | */ |
||
71 | 1 | public static function create($body = '') |
|
75 | |||
76 | /** |
||
77 | * @param string $body |
||
78 | */ |
||
79 | 14 | public function __construct($body = '') |
|
83 | |||
84 | /** |
||
85 | * Set the platform [iOS/Android]. |
||
86 | * |
||
87 | * @param string $platform |
||
88 | * |
||
89 | * @return $this |
||
90 | * |
||
91 | * @throws \NotificationChannels\PusherPushNotifications\Exceptions\CouldNotCreateMessage |
||
92 | */ |
||
93 | 1 | public function platform($platform) |
|
103 | |||
104 | /** |
||
105 | * Set the platform to iOS. |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | 2 | public function iOS() |
|
115 | |||
116 | /** |
||
117 | * Set the platform to Android. |
||
118 | * |
||
119 | * @return $this |
||
120 | */ |
||
121 | 2 | public function android() |
|
127 | |||
128 | /** |
||
129 | * Set an extra message to be sent to Android. |
||
130 | * |
||
131 | * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message |
||
132 | * @return $this |
||
133 | */ |
||
134 | 1 | public function withAndroid(self $message) |
|
140 | |||
141 | /** |
||
142 | * Set an extra message to be sent to iOS. |
||
143 | * |
||
144 | * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function withiOS(self $message) |
||
153 | |||
154 | /** |
||
155 | * Set an extra message to be sent to another platform. |
||
156 | * |
||
157 | * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message |
||
158 | * @return void |
||
159 | */ |
||
160 | 1 | private function withExtra(self $message) |
|
168 | |||
169 | /** |
||
170 | * Set the message title. |
||
171 | * |
||
172 | * @param string $value |
||
173 | * |
||
174 | * @return $this |
||
175 | */ |
||
176 | 1 | public function title($value) |
|
182 | |||
183 | /** |
||
184 | * Set the message body. |
||
185 | * |
||
186 | * @param string $value |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | 1 | public function body($value) |
|
196 | |||
197 | /** |
||
198 | * Set the message sound (Android). |
||
199 | * |
||
200 | * @param string $value |
||
201 | * |
||
202 | * @return $this |
||
203 | */ |
||
204 | 1 | public function sound($value) |
|
210 | |||
211 | /** |
||
212 | * Set the message icon (Android). |
||
213 | * |
||
214 | * @param string $value |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | 1 | public function icon($value) |
|
224 | |||
225 | /** |
||
226 | * Set the message badge (iOS). |
||
227 | * |
||
228 | * @param int $value |
||
229 | * |
||
230 | * @return $this |
||
231 | */ |
||
232 | 1 | public function badge($value) |
|
238 | |||
239 | /** |
||
240 | * @param string $key |
||
241 | * @param mixed $value |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function setOption($key, $value) |
||
251 | |||
252 | /** |
||
253 | * Get an array representation of the message. |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | 6 | public function toArray() |
|
263 | |||
264 | /** |
||
265 | * Format the message for iOS. |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | 12 | View Code Duplication | public function toiOS() |
288 | |||
289 | /** |
||
290 | * Format the message for Android. |
||
291 | * |
||
292 | * @return array |
||
293 | */ |
||
294 | 6 | View Code Duplication | public function toAndroid() |
295 | { |
||
296 | $message = [ |
||
297 | 'fcm' => [ |
||
298 | 6 | 'notification' => array_filter([ |
|
299 | 6 | 'title' => $this->title, |
|
300 | 6 | 'body' => $this->body, |
|
301 | 6 | 'sound' => $this->sound, |
|
302 | 6 | 'icon' => $this->icon, |
|
303 | ]), |
||
304 | ], |
||
305 | ]; |
||
306 | |||
307 | 6 | $this->formatMessage($message); |
|
308 | |||
309 | 6 | return $message; |
|
310 | } |
||
311 | |||
312 | /** |
||
313 | * Return the current platform. |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | 1 | public function getPlatform() |
|
321 | |||
322 | /** |
||
323 | * Format the final Payload. |
||
324 | * |
||
325 | * @param $message |
||
326 | */ |
||
327 | 13 | private function formatMessage(&$message) |
|
337 | } |
||
338 |
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.