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 |
||
7 | class Devices |
||
8 | { |
||
9 | const DEVICES_LIMIT = 300; |
||
10 | |||
11 | const IOS = 0; |
||
12 | const ANDROID = 1; |
||
13 | const AMAZON = 2; |
||
14 | const WINDOWS_PHONE = 3; |
||
15 | const WINDOWS_PHONE_MPNS = 3; |
||
16 | const CHROME_APP = 4; |
||
17 | const CHROME_WEB = 5; |
||
18 | const WINDOWS_PHONE_WNS = 6; |
||
19 | const SAFARI = 7; |
||
20 | const FIREFOX = 8; |
||
21 | const MACOS = 9; |
||
22 | |||
23 | protected $api; |
||
24 | |||
25 | public function __construct(OneSignal $api) |
||
29 | |||
30 | /** |
||
31 | * Get information about device with provided ID. |
||
32 | * |
||
33 | * @param string $id Device ID |
||
34 | * |
||
35 | * @return array |
||
36 | */ |
||
37 | public function getOne($id) |
||
45 | |||
46 | /** |
||
47 | * Get information about all registered devices for your application. |
||
48 | * |
||
49 | * Application auth key must be set. |
||
50 | * |
||
51 | * @param int $limit How many devices to return. Max is 300. Default is 300 |
||
52 | * @param int $offset Result offset. Default is 0. Results are sorted by id |
||
53 | * |
||
54 | * @return array |
||
55 | */ |
||
56 | View Code Duplication | public function getAll($limit = self::DEVICES_LIMIT, $offset = 0) |
|
68 | |||
69 | /** |
||
70 | * Register a device for your application. |
||
71 | * |
||
72 | * @param array $data Device data |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | public function add(array $data) |
||
99 | |||
100 | /** |
||
101 | * Update existing registered device for your application with provided data. |
||
102 | * |
||
103 | * @param string $id Device ID |
||
104 | * @param array $data New device data |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public function update($id, array $data) |
||
114 | |||
115 | /** |
||
116 | * Call on new device session in your app. |
||
117 | * |
||
118 | * @param string $id Device ID |
||
119 | * @param array $data Device data |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | public function onSession($id, array $data) |
||
149 | |||
150 | /** |
||
151 | * Track a new purchase. |
||
152 | * |
||
153 | * @param string $id Device ID |
||
154 | * @param array $data Device data |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | public function onPurchase($id, array $data) |
||
180 | |||
181 | /** |
||
182 | * Increment the device's total session length. |
||
183 | * |
||
184 | * @param string $id Device ID |
||
185 | * @param array $data Device data |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | public function onFocus($id, array $data) |
||
199 | |||
200 | /** |
||
201 | * Export all information about devices in a CSV format for your application. |
||
202 | * |
||
203 | * Application auth key must be set. |
||
204 | * |
||
205 | * @param array $extraFields Additional fields that you wish to include. |
||
206 | * Currently supports: "location", "country", "rooted" |
||
207 | * |
||
208 | * @return array |
||
209 | */ |
||
210 | public function csvExport(array $extraFields = []) |
||
224 | |||
225 | protected function resolve(array $data, callable $callback = null) |
||
274 | } |
||
275 |
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.