| Conditions | 15 |
| Paths | 257 |
| Total Lines | 105 |
| Lines | 30 |
| Ratio | 28.57 % |
| 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 |
||
| 102 | final public function execute(array $writeOptions = []) |
||
| 103 | { |
||
| 104 | $writeOptions += $this->writeOptions; |
||
| 105 | if (! count($this->items)) { |
||
| 106 | return ['ok' => true]; |
||
| 107 | } |
||
| 108 | |||
| 109 | if (isset($writeOptions['j'])) { |
||
| 110 | trigger_error('j parameter is not supported', E_USER_WARNING); |
||
| 111 | } |
||
| 112 | if (isset($writeOptions['fsync'])) { |
||
| 113 | trigger_error('fsync parameter is not supported', E_USER_WARNING); |
||
| 114 | } |
||
| 115 | |||
| 116 | $options['writeConcern'] = $this->createWriteConcernFromArray($writeOptions); |
||
|
|
|||
| 117 | if (isset($writeOptions['ordered'])) { |
||
| 118 | $options['ordered'] = $writeOptions['ordered']; |
||
| 119 | } |
||
| 120 | |||
| 121 | try { |
||
| 122 | $writeResult = $this->collection->getCollection()->bulkWrite($this->items, $options); |
||
| 123 | $resultDocument = []; |
||
| 124 | $ok = true; |
||
| 125 | } catch (BulkWriteException $e) { |
||
| 126 | $writeResult = $e->getWriteResult(); |
||
| 127 | $resultDocument = ['writeErrors' => $this->convertWriteErrors($writeResult)]; |
||
| 128 | $ok = false; |
||
| 129 | } |
||
| 130 | |||
| 131 | $this->items = []; |
||
| 132 | |||
| 133 | switch ($this->batchType) { |
||
| 134 | case self::COMMAND_UPDATE: |
||
| 135 | if ($options['writeConcern']->getW() === 0) { |
||
| 136 | $resultDocument += [ |
||
| 137 | 'nMatched' => 0, |
||
| 138 | 'nModified' => 0, |
||
| 139 | 'nUpserted' => 0, |
||
| 140 | 'ok' => true, |
||
| 141 | ]; |
||
| 142 | |||
| 143 | break; |
||
| 144 | } |
||
| 145 | |||
| 146 | $upsertedIds = []; |
||
| 147 | foreach ($writeResult->getUpsertedIds() as $index => $id) { |
||
| 148 | $upsertedIds[] = [ |
||
| 149 | 'index' => $index, |
||
| 150 | '_id' => TypeConverter::toLegacy($id) |
||
| 151 | ]; |
||
| 152 | } |
||
| 153 | |||
| 154 | $resultDocument += [ |
||
| 155 | 'nMatched' => $writeResult->getMatchedCount(), |
||
| 156 | 'nModified' => $writeResult->getModifiedCount(), |
||
| 157 | 'nUpserted' => $writeResult->getUpsertedCount(), |
||
| 158 | 'ok' => true, |
||
| 159 | ]; |
||
| 160 | |||
| 161 | if (count($upsertedIds)) { |
||
| 162 | $resultDocument['upserted'] = $upsertedIds; |
||
| 163 | } |
||
| 164 | break; |
||
| 165 | |||
| 166 | View Code Duplication | case self::COMMAND_DELETE: |
|
| 167 | if ($options['writeConcern']->getW() === 0) { |
||
| 168 | $resultDocument += [ |
||
| 169 | 'nRemoved' => 0, |
||
| 170 | 'ok' => true, |
||
| 171 | ]; |
||
| 172 | |||
| 173 | break; |
||
| 174 | } |
||
| 175 | |||
| 176 | $resultDocument += [ |
||
| 177 | 'nRemoved' => $writeResult->getDeletedCount(), |
||
| 178 | 'ok' => true, |
||
| 179 | ]; |
||
| 180 | break; |
||
| 181 | |||
| 182 | View Code Duplication | case self::COMMAND_INSERT: |
|
| 183 | if ($options['writeConcern']->getW() === 0) { |
||
| 184 | $resultDocument += [ |
||
| 185 | 'nInserted' => 0, |
||
| 186 | 'ok' => true, |
||
| 187 | ]; |
||
| 188 | |||
| 189 | break; |
||
| 190 | } |
||
| 191 | |||
| 192 | $resultDocument += [ |
||
| 193 | 'nInserted' => $writeResult->getInsertedCount(), |
||
| 194 | 'ok' => true, |
||
| 195 | ]; |
||
| 196 | break; |
||
| 197 | } |
||
| 198 | |||
| 199 | if (! $ok) { |
||
| 200 | // Exception code is hardcoded to the value in ext-mongo, see |
||
| 201 | // https://github.com/mongodb/mongo-php-driver-legacy/blob/ab4bc0d90e93b3f247f6bcb386d0abc8d2fa7d74/batch/write.c#L428 |
||
| 202 | throw new \MongoWriteConcernException('Failed write', 911, null, $resultDocument); |
||
| 203 | } |
||
| 204 | |||
| 205 | return $resultDocument; |
||
| 206 | } |
||
| 207 | |||
| 275 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.