| Conditions | 23 |
| Paths | 1152 |
| Total Lines | 78 |
| Code Lines | 61 |
| 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 |
||
| 16 | public function parse() { |
||
| 17 | $options = $this->param->options ? json_decode($this->param->options, true) : []; |
||
| 18 | $modelName = get_class($this->model); |
||
| 19 | $cols = $modelName::$cols; |
||
|
|
|||
| 20 | $value = $this->data; |
||
| 21 | if (is_array($value)) { |
||
| 22 | $value = ''; |
||
| 23 | } |
||
| 24 | if (!empty($cols[$this->param->value])) { |
||
| 25 | $col = $cols[$this->param->value]; |
||
| 26 | if ($col['type'] == 'dynamicType') { |
||
| 27 | switch ($col['typeSource']) { |
||
| 28 | case 'selfMethod': |
||
| 29 | $type = $this->model->{$col['selfMethod']}(); |
||
| 30 | if (is_array($type)) { |
||
| 31 | if (strpos($type['relation'], ':')) { |
||
| 32 | $relationPath = explode(':', $type['relation']); |
||
| 33 | $relationName = array_pop($relationPath); |
||
| 34 | $item = $this->model; |
||
| 35 | foreach ($relationPath as $path) { |
||
| 36 | $item = $item->$path; |
||
| 37 | } |
||
| 38 | $itemModel = get_class($item); |
||
| 39 | $relation = $itemModel::getRelation($relationName); |
||
| 40 | $sourceModel = $relation['model']; |
||
| 41 | } else { |
||
| 42 | $relation = $modelName::getRelation($type['relation']); |
||
| 43 | $sourceModel = $relation['model']; |
||
| 44 | } |
||
| 45 | $objectId = \App::$cur->migrations->findObject((string) $value, $sourceModel); |
||
| 46 | if ($objectId) { |
||
| 47 | $value = $objectId->object_id; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | break; |
||
| 51 | } |
||
| 52 | } else { |
||
| 53 | $type = $col['type']; |
||
| 54 | } |
||
| 55 | } else { |
||
| 56 | $type = 'text'; |
||
| 57 | } |
||
| 58 | |||
| 59 | if (!empty($options['valueReplace'])) { |
||
| 60 | $values = $this->param->values(['key' => 'original']); |
||
| 61 | if (!isset($values[$value])) { |
||
| 62 | $valueObject = new \Migrations\Migration\Object\Param\Value(); |
||
| 63 | $valueObject->param_id = $this->param->id; |
||
| 64 | $valueObject->original = $value; |
||
| 65 | $valueObject->save(); |
||
| 66 | } else { |
||
| 67 | $valueObject = $values[$value]; |
||
| 68 | } |
||
| 69 | $value = $valueObject->replace; |
||
| 70 | } |
||
| 71 | switch ($type) { |
||
| 72 | case 'image': |
||
| 73 | $notEq = true; |
||
| 74 | $dir = pathinfo($this->object->walker->migtarionLog->source, PATHINFO_DIRNAME); |
||
| 75 | if ($this->model->{$this->param->value}) { |
||
| 76 | $file = \Files\File::get($this->model->{$this->param->value}); |
||
| 77 | if ($file && $value && file_exists($dir . '/' . $value) && file_exists(\App::$primary->path . $file->path) && md5_file($dir . '/' . $value) == md5_file(\App::$primary->path . $file->path)) { |
||
| 78 | $notEq = false; |
||
| 79 | } |
||
| 80 | if ($file && $notEq) { |
||
| 81 | $file->delete(); |
||
| 82 | $this->model->{$this->param->value} = 0; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | if ($notEq) { |
||
| 86 | $this->model->{$this->param->value} = \App::$primary->files->uploadFromUrl($dir . '/' . $value, ['accept_group' => 'image', 'upload_code' => 'MigrationUpload']); |
||
| 87 | } |
||
| 88 | break; |
||
| 89 | default: |
||
| 90 | if (is_array($value)) { |
||
| 91 | $value = implode(' ', $value); |
||
| 92 | } |
||
| 93 | $this->model->{$this->param->value} = $value; |
||
| 94 | } |
||
| 98 |