Total Complexity | 47 |
Total Lines | 353 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Field often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Field |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | protected $args; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $dependencies; |
||
19 | |||
20 | public function __construct() |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @param string $property |
||
28 | * |
||
29 | * @return mixed |
||
30 | * @throws Exception |
||
31 | */ |
||
32 | public function __get($property) |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Get a specific Field. |
||
44 | * |
||
45 | * @return mixed GeminiLabs\SiteReviews\Html\Fields\* |
||
46 | */ |
||
47 | public function getField(array $args = []) |
||
48 | { |
||
49 | if (empty($args)) { |
||
50 | $args = $this->args; |
||
51 | } |
||
52 | |||
53 | $className = sprintf('GeminiLabs\Castor\Forms\Fields\%s', ucfirst($args['type'])); |
||
54 | |||
55 | if (!class_exists($className)) { |
||
56 | throw new ReflectionException("Class does not exist: {$className}"); |
||
57 | } |
||
58 | |||
59 | return new $className($args); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Normalize the field arguments. |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function normalize(array $args = []) |
||
68 | { |
||
69 | $defaults = [ |
||
70 | 'after' => '', |
||
71 | 'attributes' => '', |
||
72 | 'before' => '', |
||
73 | 'class' => '', |
||
74 | 'default' => null, |
||
75 | 'depends' => null, |
||
76 | 'desc' => '', |
||
77 | 'errors' => [], |
||
78 | 'inline' => false, |
||
79 | 'label' => '', |
||
80 | 'name' => '', |
||
81 | 'options' => [], |
||
82 | 'path' => '', |
||
83 | 'placeholder' => '', |
||
84 | 'prefix' => '', |
||
85 | 'render' => true, |
||
86 | 'suffix' => null, |
||
87 | 'type' => 'text', |
||
88 | 'value' => '', |
||
89 | ]; |
||
90 | |||
91 | $args = $atts = wp_parse_args($args, $defaults); |
||
92 | |||
93 | $args['attributes'] = $this->parseAttributes($atts); |
||
94 | $args['id'] = $this->parseId($atts); |
||
95 | $args['inline'] = $this->parseInline($atts); |
||
96 | $args['type'] = $this->parseType($atts); |
||
97 | $args['name'] = $this->parseName($atts); |
||
98 | $args['options'] = (array) $atts['options']; // make sure this is always an array |
||
99 | $args['path'] = $atts['name']; |
||
100 | $args['prefix'] = $this->parsePrefix($atts); |
||
101 | $args['value'] = $this->parseValue($atts); |
||
102 | |||
103 | $this->args = $args; |
||
104 | $this->dependencies = $this->getField($args)->dependencies; |
||
105 | |||
106 | $this->setDataDepends(); |
||
107 | $this->checkForErrors($atts); |
||
108 | |||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Render the field. |
||
114 | * |
||
115 | * @param mixed $print |
||
116 | * |
||
117 | * @return string|void |
||
118 | */ |
||
119 | public function render($print = true) |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Reset the Field. |
||
160 | * |
||
161 | * @return self |
||
162 | */ |
||
163 | public function reset() |
||
164 | { |
||
165 | $this->args = []; |
||
166 | |||
167 | return $this; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Check for form submission field errors. |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | protected function checkForErrors(array $atts) |
||
176 | { |
||
177 | $args = $this->args; |
||
178 | |||
179 | if (!array_key_exists($atts['name'], $args['errors'])) { |
||
180 | $this->args['errors'] = ''; // set to an empty string |
||
181 | return; |
||
182 | } |
||
183 | |||
184 | $field_errors = $args['errors'][$atts['name']]; |
||
185 | |||
186 | $errors = array_reduce($field_errors['errors'], function ($carry, $error) { |
||
187 | return $carry.sprintf('<span>%s</span> ', $error); |
||
188 | }); |
||
189 | |||
190 | $this->args['errors'] = sprintf('<span class="glsr-field-errors">%s</span>', $errors); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Parse the field attributes and convert to an array if needed. |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | protected function parseAttributes(array $args) |
||
199 | { |
||
200 | if (empty($args['attributes'])) { |
||
201 | return []; |
||
202 | } |
||
203 | |||
204 | $attributes = (array) $args['attributes']; |
||
205 | |||
206 | foreach ($attributes as $key => $value) { |
||
207 | if (is_string($key)) { |
||
208 | continue; |
||
209 | } |
||
210 | unset($attributes[$key]); |
||
211 | if (!isset($attributes[$value])) { |
||
212 | $attributes[$value] = ''; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | return $attributes; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Parse the field ID from the field path. |
||
221 | * |
||
222 | * @return string|null |
||
223 | */ |
||
224 | protected function parseId(array $args) |
||
225 | { |
||
226 | if (isset($args['id']) && !$args['id']) { |
||
227 | return; |
||
228 | } |
||
229 | |||
230 | !$args['suffix'] ?: $args['suffix'] = "-{$args['suffix']}"; |
||
231 | |||
232 | return str_replace(['[]', '[', ']', '.'], ['', '-', '', '-'], $this->parseName($args).$args['suffix']); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Parse the field inline. |
||
237 | * |
||
238 | * @return bool |
||
239 | */ |
||
240 | protected function parseInline(array $args) |
||
241 | { |
||
242 | return false !== stripos($args['type'], '_inline') |
||
243 | ? true |
||
244 | : $args['inline']; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Parse the field name. |
||
249 | * |
||
250 | * @return string |
||
251 | */ |
||
252 | protected function parseName(array $args) |
||
253 | { |
||
254 | $name = $args['name']; |
||
255 | $prefix = $this->parsePrefix($args); |
||
256 | |||
257 | if (false === $prefix) { |
||
258 | return $name; |
||
259 | } |
||
260 | |||
261 | $paths = explode('.', $name); |
||
262 | |||
263 | return array_reduce($paths, function ($result, $value) { |
||
264 | return $result .= "[$value]"; |
||
265 | }, $prefix); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Parse the field prefix. |
||
270 | * |
||
271 | * @return string|false |
||
272 | */ |
||
273 | protected function parsePrefix(array $args) |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Parse the field type. |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | protected function parseType(array $args) |
||
284 | { |
||
285 | $type = $args['type']; |
||
286 | |||
287 | return false !== stripos($type, '_inline') |
||
288 | ? str_replace('_inline', '', $type) |
||
289 | : $type; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Parse the field value. |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | protected function parseValue(array $args) |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Get the [data-depends] attribute. |
||
315 | * |
||
316 | * @return array|null |
||
317 | */ |
||
318 | public function getDataDepends() |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Set the field value. |
||
325 | * |
||
326 | * @return self |
||
327 | */ |
||
328 | public function setValue() |
||
329 | { |
||
330 | return $this; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Set the [data-depends] attribute. |
||
335 | * |
||
336 | * @return array|null |
||
337 | */ |
||
338 | protected function setDataDepends() |
||
361 | ]; |
||
362 | } |
||
363 | } |
||
364 |