| Conditions | 23 |
| Paths | 147 |
| Total Lines | 83 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 100 | private function setModel($data) |
||
| 101 | { |
||
| 102 | $model = null; |
||
| 103 | $keyCol = null; |
||
| 104 | $uniques = []; |
||
| 105 | foreach ($this->object->params as $param) { |
||
| 106 | $options = $param->options ? json_decode($param->options, true) : []; |
||
| 107 | if ($param->type == 'item_key') { |
||
| 108 | $keyCol = $param->code; |
||
| 109 | break; |
||
| 110 | } elseif (!empty($options['unique'])) { |
||
| 111 | $uniques[$param->code] = $param; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | if ($keyCol && isset($data[$keyCol])) { |
||
| 115 | $objectId = \Migrations\Id::get([['parse_id', (string) $data[$keyCol]], ['type', $this->object->model]]); |
||
| 116 | if ($objectId) { |
||
| 117 | $modelName = $this->object->model; |
||
| 118 | $model = $modelName::get($objectId->object_id); |
||
| 119 | } else { |
||
| 120 | $model = new $this->object->model; |
||
| 121 | $model->save(['empty' => true]); |
||
| 122 | $objectId = new \Migrations\Id(); |
||
| 123 | $objectId->object_id = $model->id; |
||
| 124 | $objectId->parse_id = (string) $data[$keyCol]; |
||
| 125 | $objectId->type = $this->object->model; |
||
| 126 | $objectId->save(); |
||
| 127 | } |
||
| 128 | } elseif ($uniques) { |
||
| 129 | $where = []; |
||
| 130 | foreach ($uniques as $code => $param) { |
||
| 131 | if (!isset($data[$code])) { |
||
| 132 | return; |
||
| 133 | } |
||
| 134 | switch ($param->type) { |
||
| 135 | case 'objectLink': |
||
| 136 | $object = \Migrations\Migration\Object::get($param->value); |
||
| 137 | $objectId = \Migrations\Id::get([['parse_id', (string) $data[$code]], ['type', $object->model]]); |
||
| 138 | if (!$objectId) { |
||
| 139 | return; |
||
| 140 | } |
||
| 141 | $modelName = $object->model; |
||
| 142 | $model = $modelName::get($objectId->object_id); |
||
| 143 | $where[] = [$model->index(), $model->pk()]; |
||
| 144 | break; |
||
| 145 | case 'relation': |
||
| 146 | $modelName = $this->object->model; |
||
| 147 | $relation = $modelName::getRelation($param->value); |
||
| 148 | $objectId = \Migrations\Id::get([['parse_id', (string) $data[$code]], ['type', $relation['model']]]); |
||
| 149 | if (!$objectId) { |
||
| 150 | return; |
||
| 151 | } |
||
| 152 | $modelName = $relation['model']; |
||
| 153 | $model = $modelName::get($objectId->object_id); |
||
| 154 | $where[] = [$relation['col'], $model->pk()]; |
||
| 155 | break; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | if ($where) { |
||
| 159 | if ($this->parentParam) { |
||
| 160 | $modelName = $this->parentObject->object->model; |
||
| 161 | $relation = $modelName::getRelation($this->parentParam->param->value); |
||
| 162 | if (!empty($relation['type']) && $relation['type'] == 'many') { |
||
| 163 | $where[] = [$relation['col'], $this->parentModel->pk()]; |
||
| 164 | } |
||
| 165 | } elseif ($this->parentObject) { |
||
| 166 | $modelName = $this->parentObject->object->model; |
||
| 167 | $where[] = [$modelName::index(), $this->parentModel->pk()]; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | if ($where) { |
||
| 171 | $modelName = $this->object->model; |
||
| 172 | $model = $modelName::get($where); |
||
| 173 | if (!$model) { |
||
| 174 | $model = new $this->object->model; |
||
| 175 | foreach ($where as $item) { |
||
| 176 | $model->{$item[0]} = $item[1]; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | return $model; |
||
| 182 | } |
||
| 183 | |||
| 185 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: