| Conditions | 20 |
| Paths | 128 |
| Total Lines | 83 |
| Code Lines | 55 |
| 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 |
||
| 83 | private function parseOptions(array $options): array |
||
| 84 | { |
||
| 85 | $_options = []; |
||
| 86 | |||
| 87 | $options = array_change_key_case(array_filter($options, function ($var) { |
||
| 88 | return !is_null($var); |
||
| 89 | })); |
||
| 90 | |||
| 91 | if (!empty($options['attr']['type'])) { |
||
| 92 | $options['type'] = $options['attr']['type']; |
||
| 93 | unset($options['attr']['type']); |
||
| 94 | } |
||
| 95 | |||
| 96 | if (!empty($options['constraints']['type'])) { |
||
| 97 | $options['type'] = $options['constraints']['type']; |
||
| 98 | unset($options['constraints']['type']); |
||
| 99 | } |
||
| 100 | |||
| 101 | foreach ($options as $option => $value) { |
||
| 102 | switch ($option) { |
||
| 103 | case 'default': |
||
| 104 | $_options['data'] = $value; |
||
| 105 | break; |
||
| 106 | case 'constraints': |
||
| 107 | $attributes = \json_decode($value, true); |
||
| 108 | |||
| 109 | if ((\json_last_error() !== JSON_ERROR_NONE)) { |
||
| 110 | $attributes = [$value]; |
||
| 111 | } |
||
| 112 | |||
| 113 | foreach ($attributes as $attribute => $_value) { |
||
| 114 | $attribute = \strtolower($attribute); |
||
| 115 | |||
| 116 | if ($attribute == 'required' || $_value == 'required') { |
||
| 117 | $_options['required'] = true; |
||
| 118 | } elseif (in_array($attribute, ['length', 'mincheck', 'maxcheck', 'check', 'equalto'])) { |
||
| 119 | $_options['attr']['data-parsley-' . $attribute] = $_value; |
||
| 120 | } elseif ($attribute == 'range') { |
||
| 121 | $_options['type'] = $attribute; |
||
| 122 | list($min, $max) = explode(',', $_value); |
||
| 123 | $_options['attr'] = compact('min', 'max'); |
||
| 124 | } elseif ($attribute == 'type') { |
||
| 125 | $_options['type'] = $_value; |
||
| 126 | } else { |
||
| 127 | $_options['attr'][$attribute] = $_value; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | break; |
||
| 131 | case 'empty_data': |
||
| 132 | $_options[$option] = $value; |
||
| 133 | |||
| 134 | if (empty($_options['attr'])) { |
||
| 135 | $_options['attr'] = []; |
||
| 136 | } |
||
| 137 | |||
| 138 | $_options['attr']['placeholder'] = $value; |
||
| 139 | |||
| 140 | break; |
||
| 141 | default: |
||
| 142 | if (is_array($value) && !empty($_options[$option])) { |
||
| 143 | $_options[$option] = array_merge_recursive($_options[$option], $value); |
||
| 144 | } else { |
||
| 145 | $_options[$option] = $value; |
||
| 146 | } |
||
| 147 | break; |
||
| 148 | } |
||
| 149 | |||
| 150 | unset($options[$option]); |
||
| 151 | } |
||
| 152 | |||
| 153 | if (!empty($_options['type'])) { |
||
| 154 | $_type = strtolower($_options['type']); |
||
| 155 | |||
| 156 | switch ($_type) { |
||
| 157 | case 'digits': |
||
| 158 | case 'alphanum': |
||
| 159 | $_options['attr']['data-parsley-type'] = $_type; |
||
| 160 | $_options['type'] = 'text'; |
||
| 161 | break; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | return $_options; |
||
| 166 | } |
||
| 193 |