| Conditions | 27 |
| Paths | 84 |
| Total Lines | 117 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 33 |
| CRAP Score | 27 |
| Changes | 23 | ||
| Bugs | 6 | Features | 3 |
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 |
||
| 344 | 8 | public function parseFieldData($handle, $data) |
|
| 345 | { |
||
| 346 | |||
| 347 | // Do we have any data at all |
||
| 348 | if (!is_null($data)) { |
||
| 349 | |||
| 350 | // Get field info |
||
| 351 | $field = craft()->fields->getFieldByHandle($handle); |
||
| 352 | |||
| 353 | // If it's a field ofcourse |
||
| 354 | if (!is_null($field)) { |
||
| 355 | |||
| 356 | // For some fieldtypes the're special rules |
||
| 357 | 8 | switch ($field->type) { |
|
| 358 | |||
| 359 | 8 | case ExportModel::FieldTypeEntries: |
|
| 360 | 7 | case ExportModel::FieldTypeCategories: |
|
| 361 | 7 | case ExportModel::FieldTypeAssets: |
|
| 362 | 7 | case ExportModel::FieldTypeUsers: |
|
| 363 | |||
| 364 | // Show names |
||
| 365 | 1 | $data = $data instanceof ElementCriteriaModel ? implode(', ', $data->find()) : $data; |
|
|
1 ignored issue
–
show
|
|||
| 366 | |||
| 367 | 1 | break; |
|
| 368 | |||
| 369 | 7 | case ExportModel::FieldTypeLightswitch: |
|
| 370 | |||
| 371 | // Make data human readable |
||
| 372 | switch ($data) { |
||
| 373 | |||
| 374 | 2 | case '0': |
|
| 375 | $data = Craft::t('No'); |
||
| 376 | 1 | break; |
|
| 377 | |||
| 378 | 1 | case '1': |
|
| 379 | $data = Craft::t('Yes'); |
||
| 380 | 1 | break; |
|
| 381 | |||
| 382 | } |
||
| 383 | |||
| 384 | 2 | break; |
|
| 385 | |||
| 386 | 5 | case ExportModel::FieldTypeTable: |
|
| 387 | |||
| 388 | // Parse table checkboxes |
||
| 389 | 1 | $table = array(); |
|
| 390 | foreach ($data as $row) { |
||
| 391 | |||
| 392 | // Keep track of column # |
||
| 393 | 1 | $i = 1; |
|
| 394 | |||
| 395 | // Loop through columns |
||
| 396 | foreach ($row as $column => $value) { |
||
| 397 | |||
| 398 | // Get column |
||
| 399 | $column = isset($field->settings['columns'][$column]) ? $field->settings['columns'][$column] : (isset($field->settings['columns']['col'.$i]) ? $field->settings['columns']['col'.$i] : array('type' => 'dummy')); |
||
| 400 | |||
| 401 | // Keep track of column # |
||
| 402 | $i++; |
||
| 403 | |||
| 404 | // Parse |
||
| 405 | $table[] = $column['type'] == 'checkbox' ? ($value == 1 ? Craft::t('Yes') : Craft::t('No')) : $value; |
||
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | // Return parsed data as array |
||
| 410 | 1 | $data = $table; |
|
| 411 | |||
| 412 | 1 | break; |
|
| 413 | |||
| 414 | 4 | case ExportModel::FieldTypeRichText: |
|
| 415 | 3 | case ExportModel::FieldTypeDate: |
|
| 416 | 3 | case ExportModel::FieldTypeRadioButtons: |
|
| 417 | 3 | case ExportModel::FieldTypeDropdown: |
|
| 418 | |||
| 419 | // Resolve to string |
||
| 420 | 1 | $data = (string) $data; |
|
| 421 | |||
| 422 | 1 | break; |
|
| 423 | |||
| 424 | 3 | case ExportModel::FieldTypeCheckboxes: |
|
| 425 | 3 | case ExportModel::FieldTypeMultiSelect: |
|
| 426 | |||
| 427 | // Parse multi select values |
||
| 428 | 1 | $multi = array(); |
|
| 429 | foreach ($data as $row) { |
||
| 430 | $multi[] = $row->value; |
||
| 431 | } |
||
| 432 | |||
| 433 | // Return parsed data as array |
||
| 434 | 1 | $data = $multi; |
|
| 435 | |||
| 436 | 1 | break; |
|
| 437 | |||
| 438 | } |
||
| 439 | } |
||
| 440 | |||
| 441 | // Get other operations |
||
| 442 | craft()->plugins->call('registerExportOperation', array(&$data, $handle)); |
||
| 443 | } else { |
||
| 444 | |||
| 445 | // Don't return null, return empty |
||
| 446 | 1 | $data = ''; |
|
| 447 | 8 | } |
|
| 448 | |||
| 449 | // If it's an object or an array, make it a string |
||
| 450 | if (is_array($data)) { |
||
| 451 | $data = StringHelper::arrayToString(ArrayHelper::filterEmptyStringsFromArray(ArrayHelper::flattenArray($data)), ', '); |
||
| 452 | } |
||
| 453 | |||
| 454 | // If it's an object, make it a string |
||
| 455 | if (is_object($data)) { |
||
| 456 | $data = StringHelper::arrayToString(ArrayHelper::filterEmptyStringsFromArray(ArrayHelper::flattenArray(get_object_vars($data))), ', '); |
||
| 457 | } |
||
| 458 | |||
| 459 | 8 | return $data; |
|
| 460 | } |
||
| 461 | } |
||
| 462 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.