| Conditions | 22 |
| Paths | 400 |
| Total Lines | 98 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 219 | protected function processRecord($record, $columnMap, &$results, $preview = false) { |
||
| 220 | $class = $this->objectClass; |
||
| 221 | |||
| 222 | // find existing object, or create new one |
||
| 223 | $existingObj = $this->findExistingObject($record, $columnMap); |
||
| 224 | $obj = ($existingObj) ? $existingObj : new $class(); |
||
| 225 | |||
| 226 | // first run: find/create any relations and store them on the object |
||
| 227 | // we can't combine runs, as other columns might rely on the relation being present |
||
| 228 | $relations = array(); |
||
| 229 | foreach($record as $fieldName => $val) { |
||
| 230 | // don't bother querying of value is not set |
||
| 231 | if($this->isNullValue($val)) continue; |
||
| 232 | |||
| 233 | // checking for existing relations |
||
| 234 | if(isset($this->relationCallbacks[$fieldName])) { |
||
| 235 | // trigger custom search method for finding a relation based on the given value |
||
| 236 | // and write it back to the relation (or create a new object) |
||
| 237 | $relationName = $this->relationCallbacks[$fieldName]['relationname']; |
||
| 238 | if($this->hasMethod($this->relationCallbacks[$fieldName]['callback'])) { |
||
| 239 | $relationObj = $this->{$this->relationCallbacks[$fieldName]['callback']}($obj, $val, $record); |
||
| 240 | } elseif($obj->hasMethod($this->relationCallbacks[$fieldName]['callback'])) { |
||
| 241 | $relationObj = $obj->{$this->relationCallbacks[$fieldName]['callback']}($val, $record); |
||
| 242 | } |
||
| 243 | if(!$relationObj || !$relationObj->exists()) { |
||
| 244 | $relationClass = $obj->hasOneComponent($relationName); |
||
| 245 | $relationObj = new $relationClass(); |
||
| 246 | //write if we aren't previewing |
||
| 247 | if (!$preview) $relationObj->write(); |
||
| 248 | } |
||
| 249 | $obj->{"{$relationName}ID"} = $relationObj->ID; |
||
| 250 | //write if we are not previewing |
||
| 251 | if (!$preview) { |
||
| 252 | $obj->write(); |
||
| 253 | $obj->flushCache(); // avoid relation caching confusion |
||
| 254 | } |
||
| 255 | |||
| 256 | } elseif(strpos($fieldName, '.') !== false) { |
||
| 257 | // we have a relation column with dot notation |
||
| 258 | list($relationName, $columnName) = explode('.', $fieldName); |
||
| 259 | // always gives us an component (either empty or existing) |
||
| 260 | $relationObj = $obj->getComponent($relationName); |
||
| 261 | if (!$preview) $relationObj->write(); |
||
| 262 | $obj->{"{$relationName}ID"} = $relationObj->ID; |
||
| 263 | |||
| 264 | //write if we are not previewing |
||
| 265 | if (!$preview) { |
||
| 266 | $obj->write(); |
||
| 267 | $obj->flushCache(); // avoid relation caching confusion |
||
| 268 | } |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | // second run: save data |
||
| 273 | |||
| 274 | foreach($record as $fieldName => $val) { |
||
| 275 | // break out of the loop if we are previewing |
||
| 276 | if ($preview) { |
||
| 277 | break; |
||
| 278 | } |
||
| 279 | |||
| 280 | // look up the mapping to see if this needs to map to callback |
||
| 281 | $mapped = $this->columnMap && isset($this->columnMap[$fieldName]); |
||
| 282 | |||
| 283 | if($mapped && strpos($this->columnMap[$fieldName], '->') === 0) { |
||
| 284 | $funcName = substr($this->columnMap[$fieldName], 2); |
||
| 285 | |||
| 286 | $this->$funcName($obj, $val, $record); |
||
| 287 | } else if($obj->hasMethod("import{$fieldName}")) { |
||
| 288 | $obj->{"import{$fieldName}"}($val, $record); |
||
| 289 | } else { |
||
| 290 | $obj->update(array($fieldName => $val)); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | // write record |
||
| 295 | $id = ($preview) ? 0 : $obj->write(); |
||
| 296 | |||
| 297 | // @todo better message support |
||
| 298 | $message = ''; |
||
| 299 | |||
| 300 | // save to results |
||
| 301 | if($existingObj) { |
||
| 302 | $results->addUpdated($obj, $message); |
||
| 303 | } else { |
||
| 304 | $results->addCreated($obj, $message); |
||
| 305 | } |
||
| 306 | |||
| 307 | $objID = $obj->ID; |
||
| 308 | |||
| 309 | $obj->destroy(); |
||
| 310 | |||
| 311 | // memory usage |
||
| 312 | unset($existingObj); |
||
| 313 | unset($obj); |
||
| 314 | |||
| 315 | return $objID; |
||
| 316 | } |
||
| 317 | |||
| 374 |
If you suppress an error, we recommend checking for the error condition explicitly: