| Conditions | 19 |
| Paths | 2 |
| Total Lines | 66 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 2 | 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 |
||
| 12 | public function show($view, Request $request) |
||
| 13 | { |
||
| 14 | if (!PreviewServiceProvider::isEnabled()) { |
||
| 15 | abort(404); |
||
| 16 | } |
||
| 17 | |||
| 18 | $data = $request->all(); |
||
| 19 | |||
| 20 | array_walk($data, function (&$value) { |
||
| 21 | switch ($value) { |
||
| 22 | case is_object(json_decode($value)): |
||
| 23 | $value = json_decode($value); |
||
| 24 | break; |
||
| 25 | case str_contains($value, '::'): |
||
| 26 | $parts = explode('::', $value); |
||
| 27 | |||
| 28 | switch (count($parts)) { |
||
| 29 | case 0: |
||
| 30 | break; |
||
| 31 | case 1: |
||
| 32 | break; |
||
| 33 | default: |
||
| 34 | if (2 == count($parts) && is_numeric($parts[1])) { |
||
| 35 | list($class, $id) = $parts; |
||
| 36 | if (class_exists($class)) { |
||
| 37 | try { |
||
| 38 | $object = new $class(); |
||
| 39 | if ($object instanceof Model && is_numeric($id)) { |
||
| 40 | $value = call_user_func_array([$class, 'findOrFail'], [$id]); |
||
| 41 | } |
||
| 42 | } catch (Exception $e) { /* Ignore errors */ |
||
| 43 | } |
||
| 44 | } |
||
| 45 | } else { |
||
| 46 | list($class, $method) = array_slice($parts, 0, 2); |
||
| 47 | $params = array_slice($parts, 2); |
||
| 48 | if (class_exists($class)) { |
||
| 49 | $updated = false; |
||
| 50 | try { |
||
| 51 | if (is_callable([$class, $method])) { |
||
| 52 | $value = call_user_func_array([$class, $method], $params); |
||
| 53 | $updated = true; |
||
| 54 | } |
||
| 55 | } catch (Exception $e) { /* Ignore errors */ |
||
| 56 | } |
||
| 57 | if (!$updated) { |
||
| 58 | try { |
||
| 59 | if (is_callable([$class, $method])) { |
||
| 60 | $object = new $class(); |
||
| 61 | if (is_callable([$object, $method])) { |
||
| 62 | $value = call_user_func_array([$object, $method], $params); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } catch (Exception $e) { /* Ignore errors */ |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | break; |
||
| 71 | } |
||
| 72 | break; |
||
| 73 | } |
||
| 74 | }); |
||
| 75 | |||
| 76 | return view($view, $data); |
||
| 77 | } |
||
| 78 | } |
||
| 79 |