Conditions | 18 |
Paths | 96 |
Total Lines | 85 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
115 | public function setFields($collectionName, $fields) |
||
116 | { |
||
117 | $collection = $this->selectCollection($collectionName); |
||
118 | /* |
||
119 | * Rename Columns |
||
120 | */ |
||
121 | $renames = []; |
||
122 | foreach ($fields as $field) { |
||
123 | if ($field->oldName != "") { |
||
124 | if ($field->oldName != $field->name) { |
||
125 | $renames[$field->oldName] = $field->name; |
||
126 | } |
||
127 | |||
128 | } |
||
129 | } |
||
130 | $update = []; |
||
131 | if (count($renames) > 0) { |
||
132 | $update['$rename'] = $renames; |
||
133 | $collection->updateMany(array(), $update, array('upsert' => true)); |
||
134 | foreach ($renames as $oldName => $newName) { |
||
135 | $collection_field = CollectionField::where('old_name', $oldName)->first(); |
||
136 | $collection_field->old_name = $newName; |
||
137 | $collection_field->update(); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | $newFields = $this->getColumnsName($collectionName); |
||
142 | $columns = $this->getCollectionColumns($collectionName); |
||
143 | |||
144 | /* |
||
145 | * Add Columns |
||
146 | */ |
||
147 | |||
148 | if ($collection->count() > 0) { |
||
149 | $sets = []; |
||
150 | foreach ($newFields as $newField) { |
||
151 | $cursor = $collection->find(); |
||
152 | $iterator = iterator_to_array($cursor); |
||
153 | |||
154 | foreach ($iterator as $document) { |
||
155 | $columnNames = []; |
||
156 | $id = ""; |
||
157 | foreach ($document as $columnName => $columnValue) { |
||
158 | if (is_object($columnValue)) { |
||
159 | foreach ($columnValue as $key => $value) { |
||
160 | if ($columnName == '_id') { |
||
161 | $id = $value; |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | $columnNames[] = $columnName; |
||
166 | } |
||
167 | |||
168 | if ($id != "" && !in_array($newField, $columnNames)) { |
||
169 | $update['$set'] = array($newField => ""); |
||
170 | $collection->updateOne( |
||
171 | array("_id" => new \MongoDB\BSON\ObjectID($id)), |
||
172 | $update, |
||
173 | array('upsert' => true) |
||
174 | ); |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | |||
179 | } |
||
180 | |||
181 | /* |
||
182 | * Remove Columns |
||
183 | */ |
||
184 | |||
185 | $update = []; |
||
186 | |||
187 | $unsets = []; |
||
188 | foreach ($columns as $column) { |
||
189 | if (!in_array($column, $newFields)) { |
||
190 | $unsets[$column] = ""; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | if (count($unsets) > 0) { |
||
195 | $update['$unset'] = $unsets; |
||
196 | $collection->updateMany(array(), $update, array('upsert' => true)); |
||
197 | } |
||
198 | |||
199 | return true; |
||
200 | } |
||
271 |