| Conditions | 9 |
| Paths | 16 |
| Total Lines | 59 |
| Code Lines | 33 |
| 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 |
||
| 87 | public function run() |
||
| 88 | { |
||
| 89 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 90 | |||
| 91 | $class = $this->class_name; |
||
| 92 | |||
| 93 | if (null === $current_selected_id = Yii::$app->request->get($this->query_selected_attribute)) { |
||
| 94 | $current_selected_id = Yii::$app->request->get($this->query_parent_attribute); |
||
| 95 | } |
||
| 96 | |||
| 97 | $cacheKey = "AdjacencyFullTreeData:{$this->cacheKey}:{$class}:{$this->query_sort_order}"; |
||
| 98 | |||
| 99 | if (false === $result = Yii::$app->cache->get($cacheKey)) { |
||
| 100 | $query = $class::find() |
||
| 101 | ->orderBy([$this->query_sort_order => SORT_ASC]); |
||
| 102 | |||
| 103 | if (count($this->whereCondition) > 0) { |
||
| 104 | $query = $query->where($this->whereCondition); |
||
| 105 | } |
||
| 106 | |||
| 107 | if (null === $rows = $query->asArray()->all()) { |
||
| 108 | return []; |
||
| 109 | } |
||
| 110 | |||
| 111 | $result = []; |
||
| 112 | |||
| 113 | foreach ($rows as $row) { |
||
| 114 | $item = [ |
||
| 115 | 'id' => $row[$this->model_id_attribute], |
||
| 116 | 'parent' => ($row[$this->model_parent_attribute] > 0) ? $row[$this->model_parent_attribute] : '#', |
||
| 117 | 'text' => $row[$this->model_label_attribute], |
||
| 118 | 'a_attr' => ['data-id'=>$row[$this->model_id_attribute], 'data-parent_id'=>$row[$this->model_parent_attribute]], |
||
|
|
|||
| 119 | ]; |
||
| 120 | |||
| 121 | if (null !== $this->vary_by_type_attribute) { |
||
| 122 | $item['type'] = $row[$this->vary_by_type_attribute]; |
||
| 123 | } |
||
| 124 | |||
| 125 | $result[$row[$this->model_id_attribute]] = $item; |
||
| 126 | } |
||
| 127 | |||
| 128 | Yii::$app->cache->set( |
||
| 129 | $cacheKey, |
||
| 130 | $result, |
||
| 131 | 86400, |
||
| 132 | new TagDependency([ |
||
| 133 | 'tags' => [ |
||
| 134 | ActiveRecordHelper::getCommonTag($class), |
||
| 135 | ], |
||
| 136 | ]) |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | |||
| 140 | if (array_key_exists($current_selected_id, $result)) { |
||
| 141 | $result[$current_selected_id] = array_merge($result[$current_selected_id], ['state' => ['opened' => true, 'selected' => true]]); |
||
| 142 | } |
||
| 143 | |||
| 144 | return array_values($result); |
||
| 145 | } |
||
| 146 | } |
||
| 147 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.