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, string $db = null, string $collection = null) |
||
60 | } |
||
61 |