| Conditions | 10 |
| Paths | 129 |
| Total Lines | 55 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 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 |
||
| 95 | final public function execute(array $writeOptions = []) |
||
| 96 | { |
||
| 97 | $writeOptions += $this->writeOptions; |
||
| 98 | if (! count($this->items)) { |
||
| 99 | return ['ok' => true]; |
||
| 100 | } |
||
| 101 | |||
| 102 | if (isset($writeOptions['j'])) { |
||
| 103 | trigger_error('j parameter is not supported', E_WARNING); |
||
| 104 | } |
||
| 105 | if (isset($writeOptions['fsync'])) { |
||
| 106 | trigger_error('fsync parameter is not supported', E_WARNING); |
||
| 107 | } |
||
| 108 | |||
| 109 | $options['writeConcern'] = $this->createWriteConcernFromArray($writeOptions); |
||
|
|
|||
| 110 | if (isset($writeOptions['ordered'])) { |
||
| 111 | $options['ordered'] = $writeOptions['ordered']; |
||
| 112 | } |
||
| 113 | |||
| 114 | $collection = $this->collection->getCollection(); |
||
| 115 | |||
| 116 | try { |
||
| 117 | $result = $collection->BulkWrite($this->items, $options); |
||
| 118 | $ok = true; |
||
| 119 | } catch (\MongoDB\Driver\Exception\BulkWriteException $e) { |
||
| 120 | $result = $e->getWriteResult(); |
||
| 121 | $ok = false; |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($ok === true) { |
||
| 125 | $this->items = []; |
||
| 126 | } |
||
| 127 | |||
| 128 | switch ($this->batchType) { |
||
| 129 | case self::COMMAND_UPDATE: |
||
| 130 | return [ |
||
| 131 | 'nMatched' => $result->getMatchedCount(), |
||
| 132 | 'nModified' => $result->getModifiedCount(), |
||
| 133 | 'nUpserted' => $result->getUpsertedCount(), |
||
| 134 | 'ok' => $ok, |
||
| 135 | ]; |
||
| 136 | |||
| 137 | case self::COMMAND_DELETE: |
||
| 138 | return [ |
||
| 139 | 'nRemoved' => $result->getDeletedCount(), |
||
| 140 | 'ok' => $ok, |
||
| 141 | ]; |
||
| 142 | |||
| 143 | case self::COMMAND_INSERT: |
||
| 144 | return [ |
||
| 145 | 'nInserted' => $result->getInsertedCount(), |
||
| 146 | 'ok' => $ok, |
||
| 147 | ]; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 200 |
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.