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 = 50; |
||
10 | |||
11 | const IOS = 0; |
||
12 | const ANDROID = 1; |
||
13 | const AMAZON = 2; |
||
14 | const WINDOWS_PHONE = 3; |
||
15 | |||
16 | protected $api; |
||
17 | |||
18 | public function __construct(OneSignal $api) |
||
22 | |||
23 | /** |
||
24 | * Get information about device with provided ID. |
||
25 | * |
||
26 | * @param string $id Device ID |
||
27 | * |
||
28 | * @return array |
||
29 | */ |
||
30 | public function getOne($id) |
||
34 | |||
35 | /** |
||
36 | * Get information about all registered devices for your application. |
||
37 | * |
||
38 | * Application auth key must be set. |
||
39 | * |
||
40 | * @param int $limit Results offset (results are sorted by ID) |
||
41 | * @param int $offset How many devices to return (max 50) |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | View Code Duplication | public function getAll($limit = self::DEVICES_LIMIT, $offset = 0) |
|
56 | |||
57 | /** |
||
58 | * Register a device for your application. |
||
59 | * |
||
60 | * @param array $data Device data |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | public function add(array $data) |
||
82 | |||
83 | /** |
||
84 | * Update existing registered device for your application with provided data. |
||
85 | * |
||
86 | * @param string $id Device ID |
||
87 | * @param array $data New device data |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | public function update($id, array $data) |
||
97 | |||
98 | /** |
||
99 | * Call on new device session in your app. |
||
100 | * |
||
101 | * @param string $id Device ID |
||
102 | * @param array $data Device data |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | public function onSession($id, array $data) |
||
129 | |||
130 | /** |
||
131 | * Track a new purchase. |
||
132 | * |
||
133 | * @param string $id Device ID |
||
134 | * @param array $data Device data |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | public function onPurchase($id, array $data) |
||
162 | |||
163 | /** |
||
164 | * Increment the device's total session length. |
||
165 | * |
||
166 | * @param string $id Device ID |
||
167 | * @param array $data Device data |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | public function onFocus($id, array $data) |
||
183 | |||
184 | /** |
||
185 | * Export all information about devices in a CSV format for your application. |
||
186 | * |
||
187 | * Application auth key must be set. |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | View Code Duplication | public function csvExport() |
|
199 | |||
200 | protected function resolve(array $data, callable $callback = null) |
||
243 | } |
||
244 |
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.