| Conditions | 21 |
| Paths | 201 |
| Total Lines | 100 |
| Code Lines | 54 |
| Lines | 7 |
| Ratio | 7 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 45 | public static function add( |
||
| 46 | \MongoDB\Collection $collection, |
||
| 47 | $id, |
||
| 48 | $minsBeforeExpire = PHP_INT_MAX, |
||
| 49 | $maxGlobalProcesses = 1, |
||
| 50 | $maxHostProcesses = 1 |
||
| 51 | ) |
||
| 52 | { |
||
| 53 | if (!is_string($id)) { |
||
| 54 | throw new \InvalidArgumentException('$id was not a string'); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (!is_int($minsBeforeExpire)) { |
||
| 58 | throw new \InvalidArgumentException('$minsBeforeExpire was not an int'); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (!is_int($maxGlobalProcesses)) { |
||
| 62 | throw new \InvalidArgumentException('$maxGlobalProcesses was not an int'); |
||
| 63 | } |
||
| 64 | |||
| 65 | if (!is_int($maxHostProcesses)) { |
||
| 66 | throw new \InvalidArgumentException('$maxHostProcesses was not an int'); |
||
| 67 | } |
||
| 68 | |||
| 69 | $thisHostName = self::_getEncodedHostname(); |
||
| 70 | $thisPid = getmypid(); |
||
| 71 | |||
| 72 | //loop in case the update fails its optimistic concurrency check |
||
| 73 | for ($i = 0; $i < 5; ++$i) { |
||
| 74 | $collection->findOneAndUpdate( |
||
| 75 | ['_id' => $id], |
||
| 76 | ['$setOnInsert' => ['hosts' => [], 'version' => new \MongoDB\BSON\ObjectID()]], |
||
| 77 | ['upsert' => true] |
||
| 78 | ); |
||
| 79 | $existing = $collection->findOne(['_id' => $id], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]); |
||
| 80 | |||
| 81 | $replacement = $existing; |
||
| 82 | $replacement['version'] = new \MongoDB\BSON\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 ( |
||
| 91 | ($hostname === $thisHostName && !file_exists("/proc/{$pid}")) |
||
| 92 | || time() >= $expires->toDateTime()->getTimestamp() |
||
| 93 | || ($hostname === $thisHostName && $pid === $thisPid) |
||
| 94 | ) { |
||
| 95 | unset($replacement['hosts'][$hostname][$pid]); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | if (empty($replacement['hosts'][$hostname])) { |
||
| 100 | unset($replacement['hosts'][$hostname]); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | $totalPidCount = 0; |
||
| 105 | foreach ($replacement['hosts'] as $hostname => $pids) { |
||
| 106 | $totalPidCount += count($pids); |
||
| 107 | } |
||
| 108 | |||
| 109 | $thisHostPids = array_key_exists($thisHostName, $replacement['hosts']) ? $replacement['hosts'][$thisHostName] : []; |
||
| 110 | |||
| 111 | if ($totalPidCount >= $maxGlobalProcesses || count($thisHostPids) >= $maxHostProcesses) { |
||
| 112 | return false; |
||
| 113 | } |
||
| 114 | |||
| 115 | // add our process |
||
| 116 | $expireSecs = time() + $minsBeforeExpire * 60; |
||
| 117 | View Code Duplication | if (!is_int($expireSecs)) { |
|
|
|
|||
| 118 | if ($minsBeforeExpire > 0) { |
||
| 119 | $expireSecs = self::MONGO_INT32_MAX; |
||
| 120 | } else { |
||
| 121 | $expireSecs = 0; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | $thisHostPids[$thisPid] = new \MongoDB\BSON\UTCDateTime($expireSecs * 1000); |
||
| 126 | $replacement['hosts'][$thisHostName] = $thisHostPids; |
||
| 127 | |||
| 128 | $status = $collection->replaceOne( |
||
| 129 | ['_id' => $existing['_id'], 'version' => $existing['version']], |
||
| 130 | $replacement, |
||
| 131 | ['writeConcern' => new \MongoDB\Driver\WriteConcern(1, 100, true)] |
||
| 132 | ); |
||
| 133 | if ($status->getMatchedCount() === 1) { |
||
| 134 | return true; |
||
| 135 | } |
||
| 136 | |||
| 137 | //@codeCoverageIgnoreStart |
||
| 138 | //hard to test the optimistic concurrency check |
||
| 139 | } |
||
| 140 | |||
| 141 | //too much concurrency at the moment, return false to signify not added. |
||
| 142 | return false; |
||
| 143 | //@codeCoverageIgnoreEnd |
||
| 144 | } |
||
| 145 | |||
| 227 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.