| Conditions | 22 |
| Paths | 68 |
| Total Lines | 52 |
| Code Lines | 43 |
| 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 |
||
| 22 | public function forView($options = []) { |
||
| 23 | $modelName = get_class($this->model); |
||
| 24 | $colInfo = $modelName::getColInfo($this->valueKey); |
||
| 25 | $type = !empty($colInfo['colParams']['type']) ? $colInfo['colParams']['type'] : 'string'; |
||
| 26 | switch ($type) { |
||
| 27 | case 'dateTime': |
||
| 28 | //fall |
||
| 29 | case 'date': |
||
| 30 | $yy = (int) substr($this->model->{$this->valueKey}, 0, 4); |
||
| 31 | $mm = (int) substr($this->model->{$this->valueKey}, 5, 2); |
||
| 32 | $dd = (int) substr($this->model->{$this->valueKey}, 8, 2); |
||
| 33 | |||
| 34 | $hours = substr($this->model->{$this->valueKey}, 11, 5); |
||
| 35 | |||
| 36 | $month = array('января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря'); |
||
| 37 | $yearPosrfix = isset($options['yearPostfix']) ? $options['yearPostfix'] : " г."; |
||
| 38 | return ($dd > 0 ? $dd . " " : '') . $month[$mm - 1] . " " . $yy . $yearPosrfix . (empty($options['notime']) ? " " . $hours : ''); |
||
| 39 | case 'select': |
||
| 40 | switch ($colInfo['colParams']['source']) { |
||
| 41 | case 'model': |
||
| 42 | $sourceValue = false; |
||
| 43 | if ($this->model->{$this->valueKey}) { |
||
| 44 | $sourceValue = $colInfo['colParams']['model']::get($this->model->{$this->valueKey}); |
||
| 45 | } |
||
| 46 | return $sourceValue ? $sourceValue->name() : 'Не задано'; |
||
| 47 | case 'array': |
||
| 48 | return !empty($colInfo['colParams']['sourceArray'][$this->model->{$this->valueKey}]) ? $colInfo['colParams']['sourceArray'][$this->model->{$this->valueKey}] : 'Не задано'; |
||
| 49 | case 'method': |
||
| 50 | if (!empty($colInfo['colParams']['params'])) { |
||
| 51 | $values = call_user_func_array([App::$cur->{$colInfo['colParams']['module']}, $colInfo['colParams']['method']], $colInfo['colParams']['params']); |
||
| 52 | } else { |
||
| 53 | $values = $colInfo['colParams']['module']->{$colInfo['colParams']['method']}(); |
||
| 54 | } |
||
| 55 | return !empty($values[$this->model->{$this->valueKey}]) ? $values[$this->model->{$this->valueKey}] : 'Не задано'; |
||
| 56 | case 'relation': |
||
| 57 | $relations = $colInfo['modelName']::relations(); |
||
| 58 | $relValue = $relations[$colInfo['colParams']['relation']]['model']::get($this->model->{$this->valueKey}); |
||
| 59 | return $relValue ? $relValue->name() : 'Не задано'; |
||
| 60 | } |
||
| 61 | break; |
||
| 62 | case 'image': |
||
| 63 | $file = Files\File::get($this->model->{$this->valueKey}); |
||
| 64 | if ($file) { |
||
| 65 | return '<img src="' . $file->path . '?resize=60x120" />'; |
||
| 66 | } |
||
| 67 | return '<img src="/static/system/images/no-image.png?resize=60x120" />'; |
||
| 68 | case 'bool': |
||
| 69 | return $this->model->{$this->valueKey} ? 'Да' : 'Нет'; |
||
| 70 | default: |
||
| 71 | return $this->model->{$this->valueKey}; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |