Conditions | 12 |
Paths | 225 |
Total Lines | 69 |
Code Lines | 43 |
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 | $upsertedIds = []; |
||
131 | foreach ($result->getUpsertedIds() as $index => $id) { |
||
132 | $upsertedIds[] = [ |
||
133 | 'index' => $index, |
||
134 | '_id' => TypeConverter::toLegacy($id) |
||
135 | ]; |
||
136 | } |
||
137 | |||
138 | $result = [ |
||
139 | 'nMatched' => $result->getMatchedCount(), |
||
140 | 'nModified' => $result->getModifiedCount(), |
||
141 | 'nUpserted' => $result->getUpsertedCount(), |
||
142 | 'ok' => $ok, |
||
143 | ]; |
||
144 | |||
145 | if (count($upsertedIds)) { |
||
146 | $result['upserted'] = $upsertedIds; |
||
147 | } |
||
148 | |||
149 | return $result; |
||
150 | |||
151 | case self::COMMAND_DELETE: |
||
152 | return [ |
||
153 | 'nRemoved' => $result->getDeletedCount(), |
||
154 | 'ok' => $ok, |
||
155 | ]; |
||
156 | |||
157 | case self::COMMAND_INSERT: |
||
158 | return [ |
||
159 | 'nInserted' => $result->getInsertedCount(), |
||
160 | 'ok' => $ok, |
||
161 | ]; |
||
162 | } |
||
163 | } |
||
164 | |||
214 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.