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 |
||
11 | /** |
||
12 | * Class that adds/removes from a process registry. |
||
13 | */ |
||
14 | final class ProcessRegistry implements ProcessRegistryInterface |
||
15 | { |
||
16 | /** example doc: |
||
17 | * { |
||
18 | * '_id': 'a unique id', |
||
19 | * 'hosts': { |
||
20 | * 'a hostname' : { |
||
21 | * 'a pid': expire timestamp, |
||
22 | * ... |
||
23 | * }, |
||
24 | * ... |
||
25 | * }, |
||
26 | * 'version' => ObjectID(an id), |
||
27 | * } |
||
28 | */ |
||
29 | |||
30 | const MONGO_INT32_MAX = 2147483647;//2147483648 can overflow in php mongo without using the MongoInt64 |
||
31 | |||
32 | /** |
||
33 | * MongoDB collection containing the process information. |
||
34 | * |
||
35 | * @var Collection |
||
36 | */ |
||
37 | private $collection; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $encodedHostName; |
||
43 | |||
44 | /** |
||
45 | * @var integer |
||
46 | */ |
||
47 | private $pid; |
||
48 | |||
49 | /** |
||
50 | * Construct a new instance of the registry. |
||
51 | * |
||
52 | * @param Collection $collection The MongoDB collection containing the process information. |
||
53 | */ |
||
54 | public function __construct(Collection $collection) |
||
60 | |||
61 | /** |
||
62 | * Add to process registry. Adds based on $maxGlobalProcesses and $maxHostProcesses after a process registry |
||
63 | * cleaning. |
||
64 | * |
||
65 | * @param ProcessInterface $process The process to add. |
||
66 | * |
||
67 | * @return boolean true if the process was added, false if not or there is too much concurrency at the moment. |
||
68 | */ |
||
69 | public function add(ProcessInterface $process) : bool |
||
95 | |||
96 | private function updateExistingDocument(string $id, ObjectID $version, array $replacement) : bool |
||
106 | |||
107 | private function ensureProcessDocumentExists(string $id) |
||
115 | |||
116 | private function generateReplacementDocument(array $existing) : array |
||
131 | |||
132 | private function getExistingProcessDocument(string $id) : array |
||
139 | |||
140 | private function removeExpiredPids(array $pids, string $hostname, array &$replacement) |
||
148 | |||
149 | private function canRemovePid(string $hostname, int $pid, int $expires) : bool |
||
155 | |||
156 | private function getTotalPidCount(array $document) : int |
||
165 | |||
166 | private function getExpiresSeconds(int $minsBeforeExpire) : int |
||
172 | |||
173 | /** |
||
174 | * Removes from process registry. Does not do anything needed for use of the add() method. Most will only use at the |
||
175 | * end of their script so the mongo collection is up to date. |
||
176 | * |
||
177 | * @param ProcessInterface $process The process to remove. |
||
178 | * |
||
179 | * @return void |
||
180 | */ |
||
181 | public function remove(ProcessInterface $process) |
||
188 | |||
189 | /** |
||
190 | * Reset a process expire time in the registry. |
||
191 | * |
||
192 | * @param ProcessInterface $process The process to reset. |
||
193 | * |
||
194 | * @return void |
||
195 | */ |
||
196 | public function reset(ProcessInterface $process) |
||
212 | } |
||
213 |