| Conditions | 14 |
| Paths | 5 |
| Total Lines | 58 |
| Code Lines | 43 |
| 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 |
||
| 41 | private function parseData($data, $preset) |
||
| 42 | { |
||
| 43 | $model = $this->setModel($data); |
||
| 44 | if ($model) { |
||
| 45 | foreach ($preset as $col => $value) { |
||
| 46 | $model->{$col} = $value; |
||
| 47 | } |
||
| 48 | |||
| 49 | $walked = []; |
||
| 50 | foreach ($this->object->params as $param) { |
||
| 51 | if ($model && $param->type && $param->type != 'item_key') { |
||
| 52 | if ($param->type == 'object') { |
||
| 53 | $object = \Migrations\Migration\Object::get($param->value); |
||
| 54 | $parser = new \Migrations\Parser\Object; |
||
| 55 | $parser->data = &$data[$param->code]; |
||
| 56 | $parser->object = $object; |
||
| 57 | $parser->parentObject = $this; |
||
| 58 | $parser->parentModel = $model; |
||
| 59 | $parser->walker = $this->walker; |
||
|
|
|||
| 60 | $parser->parse(); |
||
| 61 | } else { |
||
| 62 | if ($param->type == 'custom') { |
||
| 63 | $parserName = $param->value; |
||
| 64 | } else { |
||
| 65 | $parserName = '\Migrations\Parser\Object\\' . ucfirst($param->type); |
||
| 66 | } |
||
| 67 | if (!in_array($parserName, ['\Migrations\Parser\Object\ObjectLink','Exchange1c\Parser\Item\Images', '\Migrations\Parser\Object\Value', '\Migrations\Parser\Object\Relation', '\Migrations\Parser\Object\ParamsList'])) { |
||
| 68 | var_dump($parserName); |
||
| 69 | exit(); |
||
| 70 | } |
||
| 71 | $parser = new $parserName; |
||
| 72 | $parser->data = &$data[$param->code]; |
||
| 73 | $parser->param = $param; |
||
| 74 | $parser->model = $model; |
||
| 75 | $parser->object = $this; |
||
| 76 | $parser->parse(); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | $walked[$param->code] = true; |
||
| 80 | } |
||
| 81 | //check unparsed params |
||
| 82 | foreach ($data as $key => $item) { |
||
| 83 | //skip parsed and attribtes |
||
| 84 | if ($key == '@attributes' || !empty($walked[$key])) { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | $param = new \Migrations\Migration\Object\Param(); |
||
| 88 | $param->object_id = $this->object->id; |
||
| 89 | $param->code = $key; |
||
| 90 | $param->save(); |
||
| 91 | } |
||
| 92 | if ($model) { |
||
| 93 | $model->save(); |
||
| 94 | return $model->pk(); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | return 0; |
||
| 98 | } |
||
| 99 | |||
| 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: