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 |
||
125 | protected function getValueFromObject($instance, $name) |
||
126 | { |
||
127 | if ($name instanceof Closure) { |
||
128 | return $name($instance); |
||
129 | } |
||
130 | |||
131 | /* |
||
132 | * Implement json parsing |
||
133 | */ |
||
134 | if (strpos($name, '.') === false && strpos($name, '->') !== false) { |
||
135 | $casts = collect($instance->getCasts()); |
||
136 | $jsonParts = collect(explode('->', $name)); |
||
137 | |||
138 | $jsonAttr = $instance->{$jsonParts->first()}; |
||
139 | |||
140 | $cast = $casts->get($jsonParts->first(), false); |
||
141 | |||
142 | if ($cast == 'object') { |
||
143 | $jsonAttr = json_decode(json_encode($jsonAttr), true); |
||
144 | } elseif ($cast != 'array') { |
||
145 | $jsonAttr = json_decode($jsonAttr); |
||
146 | } |
||
147 | |||
148 | return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.')); |
||
149 | } |
||
150 | |||
151 | $parts = explode('.', $name); |
||
152 | $part = array_shift($parts); |
||
153 | |||
154 | if ($instance instanceof Collection) { |
||
155 | $instance = $instance->pluck($part); |
||
156 | } elseif ($instance instanceof SuportCollection) { |
||
157 | $instance = $instance->first(); |
||
158 | if ($instance instanceof Collection) { |
||
159 | $instance = $instance->pluck($part); |
||
160 | } |
||
161 | |||
162 | if ($instance === null) { |
||
163 | $instance = collect(); |
||
164 | } |
||
165 | } elseif (! is_null($instance)) { |
||
166 | $instance = $instance->getAttribute($part); |
||
167 | } |
||
168 | |||
169 | if (! empty($parts) && ! is_null($instance)) { |
||
170 | return $this->getValueFromObject($instance, implode('.', $parts)); |
||
171 | } |
||
172 | |||
173 | return $instance; |
||
174 | } |
||
176 |