| Conditions | 17 |
| Paths | 197 |
| Total Lines | 84 |
| Code Lines | 47 |
| Lines | 7 |
| Ratio | 8.33 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 60 | public function add( |
||
| 61 | string $id, |
||
| 62 | int $minsBeforeExpire = PHP_INT_MAX, |
||
| 63 | int $maxGlobalProcesses = 1, |
||
| 64 | int $maxHostProcesses = 1 |
||
| 65 | ) : bool { |
||
| 66 | $thisHostName = self::_getEncodedHostname(); |
||
| 67 | $thisPid = getmypid(); |
||
| 68 | |||
| 69 | //loop in case the update fails its optimistic concurrency check |
||
| 70 | for ($i = 0; $i < 5; ++$i) { |
||
| 71 | $this->collection->findOneAndUpdate( |
||
| 72 | ['_id' => $id], |
||
| 73 | ['$setOnInsert' => ['hosts' => [], 'version' => new ObjectID()]], |
||
| 74 | ['upsert' => true] |
||
| 75 | ); |
||
| 76 | $existing = $this->collection->findOne( |
||
| 77 | ['_id' => $id], |
||
| 78 | ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] |
||
| 79 | ); |
||
| 80 | |||
| 81 | $replacement = $existing; |
||
| 82 | $replacement['version'] = new ObjectID(); |
||
| 83 | |||
| 84 | //clean $replacement based on their pids and expire times |
||
| 85 | foreach ($existing['hosts'] as $hostname => $pids) { |
||
| 86 | foreach ($pids as $pid => $expires) { |
||
| 87 | //our machine and not running |
||
| 88 | //the task expired |
||
| 89 | //our machine and pid is recycled (should rarely happen) |
||
| 90 | if (($hostname === $thisHostName && !file_exists("/proc/{$pid}")) |
||
| 91 | || time() > $expires |
||
| 92 | || ($hostname === $thisHostName && $pid === $thisPid) |
||
| 93 | ) { |
||
| 94 | unset($replacement['hosts'][$hostname][$pid]); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | if (empty($replacement['hosts'][$hostname])) { |
||
| 99 | unset($replacement['hosts'][$hostname]); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | $totalPidCount = 0; |
||
| 104 | foreach ($replacement['hosts'] as $hostname => $pids) { |
||
| 105 | $totalPidCount += count($pids); |
||
| 106 | } |
||
| 107 | |||
| 108 | $thisHostPids = array_key_exists($thisHostName, $replacement['hosts']) ? $replacement['hosts'][$thisHostName] : []; |
||
| 109 | |||
| 110 | if ($totalPidCount >= $maxGlobalProcesses || count($thisHostPids) >= $maxHostProcesses) { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | // add our process |
||
| 115 | $expireSecs = time() + $minsBeforeExpire * 60; |
||
| 116 | View Code Duplication | if (!is_int($expireSecs)) { |
|
| 117 | if ($minsBeforeExpire > 0) { |
||
| 118 | $expireSecs = self::MONGO_INT32_MAX; |
||
| 119 | } else { |
||
| 120 | $expireSecs = 0; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | $thisHostPids[$thisPid] = $expireSecs; |
||
| 125 | $replacement['hosts'][$thisHostName] = $thisHostPids; |
||
| 126 | |||
| 127 | $status = $this->collection->replaceOne( |
||
| 128 | ['_id' => $existing['_id'], 'version' => $existing['version']], |
||
| 129 | $replacement, |
||
| 130 | ['writeConcern' => new \MongoDB\Driver\WriteConcern(1, 100, true)] |
||
| 131 | ); |
||
| 132 | if ($status->getMatchedCount() === 1) { |
||
| 133 | return true; |
||
| 134 | } |
||
| 135 | |||
| 136 | //@codeCoverageIgnoreStart |
||
| 137 | //hard to test the optimistic concurrency check |
||
| 138 | } |
||
| 139 | |||
| 140 | //too much concurrency at the moment, return false to signify not added. |
||
| 141 | return false; |
||
| 142 | //@codeCoverageIgnoreEnd |
||
| 143 | } |
||
| 144 | |||
| 207 |