Conditions | 23 |
Paths | 80 |
Total Lines | 99 |
Code Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 1 | 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 |
||
16 | public function parse() |
||
17 | { |
||
18 | |||
19 | $options = $this->param->options ? json_decode($this->param->options, true) : []; |
||
20 | $modelName = $this->object->object->model; |
||
21 | $relation = $modelName::getRelation($this->param->value); |
||
22 | $object = \Migrations\Migration\Object::get([ |
||
23 | ['model', $relation['model']], |
||
24 | ['migration_id', $this->object->object->migration_id] |
||
25 | ]); |
||
26 | if (!$object) { |
||
27 | $object = new \Migrations\Migration\Object([ |
||
28 | 'model' => $relation['model'], |
||
29 | 'code' => $this->param->code, |
||
30 | 'name' => $this->param->code, |
||
31 | 'migration_id' => $this->object->object->migration_id |
||
32 | ]); |
||
33 | $object->save(); |
||
34 | } |
||
35 | if (!empty($relation['type']) && $relation['type'] == 'many') { |
||
36 | $ids = []; |
||
37 | |||
38 | if ($this->data) { |
||
39 | foreach ($this->data as $code => &$item) { |
||
40 | if (!\Tools::isAssoc($this->data)) { |
||
41 | foreach ($this->data as &$item) { |
||
42 | $objectParser = new \Migrations\Parser\Object(); |
||
43 | $objectParser->object = $object; |
||
44 | $objectParser->parentObject = $this->object; |
||
45 | $objectParser->parentModel = $this->model; |
||
46 | $objectParser->walker = $this->object->walker; |
||
47 | $objectParser->parentParam = $this; |
||
48 | $objectParser->data = &$item; |
||
49 | |||
50 | |||
51 | if (!$this->model->pk()) { |
||
52 | $this->model->save(); |
||
53 | } |
||
54 | $ids = array_merge($ids, $objectParser->parse([$relation['col'] => $this->model->pk()])); |
||
55 | } |
||
56 | } else { |
||
57 | $objectParser = new \Migrations\Parser\Object(); |
||
58 | $objectParser->object = $object; |
||
59 | $objectParser->parentObject = $this->object; |
||
60 | $objectParser->parentModel = $this->model; |
||
61 | $objectParser->walker = $this->object->walker; |
||
62 | $objectParser->parentParam = $this; |
||
63 | $objectParser->data = &$item; |
||
64 | if (!$this->model->pk()) { |
||
65 | $this->model->save(); |
||
66 | } |
||
67 | $ids = array_merge($ids, $objectParser->parse([$relation['col'] => $this->model->pk()])); |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 | if (!empty($options['clearMissing']) && $this->model->pk()) { |
||
72 | $where = []; |
||
73 | $where[] = [$relation['col'], $this->model->pk()]; |
||
74 | if ($ids) { |
||
75 | $where[] = ['id', implode(',', $ids), 'NOT IN']; |
||
76 | } |
||
77 | $modelName = $relation['model']; |
||
78 | $objects = $modelName::getList(['where' => $where]); |
||
79 | foreach ($objects as $delObject) { |
||
80 | $objectId = \App::$cur->migrations->findParse($delObject->id, get_class($delObject)); |
||
81 | if ($objectId) { |
||
82 | unset(\App::$cur->migrations->ids['objectIds'][get_class($delObject)][$delObject->id]); |
||
83 | unset(\App::$cur->migrations->ids['parseIds'][get_class($delObject)][$objectId->parse_id]); |
||
84 | $objectId->delete(); |
||
85 | } |
||
86 | $delObject->delete(); |
||
87 | } |
||
88 | } |
||
89 | } else { |
||
90 | $objectParser = new \Migrations\Parser\Object(); |
||
91 | $objectParser->object = $object; |
||
92 | $objectParser->parentObject = $this->object; |
||
93 | $objectParser->parentModel = $this->model; |
||
94 | $objectParser->parentParam = $this; |
||
95 | $objectParser->data = &$this->data; |
||
96 | $ids = []; |
||
97 | if (!\Tools::isAssoc($this->data)) { |
||
98 | foreach ($this->data as &$data) { |
||
99 | $model = $objectParser->setModel($this->data); |
||
100 | if ($model && $model->id) { |
||
101 | $ids[] = $model->id; |
||
102 | } |
||
103 | } |
||
104 | } else { |
||
105 | $model = $objectParser->setModel($this->data); |
||
106 | if ($model && $model->id) { |
||
107 | $ids[] = $model->id; |
||
108 | } |
||
109 | } |
||
110 | if ($ids) { |
||
111 | $this->model->{$relation['col']} = $ids[0]; |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | |||
117 |