| Conditions | 24 |
| Paths | 320 |
| Total Lines | 83 |
| Code Lines | 50 |
| 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 |
||
| 131 | public function toArray() |
||
| 132 | { |
||
| 133 | $item = []; |
||
| 134 | $hasVisible = false; |
||
| 135 | |||
| 136 | foreach ($this->visible as $key => $val) { |
||
| 137 | if (is_string($val)) { |
||
| 138 | if (strpos($val, '.')) { |
||
| 139 | list($relation, $name) = explode('.', $val); |
||
| 140 | $this->visible[$relation][] = $name; |
||
| 141 | } else { |
||
| 142 | $this->visible[$val] = true; |
||
| 143 | $hasVisible = true; |
||
| 144 | } |
||
| 145 | unset($this->visible[$key]); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | foreach ($this->hidden as $key => $val) { |
||
| 150 | if (is_string($val)) { |
||
| 151 | if (strpos($val, '.')) { |
||
| 152 | list($relation, $name) = explode('.', $val); |
||
| 153 | $this->hidden[$relation][] = $name; |
||
| 154 | } else { |
||
| 155 | $this->hidden[$val] = true; |
||
| 156 | } |
||
| 157 | unset($this->hidden[$key]); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | // 合并关联数据 |
||
| 162 | $data = array_merge($this->data, $this->relation); |
||
| 163 | |||
| 164 | foreach ($data as $key => $val) { |
||
| 165 | if ($val instanceof Model || $val instanceof ModelCollection) { |
||
| 166 | // 关联模型对象 |
||
| 167 | if (isset($this->visible[$key])) { |
||
| 168 | $val->visible($this->visible[$key]); |
||
| 169 | } elseif (isset($this->hidden[$key]) && is_array($this->hidden[$key])) { |
||
| 170 | $val->hidden($this->hidden[$key]); |
||
| 171 | } |
||
| 172 | // 关联模型对象 |
||
| 173 | if (!isset($this->hidden[$key]) || true !== $this->hidden[$key]) { |
||
| 174 | $item[$key] = $val->toArray(); |
||
| 175 | } |
||
| 176 | } elseif (isset($this->visible[$key])) { |
||
| 177 | $item[$key] = $this->getAttr($key); |
||
| 178 | } elseif (!isset($this->hidden[$key]) && !$hasVisible) { |
||
| 179 | $item[$key] = $this->getAttr($key); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | // 追加属性(必须定义获取器) |
||
| 184 | if (!empty($this->append)) { |
||
| 185 | foreach ($this->append as $key => $name) { |
||
| 186 | if (is_array($name)) { |
||
| 187 | // 追加关联对象属性 |
||
| 188 | $relation = $this->getRelation($key); |
||
| 189 | |||
| 190 | if (!$relation) { |
||
| 191 | $relation = $this->getAttr($key); |
||
| 192 | $relation->visible($name); |
||
| 193 | } |
||
| 194 | |||
| 195 | $item[$key] = $relation->append($name)->toArray(); |
||
| 196 | } elseif (strpos($name, '.')) { |
||
| 197 | list($key, $attr) = explode('.', $name); |
||
| 198 | // 追加关联对象属性 |
||
| 199 | $relation = $this->getRelation($key); |
||
| 200 | |||
| 201 | if (!$relation) { |
||
| 202 | $relation = $this->getAttr($key); |
||
| 203 | $relation->visible([$attr]); |
||
| 204 | } |
||
| 205 | |||
| 206 | $item[$key] = $relation->append([$attr])->toArray(); |
||
| 207 | } else { |
||
| 208 | $item[$name] = $this->getAttr($name, $item); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | return $item; |
||
| 214 | } |
||
| 270 |