| Conditions | 10 |
| Paths | 144 |
| Total Lines | 43 |
| Code Lines | 33 |
| 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 |
||
| 54 | public function import(): void |
||
| 55 | { |
||
| 56 | if ($this->config->get('log_all')) { |
||
| 57 | $this->controller->log('Start import ' . $this->name, []); |
||
| 58 | } |
||
| 59 | $isRoleBased = $this->fieldModel->isRoleBased(); |
||
| 60 | $fieldName = $this->fieldModel->getName(); |
||
| 61 | $picklistValues = \App\Fields\Picklist::getValues($fieldName); |
||
| 62 | $values = []; |
||
| 63 | foreach ($picklistValues as $value) { |
||
| 64 | $values[trim(mb_strtolower($value['picklistValue']), '.')] = $value['picklistValue']; |
||
| 65 | $values[trim(mb_strtolower(\App\Language::translate($value['picklistValue'], 'Products')), '.')] = $value['picklistValue']; |
||
| 66 | } |
||
| 67 | $i = 0; |
||
| 68 | foreach ($this->cache as $value) { |
||
| 69 | if (empty($value['key'])) { |
||
| 70 | continue; |
||
| 71 | } |
||
| 72 | $name = mb_strtolower(trim($value['key'], '.')); |
||
| 73 | if (empty($values[$name])) { |
||
| 74 | try { |
||
| 75 | $itemModel = $this->fieldModel->getItemModel(); |
||
| 76 | $itemModel->validateValue('name', $value['key']); |
||
| 77 | $itemModel->set('name', $value['key']); |
||
| 78 | if ($isRoleBased) { |
||
| 79 | if (empty($this->roleIdList)) { |
||
| 80 | $this->roleIdList = array_keys(\Settings_Roles_Record_Model::getAll()); |
||
| 81 | } |
||
| 82 | $itemModel->set('roles', $this->roleIdList); |
||
| 83 | } |
||
| 84 | $itemModel->save(); |
||
| 85 | $this->cacheList[$value['key']] = $value['key']; |
||
| 86 | ++$i; |
||
| 87 | } catch (\Throwable $ex) { |
||
| 88 | $this->controller->log('Import ' . $this->name, ['API' => $value], $ex); |
||
| 89 | \App\Log::error("Error during import {$this->name}: \n{$ex->__toString()}", self::LOG_CATEGORY); |
||
| 90 | } |
||
| 91 | } else { |
||
| 92 | $this->cacheList[$values[$name]] = $value['key']; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | if ($this->config->get('log_all')) { |
||
| 96 | $this->controller->log('End import ' . $this->name, ['imported' => $i]); |
||
| 97 | } |
||
| 151 |