| Conditions | 13 |
| Paths | 18 |
| Total Lines | 49 |
| Code Lines | 27 |
| 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 |
||
| 177 | protected function getValueFromObject($instance, $name) |
||
| 178 | { |
||
| 179 | if ($name instanceof Closure) { |
||
| 180 | return $name($instance); |
||
| 181 | } |
||
| 182 | |||
| 183 | /* |
||
| 184 | * Implement json parsing |
||
| 185 | */ |
||
| 186 | if (strpos($name, '.') === false && strpos($name, '->') !== false) { |
||
| 187 | $casts = collect($instance->getCasts()); |
||
| 188 | $jsonParts = collect(explode('->', $name)); |
||
| 189 | |||
| 190 | $jsonAttr = $instance->{$jsonParts->first()}; |
||
| 191 | |||
| 192 | $cast = $casts->get($jsonParts->first(), false); |
||
| 193 | |||
| 194 | if ($cast == 'object') { |
||
| 195 | $jsonAttr = json_decode(json_encode($jsonAttr), true); |
||
| 196 | } elseif ($cast != 'array') { |
||
| 197 | $jsonAttr = json_decode($jsonAttr); |
||
| 198 | } |
||
| 199 | |||
| 200 | return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.')); |
||
| 201 | } |
||
| 202 | |||
| 203 | $parts = explode('.', $name); |
||
| 204 | $part = array_shift($parts); |
||
| 205 | |||
| 206 | if ($instance instanceof Collection) { |
||
| 207 | $instance = $instance->pluck($part); |
||
| 208 | } elseif ($instance instanceof SuportCollection) { |
||
| 209 | $instance = $instance->first(); |
||
| 210 | if ($instance instanceof Collection) { |
||
| 211 | $instance = $instance->pluck($part); |
||
| 212 | } |
||
| 213 | |||
| 214 | if ($instance === null) { |
||
| 215 | $instance = collect(); |
||
| 216 | } |
||
| 217 | } elseif (! is_null($instance)) { |
||
| 218 | $instance = $instance->getAttribute($part); |
||
| 219 | } |
||
| 220 | |||
| 221 | if (! empty($parts) && ! is_null($instance)) { |
||
| 222 | return $this->getValueFromObject($instance, implode('.', $parts)); |
||
| 223 | } |
||
| 224 | |||
| 225 | return $instance; |
||
| 226 | } |
||
| 228 |