| Conditions | 39 |
| Paths | 35 |
| Total Lines | 163 |
| Code Lines | 82 |
| 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 |
||
| 252 | protected function processRecord( |
||
| 253 | $record, |
||
| 254 | $columnMap, |
||
| 255 | &$results, |
||
| 256 | $preview = false, |
||
| 257 | $makeRelations = false |
||
| 258 | ) { |
||
| 259 | // find existing object, or create new one |
||
| 260 | $existingObj = $this->findExistingObject($record, $columnMap); |
||
| 261 | $alreadyExists = (bool) $existingObj; |
||
| 262 | |||
| 263 | // If we can't edit the existing object, bail early. |
||
| 264 | if ($this->getCheckPermissions() && !$preview && $alreadyExists && !$existingObj->canEdit()) { |
||
| 265 | $type = $existingObj->i18n_singular_name(); |
||
| 266 | throw new HTTPResponse_Exception( |
||
| 267 | _t(BulkLoader::class . '.CANNOT_EDIT', "Not allowed to edit '{type}' records", ["type" => $type]), |
||
| 268 | 403 |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | |||
| 272 | $class = $record['ClassName'] ?? $this->objectClass; |
||
| 273 | $obj = $existingObj ? $existingObj : new $class(); |
||
| 274 | |||
| 275 | // If we can't create a new record, bail out early. |
||
| 276 | if ($this->getCheckPermissions() && !$preview && !$alreadyExists && !$obj->canCreate()) { |
||
| 277 | $type = $obj->i18n_singular_name(); |
||
| 278 | throw new HTTPResponse_Exception( |
||
| 279 | _t(BulkLoader::class . '.CANNOT_CREATE', "Not allowed to create '{type}' records", ["type" => $type]), |
||
| 280 | 403 |
||
| 281 | ); |
||
| 282 | } |
||
| 283 | |||
| 284 | // first run: find/create any relations and store them on the object |
||
| 285 | // we can't combine runs, as other columns might rely on the relation being present |
||
| 286 | if ($makeRelations) { |
||
| 287 | foreach ($record as $fieldName => $val) { |
||
| 288 | // don't bother querying of value is not set |
||
| 289 | if ($this->isNullValue($val)) { |
||
| 290 | continue; |
||
| 291 | } |
||
| 292 | |||
| 293 | // checking for existing relations |
||
| 294 | if (isset($this->relationCallbacks[$fieldName])) { |
||
| 295 | // trigger custom search method for finding a relation based on the given value |
||
| 296 | // and write it back to the relation (or create a new object) |
||
| 297 | $relationName = $this->relationCallbacks[$fieldName]['relationname']; |
||
| 298 | if ($this->hasMethod($this->relationCallbacks[$fieldName]['callback'])) { |
||
| 299 | $relationObj = $this->{$this->relationCallbacks[$fieldName]['callback']}( |
||
| 300 | $obj, |
||
| 301 | $val, |
||
| 302 | $record |
||
| 303 | ); |
||
| 304 | } elseif ($obj->hasMethod($this->relationCallbacks[$fieldName]['callback'])) { |
||
| 305 | $relationObj = $obj->{$this->relationCallbacks[$fieldName]['callback']}( |
||
| 306 | $val, |
||
| 307 | $record |
||
| 308 | ); |
||
| 309 | } |
||
| 310 | if (!$relationObj || !$relationObj->exists()) { |
||
| 311 | $relationClass = $obj->hasOneComponent($relationName); |
||
| 312 | $relationObj = new $relationClass(); |
||
| 313 | //write if we aren't previewing |
||
| 314 | if (!$preview) { |
||
| 315 | $relationObj->write(); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | $obj->{"{$relationName}ID"} = $relationObj->ID; |
||
| 319 | //write if we are not previewing |
||
| 320 | if (!$preview) { |
||
| 321 | $obj->write(); |
||
| 322 | $obj->flushCache(); // avoid relation caching confusion |
||
| 323 | } |
||
| 324 | } elseif (strpos($fieldName, '.') !== false) { |
||
| 325 | // we have a relation column with dot notation |
||
| 326 | list($relationName) = explode('.', $fieldName); |
||
| 327 | // always gives us an component (either empty or existing) |
||
| 328 | $relationObj = $obj->getComponent($relationName); |
||
| 329 | if (!$preview) { |
||
| 330 | $relationObj->write(); |
||
| 331 | } |
||
| 332 | $obj->{"{$relationName}ID"} = $relationObj->ID; |
||
| 333 | |||
| 334 | //write if we are not previewing |
||
| 335 | if (!$preview) { |
||
| 336 | $obj->write(); |
||
| 337 | $obj->flushCache(); // avoid relation caching confusion |
||
| 338 | } |
||
| 339 | } |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | |||
| 344 | // second run: save data |
||
| 345 | |||
| 346 | $db = $this->db; |
||
| 347 | |||
| 348 | foreach ($record as $fieldName => $val) { |
||
| 349 | // break out of the loop if we are previewing |
||
| 350 | if ($preview) { |
||
| 351 | break; |
||
| 352 | } |
||
| 353 | |||
| 354 | // Do not update ID if any exist |
||
| 355 | if ($fieldName == 'ID' && $obj->ID) { |
||
| 356 | continue; |
||
| 357 | } |
||
| 358 | |||
| 359 | // look up the mapping to see if this needs to map to callback |
||
| 360 | $mapping = ($columnMap && isset($columnMap[$fieldName])) ? $columnMap[$fieldName] |
||
| 361 | : null; |
||
| 362 | |||
| 363 | // Mapping that starts with -> map to a method |
||
| 364 | if ($mapping && strpos($mapping, '->') === 0) { |
||
| 365 | $funcName = substr($mapping, 2); |
||
| 366 | |||
| 367 | $this->$funcName($obj, $val, $record); |
||
| 368 | } elseif ($obj->hasMethod("import{$fieldName}")) { |
||
| 369 | // Try to call import_myFieldName |
||
| 370 | $obj->{"import{$fieldName}"}($val, $record); |
||
| 371 | } else { |
||
| 372 | // Map column to field |
||
| 373 | $usedName = $mapping ? $mapping : $fieldName; |
||
| 374 | |||
| 375 | // Basic value mapping based on datatype if needed |
||
| 376 | if (isset($db[$usedName])) { |
||
| 377 | switch ($db[$usedName]) { |
||
| 378 | case 'Boolean': |
||
| 379 | if ((string) $val == 'yes') { |
||
| 380 | $val = true; |
||
| 381 | } elseif ((string) $val == 'no') { |
||
| 382 | $val = false; |
||
| 383 | } |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | $obj->update(array($usedName => $val)); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | // write record |
||
| 392 | if (!$preview) { |
||
| 393 | $obj->write(); |
||
| 394 | } |
||
| 395 | |||
| 396 | // @todo better message support |
||
| 397 | $message = ''; |
||
| 398 | |||
| 399 | // save to results |
||
| 400 | if ($existingObj) { |
||
| 401 | $results->addUpdated($obj, $message); |
||
| 402 | } else { |
||
| 403 | $results->addCreated($obj, $message); |
||
| 404 | } |
||
| 405 | |||
| 406 | $objID = $obj->ID; |
||
| 407 | |||
| 408 | $obj->destroy(); |
||
| 409 | |||
| 410 | // memory usage |
||
| 411 | unset($existingObj); |
||
| 412 | unset($obj); |
||
| 413 | |||
| 414 | return $objID; |
||
| 415 | } |
||
| 546 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths