| Conditions | 20 |
| Paths | 768 |
| Total Lines | 98 |
| Code Lines | 32 |
| Lines | 30 |
| Ratio | 30.61 % |
| 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 namespace Anomaly\Streams\Platform\Ui\Form\Component\Action; |
||
| 42 | protected function process($prefix, $slug, $action) |
||
| 43 | { |
||
| 44 | /* |
||
| 45 | * If the slug is numeric and the action is |
||
| 46 | * a string then treat the string as both the |
||
| 47 | * action and the slug. This is OK as long as |
||
| 48 | * there are not multiple instances of this |
||
| 49 | * input using the same action which is not likely. |
||
| 50 | */ |
||
| 51 | View Code Duplication | if (is_numeric($slug) && is_string($action)) { |
|
| 52 | $action = [ |
||
| 53 | 'slug' => $action, |
||
| 54 | 'action' => $action, |
||
| 55 | ]; |
||
| 56 | } |
||
| 57 | |||
| 58 | /* |
||
| 59 | * If the slug is NOT numeric and the action is a |
||
| 60 | * string then use the slug as the slug and the |
||
| 61 | * action as the action. |
||
| 62 | */ |
||
| 63 | View Code Duplication | if (!is_numeric($slug) && is_string($action)) { |
|
| 64 | $action = [ |
||
| 65 | 'slug' => $slug, |
||
| 66 | 'action' => $action, |
||
| 67 | ]; |
||
| 68 | } |
||
| 69 | |||
| 70 | /* |
||
| 71 | * If the slug is not numeric and the action is an |
||
| 72 | * array without a slug then use the slug for |
||
| 73 | * the slug for the action. |
||
| 74 | */ |
||
| 75 | View Code Duplication | if (is_array($action) && !isset($action['slug']) && !is_numeric($slug)) { |
|
| 76 | $action['slug'] = $slug; |
||
| 77 | } |
||
| 78 | |||
| 79 | /* |
||
| 80 | * If the slug is not numeric and the action is an |
||
| 81 | * array without a action then use the slug for |
||
| 82 | * the action for the action. |
||
| 83 | */ |
||
| 84 | View Code Duplication | if (is_array($action) && !isset($action['action']) && !is_numeric($slug)) { |
|
| 85 | $action['action'] = $slug; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Default to size "sm" |
||
| 90 | */ |
||
| 91 | if (!isset($action['size'])) { |
||
| 92 | $action['size'] = 'sm'; |
||
| 93 | } |
||
| 94 | |||
| 95 | /* |
||
| 96 | * Make sure the attributes array is set. |
||
| 97 | */ |
||
| 98 | $action['attributes'] = array_get($action, 'attributes', []); |
||
| 99 | |||
| 100 | /* |
||
| 101 | * Move all data-* keys |
||
| 102 | * to attributes. |
||
| 103 | */ |
||
| 104 | foreach ($action as $attribute => $value) { |
||
| 105 | if (str_is('data-*', $attribute)) { |
||
| 106 | array_set($action, 'attributes.' . $attribute, array_pull($action, $attribute)); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | /* |
||
| 111 | * If the HREF is present outside of the attributes |
||
| 112 | * then pull it and put it in the attributes array. |
||
| 113 | */ |
||
| 114 | if (isset($action['url'])) { |
||
| 115 | $action['attributes']['url'] = array_pull($action, 'url'); |
||
| 116 | } |
||
| 117 | |||
| 118 | /* |
||
| 119 | * Make sure the HREF is absolute. |
||
| 120 | */ |
||
| 121 | View Code Duplication | if ( |
|
| 122 | isset($action['redirect']) && |
||
| 123 | is_string($action['redirect']) && |
||
| 124 | !starts_with($action['redirect'], ['http', '{url.']) |
||
| 125 | ) { |
||
| 126 | $action['redirect'] = url($action['redirect']); |
||
| 127 | } |
||
| 128 | |||
| 129 | $action['attributes']['name'] = $prefix . 'action'; |
||
| 130 | $action['attributes']['value'] = $action['slug']; |
||
| 131 | |||
| 132 | View Code Duplication | if (isset($action['dropdown'])) { |
|
| 133 | foreach ($action['dropdown'] as $key => &$dropdown) { |
||
| 134 | $dropdown = $this->process($prefix, $key, $dropdown); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | return $action; |
||
| 139 | } |
||
| 140 | } |
||
| 141 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.