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 |
||
| 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 | * Construct a new instance of the registry. |
||
| 41 | * |
||
| 42 | * @param Collection $collection The MongoDB collection containing the process information. |
||
| 43 | */ |
||
| 44 | public function __construct(Collection $collection) |
||
| 45 | { |
||
| 46 | $this->collection = $collection; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Add to process registry. Adds based on $maxGlobalProcesses and $maxHostProcesses after a process registry |
||
| 51 | * cleaning. |
||
| 52 | * |
||
| 53 | * @param string $id A unique id. |
||
| 54 | * @param integer $minsBeforeExpire Number of minutes before a process is considered expired. |
||
| 55 | * @param integer $maxGlobalProcesses Max processes of an id allowed to run across all hosts. |
||
| 56 | * @param integer $maxHostProcesses Max processes of an id allowed to run across a single host. |
||
| 57 | * |
||
| 58 | * @return boolean true if the process was added, false if not or there is too much concurrency at the moment. |
||
| 59 | */ |
||
| 60 | public function add( |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Removes from process registry. Does not do anything needed for use of the add() method. Most will only use at the |
||
| 147 | * end of their script so the mongo collection is up to date. |
||
| 148 | * |
||
| 149 | * @param string $id A unique id. |
||
| 150 | * |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | public function remove(string $id) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Reset a process expire time in the registry. |
||
| 166 | * |
||
| 167 | * @param string $id A unique id. |
||
| 168 | * @param integer $minsBeforeExpire Number of minutes before a process is considered expired. |
||
| 169 | * |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | public function reset(string $id, int $minsBeforeExpire) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Encodes '.' and '$' to be used as a mongo field name. |
||
| 199 | * |
||
| 200 | * @return string the encoded hostname from gethostname(). |
||
| 201 | */ |
||
| 202 | private static function _getEncodedHostname() : string |
||
| 206 | } |
||
| 207 |