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 Queue 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 Queue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | final class Queue implements QueueInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Maximum millisecond value to use for UTCDateTime creation. |
||
| 20 | * |
||
| 21 | * @var integer |
||
| 22 | */ |
||
| 23 | const MONGO_INT32_MAX = PHP_INT_MAX; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * mongo collection to use for queue. |
||
| 27 | * |
||
| 28 | * @var \MongoDB\Collection |
||
| 29 | */ |
||
| 30 | private $collection; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Construct queue. |
||
| 34 | * |
||
| 35 | * @param \MongoDB\Collection|string $collectionOrUrl A MongoCollection instance or the mongo connection url. |
||
| 36 | * @param string $db the mongo db name |
||
| 37 | * @param string $collection the collection name to use for the queue |
||
| 38 | * |
||
| 39 | * @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string |
||
| 40 | */ |
||
| 41 | public function __construct($collectionOrUrl, $db = null, $collection = null) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Ensure an index for the get() method. |
||
| 67 | * |
||
| 68 | * @param array $beforeSort Fields in get() call to index before the sort field in same format |
||
| 69 | * as \MongoDB\Collection::ensureIndex() |
||
| 70 | * @param array $afterSort Fields in get() call to index after the sort field in same format as |
||
| 71 | * \MongoDB\Collection::ensureIndex() |
||
| 72 | * |
||
| 73 | * @return void |
||
| 74 | * |
||
| 75 | * @throws \InvalidArgumentException value of $beforeSort or $afterSort is not 1 or -1 for ascending and descending |
||
| 76 | * @throws \InvalidArgumentException key in $beforeSort or $afterSort was not a string |
||
| 77 | */ |
||
| 78 | public function ensureGetIndex(array $beforeSort = [], array $afterSort = []) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Ensure an index for the count() method. |
||
| 96 | * Is a no-op if the generated index is a prefix of an existing one. If you have a similar ensureGetIndex call, |
||
| 97 | * call it first. |
||
| 98 | * |
||
| 99 | * @param array $fields fields in count() call to index in same format as \MongoDB\Collection::createIndex() |
||
| 100 | * @param bool $includeRunning whether to include the running field in the index |
||
| 101 | * |
||
| 102 | * @return void |
||
| 103 | * |
||
| 104 | * @throws \InvalidArgumentException $includeRunning was not a boolean |
||
| 105 | * @throws \InvalidArgumentException key in $fields was not a string |
||
| 106 | * @throws \InvalidArgumentException value of $fields is not 1 or -1 for ascending and descending |
||
| 107 | */ |
||
| 108 | public function ensureCountIndex(array $fields, $includeRunning) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get a non running message from the queue. |
||
| 127 | * |
||
| 128 | * @param array $query in same format as \MongoDB\Collection::find() where top level fields do not contain operators. |
||
| 129 | * Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, |
||
| 130 | * invalid {$and: [{...}, {...}]} |
||
| 131 | * @param int $runningResetDuration second duration the message can stay unacked before it resets and can be |
||
| 132 | * retreived again. |
||
| 133 | * @param int $waitDurationInMillis millisecond duration to wait for a message. |
||
| 134 | * @param int $pollDurationInMillis millisecond duration to wait between polls. |
||
| 135 | * |
||
| 136 | * @return array|null the message or null if one is not found |
||
| 137 | * |
||
| 138 | * @throws \InvalidArgumentException $runningResetDuration, $waitDurationInMillis or $pollDurationInMillis was not |
||
| 139 | * an int |
||
| 140 | * @throws \InvalidArgumentException key in $query was not a string |
||
| 141 | */ |
||
| 142 | public function get(array $query, $runningResetDuration, $waitDurationInMillis = 3000, $pollDurationInMillis = 200) |
||
| 212 | //@codeCoverageIgnoreEnd |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Count queue messages. |
||
| 216 | * |
||
| 217 | * @param array $query in same format as \MongoDB\Collection::find() where top level fields do not contain operators. |
||
| 218 | * Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, invalid {$and: [{...}, {...}]} |
||
| 219 | * @param bool|null $running query a running message or not or all |
||
| 220 | * |
||
| 221 | * @return int the count |
||
| 222 | * |
||
| 223 | * @throws \InvalidArgumentException $running was not null and not a bool |
||
| 224 | * @throws \InvalidArgumentException key in $query was not a string |
||
| 225 | */ |
||
| 226 | public function count(array $query, $running = null) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Acknowledge a message was processed and remove from queue. |
||
| 252 | * |
||
| 253 | * @param array $message message received from get() |
||
| 254 | * |
||
| 255 | * @return void |
||
| 256 | * |
||
| 257 | * @throws \InvalidArgumentException $message does not have a field "id" that is a MongoDB\BSON\ObjectID |
||
| 258 | */ |
||
| 259 | public function ack(array $message) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Atomically acknowledge and send a message to the queue. |
||
| 275 | * |
||
| 276 | * @param array $message the message to ack received from get() |
||
| 277 | * @param array $payload the data to store in the message to send. Data is handled same way |
||
| 278 | * as \MongoDB\Collection::insertOne() |
||
| 279 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 280 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 281 | * @param bool $newTimestamp true to give the payload a new timestamp or false to use given message timestamp |
||
| 282 | * |
||
| 283 | * @return void |
||
| 284 | * |
||
| 285 | * @throws \InvalidArgumentException $message does not have a field "id" that is a ObjectID |
||
| 286 | * @throws \InvalidArgumentException $earliestGet was not an int |
||
| 287 | * @throws \InvalidArgumentException $priority was not a float |
||
| 288 | * @throws \InvalidArgumentException $priority is NaN |
||
| 289 | * @throws \InvalidArgumentException $newTimestamp was not a bool |
||
| 290 | */ |
||
| 291 | public function ackSend(array $message, array $payload, $earliestGet = 0, $priority = 0.0, $newTimestamp = true) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Requeue message to the queue. Same as ackSend() with the same message. |
||
| 337 | * |
||
| 338 | * @param array $message message received from get(). |
||
| 339 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 340 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 341 | * @param bool $newTimestamp true to give the payload a new timestamp or false to use given message timestamp |
||
| 342 | * |
||
| 343 | * @return void |
||
| 344 | * |
||
| 345 | * @throws \InvalidArgumentException $message does not have a field "id" that is a ObjectID |
||
| 346 | * @throws \InvalidArgumentException $earliestGet was not an int |
||
| 347 | * @throws \InvalidArgumentException $priority was not a float |
||
| 348 | * @throws \InvalidArgumentException priority is NaN |
||
| 349 | * @throws \InvalidArgumentException $newTimestamp was not a bool |
||
| 350 | */ |
||
| 351 | public function requeue(array $message, $earliestGet = 0, $priority = 0.0, $newTimestamp = true) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Send a message to the queue. |
||
| 360 | * |
||
| 361 | * @param array $payload the data to store in the message. Data is handled same way as \MongoDB\Collection::insertOne() |
||
| 362 | * @param int $earliestGet earliest unix timestamp the message can be retreived. |
||
| 363 | * @param float $priority priority for order out of get(). 0 is higher priority than 1 |
||
| 364 | * |
||
| 365 | * @return void |
||
| 366 | * |
||
| 367 | * @throws \InvalidArgumentException $earliestGet was not an int |
||
| 368 | * @throws \InvalidArgumentException $priority was not a float |
||
| 369 | * @throws \InvalidArgumentException $priority is NaN |
||
| 370 | */ |
||
| 371 | public function send(array $payload, $earliestGet = 0, $priority = 0.0) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Ensure index of correct specification and a unique name whether the specification or name already exist or not. |
||
| 400 | * Will not create index if $index is a prefix of an existing index |
||
| 401 | * |
||
| 402 | * @param array $index index to create in same format as \MongoDB\Collection::createIndex() |
||
| 403 | * |
||
| 404 | * @return void |
||
| 405 | * |
||
| 406 | * @throws \Exception couldnt create index after 5 attempts |
||
| 407 | */ |
||
| 408 | private function ensureIndex(array $index) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Helper method to validate keys and values for the given sort array |
||
| 444 | * |
||
| 445 | * @param array $sort The proposed sort for a mongo index. |
||
| 446 | * @param string $label The name of the variable given to the public ensureXIndex method. |
||
| 447 | * @param array &$completedFields The final index array with payload. prefix added to fields. |
||
| 448 | * |
||
| 449 | * @return void |
||
| 450 | */ |
||
| 451 | private static function verifySort(array $sort, $label, &$completeFields) |
||
| 467 | } |
||
| 468 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.