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:
Complex classes like GcmComponent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GcmComponent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class GcmComponent extends Component |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * Default config |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $_defaultConfig = [ |
||
35 | 'api' => [ |
||
36 | 'key' => null, |
||
37 | 'url' => 'https://gcm-http.googleapis.com/gcm/send' |
||
38 | ], |
||
39 | 'parameters' => [ |
||
40 | 'collapse_key' => null, |
||
41 | 'priority' => 'normal', |
||
42 | 'delay_while_idle' => false, |
||
43 | 'dry_run' => false, |
||
44 | 'time_to_live' => 0, |
||
45 | 'restricted_package_name' => null |
||
46 | ] |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * List of parameters available to use in notification messages. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $_allowedNotificationParameters = [ |
||
55 | 'title', |
||
56 | 'body', |
||
57 | 'icon', |
||
58 | 'sound', |
||
59 | 'badge', |
||
60 | 'tag', |
||
61 | 'color', |
||
62 | 'click_action', |
||
63 | 'body_loc_key', |
||
64 | 'body_loc_args', |
||
65 | 'title_loc_key', |
||
66 | 'title_loc_args' |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * Error code and message. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $_errorMessages = []; |
||
75 | |||
76 | /** |
||
77 | * Response of the request |
||
78 | * |
||
79 | * @var object |
||
80 | */ |
||
81 | protected $_response = null; |
||
82 | |||
83 | /** |
||
84 | * Constructor |
||
85 | * |
||
86 | * @param ComponentRegistry $registry A ComponentRegistry |
||
87 | * @param array $config Array of configuration settings |
||
88 | */ |
||
89 | public function __construct(ComponentRegistry $registry, array $config = []) |
||
99 | |||
100 | /** |
||
101 | * send method |
||
102 | * |
||
103 | * @param string|array $ids |
||
104 | * @param array $payload |
||
105 | * @param array $parameters |
||
106 | * @throws Exception |
||
107 | * @return boolean |
||
108 | */ |
||
109 | public function send($ids = null, array $payload = [], array $parameters = []) |
||
150 | |||
151 | /** |
||
152 | * sendNotification method |
||
153 | * |
||
154 | * @param string|array $ids |
||
155 | * @param array $notification |
||
156 | * @param array $parameters |
||
157 | * @return boolean |
||
158 | */ |
||
159 | public function sendNotification($ids = null, array $notification = [], array $parameters = []) |
||
163 | |||
164 | /** |
||
165 | * sendData method |
||
166 | * |
||
167 | * @param string|array $ids |
||
168 | * @param array $data |
||
169 | * @param array $parameters |
||
170 | * @return boolean |
||
171 | */ |
||
172 | public function sendData($ids = null, array $data = [], array $parameters = []) |
||
176 | |||
177 | /** |
||
178 | * response method |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function response() |
||
190 | |||
191 | /** |
||
192 | * _executePush method |
||
193 | * |
||
194 | * @param string $message |
||
195 | * @throws Exception |
||
196 | * @return boolean |
||
197 | */ |
||
198 | protected function _executePush($message) |
||
219 | |||
220 | /** |
||
221 | * _buildMessage method |
||
222 | * |
||
223 | * @param array|string $ids |
||
224 | * @param array $payload |
||
225 | * @param array $parameters |
||
226 | * @return string |
||
227 | */ |
||
228 | protected function _buildMessage($ids, $payload, $parameters) |
||
242 | |||
243 | /** |
||
244 | * _checkNotification method |
||
245 | * |
||
246 | * @param array $notification |
||
247 | * @throws Exception |
||
248 | * @return array $notification |
||
249 | */ |
||
250 | protected function _checkNotification(array $notification = []) |
||
272 | |||
273 | /** |
||
274 | * _checkData method |
||
275 | * |
||
276 | * @param array $data |
||
277 | * @throws Exception |
||
278 | * @return array $data |
||
279 | */ |
||
280 | public function _checkData(array $data = []) |
||
297 | |||
298 | /** |
||
299 | * _checkParameters method |
||
300 | * |
||
301 | * @param array $parameters |
||
302 | * @return array $parameters |
||
303 | */ |
||
304 | protected function _checkParameters(array $parameters = []) |
||
323 | } |
||
324 |
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.