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 = [ |
||
| 24 | 'CAN_DO' => 1, |
||
| 25 | 'CANT_DO' => 2, |
||
| 26 | 'RESET_ABILITIES' => 3, |
||
| 27 | 'PRE_SLEEP' => 4, |
||
| 28 | 'SUBMIT_JOB' => 7, |
||
| 29 | 'GRAB_JOB' => 9, |
||
| 30 | 'WORK_STATUS' => 12, |
||
| 31 | 'WORK_COMPLETE' => 13, |
||
| 32 | 'WORK_FAIL' => 14, |
||
| 33 | 'GET_STATUS' => 15, |
||
| 34 | 'ECHO_REQ' => 16, |
||
| 35 | 'SUBMIT_JOB_BG' => 18, |
||
| 36 | 'SUBMIT_JOB_HIGH' => 21, |
||
| 37 | 'SET_CLIENT_ID' => 22, |
||
| 38 | 'CAN_DO_TIMEOUT' => 23, |
||
| 39 | 'ALL_YOURS' => 24, |
||
| 40 | 'WORK_EXCEPTION' => 25, |
||
| 41 | 'OPTION_REQ' => 26, |
||
| 42 | 'OPTION_RES' => 27, |
||
| 43 | 'WORK_DATA' => 28, |
||
| 44 | 'WORK_WARNING' => 29, |
||
| 45 | 'GRAB_JOB_UNIQ' => 30, |
||
| 46 | 'SUBMIT_JOB_HIGH_BG' => 32, |
||
| 47 | 'SUBMIT_JOB_LOW' => 33, |
||
| 48 | 'SUBMIT_JOB_LOW_BG' => 34, |
||
| 49 | 'SUBMIT_JOB_SCHED' => 35, |
||
| 50 | 'SUBMIT_JOB_EPOCH' => 36, |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Response codes |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | private $responseCommandList = [ |
||
| 59 | 'NOOP' => 6, |
||
| 60 | 'JOB_CREATED' => 8, |
||
| 61 | 'NO_JOB' => 10, |
||
| 62 | 'JOB_ASSIGN' => 11, |
||
| 63 | 'WORK_STATUS' => 12, |
||
| 64 | 'WORK_COMPLETE' => 13, |
||
| 65 | 'WORK_FAIL' => 14, |
||
| 66 | 'ECHO_RES' => 17, |
||
| 67 | 'ERROR' => 19, |
||
| 68 | 'STATUS_RES' => 20, |
||
| 69 | 'WORK_EXCEPTION' => 25, |
||
| 70 | 'OPTION_RES' => 27, |
||
| 71 | 'WORK_WARNING' => 29, |
||
| 72 | 'JOB_ASSIGN_UNIQ' => 31, |
||
| 73 | ]; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Magic code for request |
||
| 77 | */ |
||
| 78 | const MAGIC_REQUEST = "\0REQ"; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Magic code for response |
||
| 82 | */ |
||
| 83 | const MAGIC_RESPONSE = "\0RES"; |
||
| 84 | |||
| 85 | /* |
||
| 86 | * Byte length of header |
||
| 87 | */ |
||
| 88 | const HEADER_LENGTH = 12; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Header binary format |
||
| 92 | */ |
||
| 93 | const HEADER_WRITE_FORMAT = "a4NN"; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Header read format |
||
| 97 | */ |
||
| 98 | const HEADER_READ_FORMAT = "a4magic/Ntype/Nsize"; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Delimeter for function arguments |
||
| 102 | */ |
||
| 103 | const ARGS_DELIMITER = "\0"; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Flag |
||
| 107 | * @var bool |
||
| 108 | */ |
||
| 109 | private $jobAwaitResult = false; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Generate a random string token |
||
| 113 | * |
||
| 114 | * @param int $length How many characters do we want? |
||
| 115 | * @param string $keySpaces A string of all possible characters to select from |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | private function generateUniqueToken($length = 10, $keySpaces = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Called when new data received |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | public function onRead() { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Function send ECHO |
||
| 168 | * |
||
| 169 | * @param $payload |
||
| 170 | * @param callable|null $cb |
||
| 171 | */ |
||
| 172 | public function sendEcho ($payload, callable $cb = null) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Function run task and wait result in callback |
||
| 178 | * |
||
| 179 | * @param $function_name |
||
| 180 | * @param $payload |
||
| 181 | * @param null $context |
||
| 182 | * @param null $unique |
||
| 183 | */ |
||
| 184 | View Code Duplication | public function runJob($function_name, $payload, callable $resultCallback, $unique = null) { |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Function run task and wait result in callback |
||
| 196 | * |
||
| 197 | * @param $function_name |
||
| 198 | * @param $payload |
||
| 199 | * @param null $context |
||
| 200 | * @param null $unique |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function doNormal($function_name, $payload, callable $resultCallback, $unique = null) { |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Function run task in background |
||
| 213 | * |
||
| 214 | * @param $function_name |
||
| 215 | * @param $payload |
||
| 216 | * @param null $context |
||
| 217 | * @param null $unique |
||
| 218 | */ |
||
| 219 | View Code Duplication | public function doBackground($function_name, $payload, callable $resultCallback, $unique = null) { |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Function run task with high prority |
||
| 230 | * |
||
| 231 | * @param $function_name |
||
| 232 | * @param $payload |
||
| 233 | * @param null $context |
||
| 234 | * @param stirng $unique |
||
| 235 | */ |
||
| 236 | View Code Duplication | public function doHigh($function_name, $payload, callable $resultCallback, $unique = null) { |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Function run task in background with high prority |
||
| 247 | * |
||
| 248 | * @param $function_name |
||
| 249 | * @param $payload |
||
| 250 | * @param null $context |
||
| 251 | * @param null $unique |
||
| 252 | */ |
||
| 253 | View Code Duplication | public function doHighBackground($function_name, $payload, callable $resultCallback, $unique = null) { |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Function run task with low priority |
||
| 264 | * |
||
| 265 | * @param $function_name |
||
| 266 | * @param $payload |
||
| 267 | * @param null $context |
||
| 268 | * @param null $unique |
||
| 269 | */ |
||
| 270 | View Code Duplication | public function doLow($function_name, $payload, callable $resultCallback, $unique = null) { |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Function run task in background with low priority |
||
| 281 | * |
||
| 282 | * @param $function_name |
||
| 283 | * @param $payload |
||
| 284 | * @param null $context |
||
| 285 | * @param null $unique |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function doLowBackground($function_name, $payload, callable $resultCallback, $unique = null) { |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Get job status |
||
| 298 | * |
||
| 299 | * @param $job_handle Job handle that was given in JOB_CREATED packet. |
||
| 300 | * |
||
| 301 | */ |
||
| 302 | public function doStatus($job_handle, callable $resultCallback) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Function set settings for current connection |
||
| 308 | * Available settings |
||
| 309 | * 'exceptions' - Forward WORK_EXCEPTION packets to the client. |
||
| 310 | * |
||
| 311 | * @url http://gearman.org/protocol/ |
||
| 312 | * |
||
| 313 | * |
||
| 314 | * @param int $option_name |
||
| 315 | * @param callable $doneCallback |
||
| 316 | */ |
||
| 317 | public function setConnectionOption($option_name, callable $doneCallback) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Low level commands sender |
||
| 323 | * |
||
| 324 | * @param $commandName |
||
| 325 | * @param $payload |
||
| 326 | * @param $doneCallback callable|null $doneCallback |
||
| 327 | */ |
||
| 328 | private function sendCommand($commandName, $payload, callable $doneCallback = null) { |
||
| 344 | } |