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 |
||
140 | protected function getValueFromObject($instance, $name) |
||
141 | { |
||
142 | if ($name instanceof Closure) { |
||
143 | return $name($instance); |
||
144 | } |
||
145 | |||
146 | /* |
||
147 | * Implement json parsing |
||
148 | */ |
||
149 | if (strpos($name, '.') === false && strpos($name, '->') !== false) { |
||
150 | $casts = collect($instance->getCasts()); |
||
151 | $jsonParts = collect(explode('->', $name)); |
||
152 | |||
153 | $jsonAttr = $instance->{$jsonParts->first()}; |
||
154 | |||
155 | $cast = $casts->get($jsonParts->first(), false); |
||
156 | |||
157 | if ($cast == 'object') { |
||
158 | $jsonAttr = json_decode(json_encode($jsonAttr), true); |
||
159 | } elseif ($cast != 'array') { |
||
160 | $jsonAttr = json_decode($jsonAttr); |
||
161 | } |
||
162 | |||
163 | return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.')); |
||
164 | } |
||
165 | |||
166 | $parts = explode('.', $name); |
||
167 | $part = array_shift($parts); |
||
168 | |||
169 | if ($instance instanceof Collection) { |
||
170 | $instance = $instance->pluck($part); |
||
171 | } elseif ($instance instanceof SuportCollection) { |
||
172 | $instance = $instance->first(); |
||
173 | if ($instance instanceof Collection) { |
||
174 | $instance = $instance->pluck($part); |
||
175 | } |
||
176 | |||
177 | if ($instance === null) { |
||
178 | $instance = collect(); |
||
179 | } |
||
180 | } elseif (! is_null($instance)) { |
||
181 | $instance = $instance->getAttribute($part); |
||
182 | } |
||
183 | |||
184 | if (! empty($parts) && ! is_null($instance)) { |
||
185 | return $this->getValueFromObject($instance, implode('.', $parts)); |
||
186 | } |
||
187 | |||
188 | return $instance; |
||
189 | } |
||
191 |