Conditions | 26 |
Paths | 79 |
Total Lines | 172 |
Lines | 93 |
Ratio | 54.07 % |
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 |
||
54 | public function getValidator(array $input) |
||
55 | { |
||
56 | if (!array_key_exists($this->column, $input)) { |
||
57 | return false; |
||
58 | } |
||
59 | |||
60 | $input = array_only($input, $this->column); |
||
61 | $rules = $attributes = $messages = $newInputs = []; |
||
62 | $rel = $this->column; |
||
63 | $availInput = $input; |
||
64 | View Code Duplication | $array_key_attach_str = function (array $a, string $b, string $c = '.') { |
|
65 | return call_user_func_array( |
||
66 | 'array_merge', |
||
67 | array_map(function ($u, $v) use ($b, $c) { |
||
68 | return ["{$b}{$c}{$u}" => $v]; |
||
69 | }, array_keys($a), array_values($a)) |
||
70 | ); |
||
71 | }; |
||
72 | |||
73 | View Code Duplication | $array_key_clean = function (array $a) { |
|
74 | $a = count($a) ? call_user_func_array('array_merge', array_map(function ($k, $v) { |
||
75 | return [str_replace(':', '', $k) => $v]; |
||
76 | }, array_keys($a), array_values($a))) : $a; |
||
77 | |||
78 | return $a; |
||
79 | }; |
||
80 | |||
81 | $array_clean_merge = function (array $a, $b) { |
||
82 | return array_merge($a, call_user_func_array('array_merge', $b)); |
||
83 | }; |
||
84 | |||
85 | View Code Duplication | $array_key_clean_undot = function (array $a) { |
|
86 | $keys = preg_grep('/[\.\:]/', array_keys($a)); |
||
87 | if ($keys) { |
||
88 | foreach ($keys as $key) { |
||
89 | array_set($a, str_replace(':', '', $key), $a[$key]); |
||
90 | unset($a[$key]); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | return $a; |
||
95 | }; |
||
96 | |||
97 | /** @var Field $field */ |
||
98 | foreach ($this->buildEmbeddedForm()->fields() as $field) { |
||
99 | if (!$fieldRules = $field->getRules()) { |
||
100 | continue; |
||
101 | } |
||
102 | |||
103 | $column = $field->column(); |
||
104 | $columns = is_array($column) ? $column : [$column]; |
||
105 | View Code Duplication | if ($field instanceof Field\MultipleSelect) { |
|
106 | $availInput[$column] = array_filter($availInput[$column], 'strlen'); |
||
107 | $availInput[$column] = $availInput[$column] ?: null; |
||
108 | } |
||
109 | /* |
||
110 | * |
||
111 | * For single column field format rules to: |
||
112 | * [ |
||
113 | * 'extra.name' => 'required' |
||
114 | * 'extra.email' => 'required' |
||
115 | * ] |
||
116 | * |
||
117 | * For multiple column field with rules like 'required': |
||
118 | * 'extra' => [ |
||
119 | * 'start' => 'start_at' |
||
120 | * 'end' => 'end_at', |
||
121 | * ] |
||
122 | * |
||
123 | * format rules to: |
||
124 | * [ |
||
125 | * 'extra.start_atstart' => 'required' |
||
126 | * 'extra.end_atend' => 'required' |
||
127 | * ] |
||
128 | */ |
||
129 | $newColumn = array_map(function ($k, $v) use ($rel) { |
||
130 | //Fix ResetInput Function! A Headache Implementation! |
||
131 | return !$k ? "{$rel}.{$v}" : "{$rel}.{$v}:{$k}"; |
||
132 | }, array_keys($columns), array_values($columns)); |
||
133 | |||
134 | $fieldRules = is_array($fieldRules) ? implode('|', $fieldRules) : $fieldRules; |
||
135 | View Code Duplication | $newRules = array_map(function ($v) use ($fieldRules, $availInput) { |
|
136 | list($k, $c) = explode('.', $v); |
||
137 | //Fix ResetInput Function! A Headache Implementation! |
||
138 | $col = explode(':', $c)[0]; |
||
139 | |||
140 | if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) { |
||
141 | return $array_key_attach_str(preg_replace('/./', $fieldRules, $availInput[$k][$col]), $v, ':'); |
||
142 | } |
||
143 | |||
144 | //May Have Problem in Dealing with File Upload in Edit Mode |
||
145 | return [$v => $fieldRules]; |
||
146 | }, $newColumn); |
||
147 | $rules = $array_clean_merge($rules, $newRules); |
||
148 | |||
149 | View Code Duplication | $newInput = array_map(function ($v) use ($availInput, $array_key_attach_str) { |
|
150 | list($k, $c) = explode('.', $v); |
||
151 | //Fix ResetInput Function! A Headache Implementation! |
||
152 | $col = explode(':', $c)[0]; |
||
153 | if (!array_key_exists($col, $availInput[$k])) { |
||
154 | //May Have Problem in Dealing with File Upload in Edit Mode |
||
155 | return [$v => null]; |
||
156 | } |
||
157 | |||
158 | if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) { |
||
159 | return $array_key_attach_str($availInput[$k][$col], $v, ':'); |
||
160 | } |
||
161 | |||
162 | return [$v => $availInput[$k][$col]]; |
||
163 | }, $newColumn); |
||
164 | $newInputs = $array_clean_merge($newInputs, $newInput); |
||
165 | |||
166 | View Code Duplication | $newAttributes = array_map(function ($v) use ($field, $availInput) { |
|
167 | list($k, $c) = explode('.', $v); |
||
168 | //Fix ResetInput Function! A Headache Implementation! |
||
169 | $col = explode(':', $c)[0]; |
||
170 | if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) { |
||
171 | return call_user_func_array('array_merge', array_map(function ($u) use ($v, $field) { |
||
172 | $w = $field->label(); |
||
173 | //Fix ResetInput Function! A Headache Implementation! |
||
174 | $w .= is_array($field->column()) ? '['.explode(':', explode('.', $v)[2])[0].']' : ''; |
||
175 | |||
176 | return ["{$v}:{$u}" => $w]; |
||
177 | }, array_keys($availInput[$k][$col]))); |
||
178 | } |
||
179 | |||
180 | //May Have Problem in Dealing with File Upload in Edit Mode |
||
181 | $w = $field->label(); |
||
182 | //Fix ResetInput Function! A Headache Implementation! |
||
183 | $w .= is_array($field->column()) ? '['.explode(':', explode('.', $v)[2])[0].']' : ''; |
||
184 | |||
185 | return [$v => $w]; |
||
186 | }, $newColumn); |
||
187 | $attributes = $array_clean_merge($attributes, $newAttributes); |
||
188 | |||
189 | View Code Duplication | if ($field->validationMessages) { |
|
190 | $newMessages = array_map(function ($v) use ($field, $availInput, $array_key_attach_str) { |
||
191 | list($k, $c) = explode('.', $v); |
||
192 | //Fix ResetInput Function! A Headache Implementation! |
||
193 | $col = explode(':', $c)[0]; |
||
194 | if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) { |
||
195 | return call_user_func_array('array_merge', array_map(function ($u) use ($v, $field, $array_key_attach_str) { |
||
196 | return $array_key_attach_str($field->validationMessages, "{$v}:{$u}"); |
||
197 | }, array_keys($availInput[$k][$col]))); |
||
198 | } |
||
199 | |||
200 | //May Have Problem in Dealing with File Upload in Edit Mode |
||
201 | return $array_key_attach_str($field->validationMessages, $v); |
||
202 | }, $newColumn); |
||
203 | $messages = $array_clean_merge($messages, $newMessages); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | $rules = array_filter($rules, 'strlen'); |
||
208 | |||
209 | if (empty($rules)) { |
||
210 | return false; |
||
211 | } |
||
212 | |||
213 | $attributes = array_filter($attributes, 'strlen'); |
||
214 | $messages = array_filter($messages, 'strlen'); |
||
215 | $input = $array_key_clean_undot(array_filter($newInputs, 'strlen', ARRAY_FILTER_USE_KEY)); |
||
216 | $rules = $array_key_clean($rules); |
||
217 | $attributes = $array_key_clean($attributes); |
||
218 | $messages = $array_key_clean($messages); |
||
219 | |||
220 | if (empty($input)) { |
||
221 | $input = [$rel => $availInput]; |
||
222 | } |
||
223 | |||
224 | return Validator::make($input, $rules, $messages, $attributes); |
||
225 | } |
||
226 | |||
281 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.