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 |
||
16 | class Connection extends ClientConnection { |
||
17 | |||
18 | /** |
||
19 | * Request codes |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private $requestCommandList = array( |
||
24 | 'CAN_DO' => array (1), |
||
25 | 'CANT_DO' => array (2), |
||
26 | 'RESET_ABILITIES' => array (3), |
||
27 | 'PRE_SLEEP' => array (4), |
||
28 | 'SUBMIT_JOB' => array (7), |
||
29 | 'GRAB_JOB' => array (9), |
||
30 | 'WORK_STATUS' => array (12), |
||
31 | 'WORK_COMPLETE' => array (13), |
||
32 | 'WORK_FAIL' => array (14), |
||
33 | 'GET_STATUS' => array (15), |
||
34 | 'ECHO_REQ' => array (16), |
||
35 | 'SUBMIT_JOB_BG' => array (18), |
||
36 | 'SUBMIT_JOB_HIGH' => array (21), |
||
37 | 'SET_CLIENT_ID' => array (22), |
||
38 | 'CAN_DO_TIMEOUT' => array (23), |
||
39 | 'ALL_YOURS' => array (24), |
||
40 | 'WORK_EXCEPTION' => array (25), |
||
41 | 'OPTION_REQ' => array (26), |
||
42 | 'OPTION_RES' => array (27), |
||
43 | 'WORK_DATA' => array (28), |
||
44 | 'WORK_WARNING' => array (29), |
||
45 | 'GRAB_JOB_UNIQ' => array (30), |
||
46 | 'SUBMIT_JOB_HIGH_BG' => array (32), |
||
47 | 'SUBMIT_JOB_LOW' => array (33), |
||
48 | 'SUBMIT_JOB_LOW_BG' => array (34), |
||
49 | 'SUBMIT_JOB_SCHED' => array (35), |
||
50 | 'SUBMIT_JOB_EPOCH' => array (36), |
||
51 | ); |
||
52 | |||
53 | /** |
||
54 | * Response codes |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | private $responseCommandList = array( |
||
59 | 'NOOP' => array (6), |
||
60 | 'JOB_CREATED' => array (8), |
||
61 | 'NO_JOB' => array (10), |
||
62 | 'JOB_ASSIGN' => array (11), |
||
63 | 'WORK_STATUS' => array (12), |
||
64 | 'WORK_COMPLETE' => array (13), |
||
65 | 'WORK_FAIL' => array (14), |
||
66 | 'ECHO_RES' => array (17), |
||
67 | 'ERROR' => array (19), |
||
68 | 'STATUS_RES' => array (20), |
||
69 | 'WORK_EXCEPTION' => array (25), |
||
70 | 'OPTION_RES' => array (27), |
||
71 | 'WORK_WARNING' => array (29), |
||
72 | 'JOB_ASSIGN_UNIQ' => array (31) |
||
73 | ); |
||
74 | |||
75 | const MAGIC_REQUEST = "\0REQ"; |
||
76 | |||
77 | const MAGIC_RESPONSE = "\0RES"; |
||
78 | |||
79 | const HEADER_LENGTH = 12; |
||
80 | |||
81 | const HEADER_WRITE_FORMAT = "a4NN"; |
||
82 | |||
83 | const HEADER_READ_FORMAT = "a4magic/Ntype/Nsize"; |
||
84 | |||
85 | const ARGS_DELIMITER = "\0"; |
||
86 | |||
87 | |||
88 | private $jobAwaitResult = false; |
||
89 | |||
90 | /** |
||
91 | * Called when new data received |
||
92 | * |
||
93 | * @return void |
||
94 | */ |
||
95 | public function onRead() { |
||
132 | |||
133 | /** |
||
134 | * Function send ECHO |
||
135 | * |
||
136 | * @param $payload |
||
137 | * @param callable|null $cb |
||
138 | */ |
||
139 | public function sendEcho ($payload, callable $cb = null) { |
||
142 | |||
143 | /** |
||
144 | * Function run task and whait result in callback |
||
145 | * |
||
146 | * @param $function_name |
||
147 | * @param $payload |
||
148 | * @param null $context |
||
149 | * @param null $unique |
||
150 | */ |
||
151 | View Code Duplication | public function runJob($function_name, $payload, $unique = null) { |
|
162 | |||
163 | /** |
||
164 | * Function run task and whait result in callback |
||
165 | * |
||
166 | * @param $function_name |
||
167 | * @param $payload |
||
168 | * @param null $context |
||
169 | * @param null $unique |
||
170 | */ |
||
171 | View Code Duplication | public function doNormal($function_name, $payload, $unique = null) { |
|
180 | |||
181 | /** |
||
182 | * Function run task in background |
||
183 | * |
||
184 | * @param $function_name |
||
185 | * @param $payload |
||
186 | * @param null $context |
||
187 | * @param null $unique |
||
188 | */ |
||
189 | View Code Duplication | public function doBackground($function_name, $payload, $unique = null) { |
|
198 | |||
199 | /** |
||
200 | * Function run task with high prority |
||
201 | * |
||
202 | * @param $function_name |
||
203 | * @param $payload |
||
204 | * @param null $context |
||
205 | * @param null $unique |
||
206 | */ |
||
207 | View Code Duplication | public function doHigh($function_name, $payload, $unique = null) { |
|
216 | |||
217 | /** |
||
218 | * Function run task in background with high prority |
||
219 | * |
||
220 | * @param $function_name |
||
221 | * @param $payload |
||
222 | * @param null $context |
||
223 | * @param null $unique |
||
224 | */ |
||
225 | View Code Duplication | public function doHighBackground($function_name, $payload, $unique = null) { |
|
234 | |||
235 | /** |
||
236 | * Function run task with low prority |
||
237 | * |
||
238 | * @param $function_name |
||
239 | * @param $payload |
||
240 | * @param null $context |
||
241 | * @param null $unique |
||
242 | */ |
||
243 | View Code Duplication | public function doLow($function_name, $payload, $unique = null) { |
|
252 | |||
253 | /** |
||
254 | * Function run task in background with low prority |
||
255 | * |
||
256 | * @param $function_name |
||
257 | * @param $payload |
||
258 | * @param null $context |
||
259 | * @param null $unique |
||
260 | */ |
||
261 | View Code Duplication | public function doLowBackground($function_name, $payload, $unique = null) { |
|
270 | |||
271 | /** |
||
272 | * Функция исполняет удаленно таск |
||
273 | * @param $job_handle Job handle that was given in JOB_CREATED packet. |
||
274 | * |
||
275 | */ |
||
276 | public function doStatus($job_handle) { |
||
281 | |||
282 | /** |
||
283 | * Function set settings for current connection |
||
284 | * Available settings |
||
285 | * "exceptions" - Forward WORK_EXCEPTION packets to the client. |
||
286 | * |
||
287 | * @url http://gearman.org/protocol/ |
||
288 | * |
||
289 | * |
||
290 | * @param int $option_name |
||
291 | * @param callable $doneCallback |
||
292 | */ |
||
293 | public function setConnectionOption($option_name, callable $doneCallback) { |
||
296 | |||
297 | |||
298 | /** |
||
299 | * Low level commands sender |
||
300 | * |
||
301 | * @param $commandName |
||
302 | * @param $payload |
||
303 | * @param callable|null $doneCallback |
||
304 | */ |
||
305 | private function sendCommand($commandName, $payload, callable $doneCallback = null) { |
||
321 | } |