Complex classes like ModelUtils 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ModelUtils, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class ModelUtils |
||
16 | { |
||
17 | static protected $field_attributes = [ |
||
18 | '_type' => null, |
||
19 | '_input_type' => null, |
||
20 | '_min_length' => null, |
||
21 | '_max_length' => null, |
||
22 | '_in_options' => null, |
||
23 | '_input_format' => null, |
||
24 | '_required' => null |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * Validate given documents |
||
29 | * |
||
30 | * @param array $my_model |
||
31 | * @param array $my_doc |
||
32 | * @param string $my_key |
||
33 | * @return array |
||
34 | * @throws \Exception |
||
35 | */ |
||
36 | public static function validateDoc($my_model, $my_doc, $my_key = null) |
||
37 | { |
||
38 | $my_keys = array_keys($my_doc); |
||
39 | foreach ($my_keys as $key) { |
||
40 | |||
41 | $my_doc_key_type = self::getType($my_doc[$key]); |
||
42 | $v_key = $key; |
||
43 | if ($my_key !== null) { |
||
44 | $v_key = strval($my_key).".".strval($key); |
||
45 | } |
||
46 | // Does doc has a array that does not exist in model definition. |
||
47 | if (!isset($my_model[$key])) { |
||
48 | throw new \Exception("Error for key '".$v_key."' that does not exist in the model"); |
||
49 | } // Is the value of the array[key] again another array? . |
||
50 | elseif ($my_doc_key_type == "array") { |
||
51 | // Validate this array too. |
||
52 | $my_doc[$key] = self::validateDoc($my_model[$key], $my_doc[$key], $v_key); |
||
53 | if (self::getType($my_doc[$key]) != "array") { |
||
54 | return $my_doc[$key]; |
||
55 | } |
||
56 | } // Is the value of the array[key] have same variable type |
||
57 | //that stated in the definition of the model array. |
||
58 | elseif ($my_doc_key_type != $my_model[$key]['_type']) { |
||
59 | throw new \Exception("Error for key '".$v_key."'".", ".$my_doc_key_type. |
||
60 | " given but it must be ".$my_model[$key]['_type']); |
||
61 | } else { |
||
62 | $my_doc[$key] = self::validateDocItem($my_doc[$key], $my_model[$key], $v_key); |
||
63 | } |
||
64 | } |
||
65 | return $my_doc; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param mixed $value |
||
70 | * @param array $my_model |
||
71 | * @param string $key |
||
72 | * |
||
73 | * @return mixed |
||
74 | * @throws \Exception |
||
75 | */ |
||
76 | private static function validateDocItem($value, $my_model, $key) |
||
77 | { |
||
78 | $my_model = self::setDefaultModelAttributes($my_model); |
||
79 | if (self::getType($value) != $my_model['_type']) { |
||
80 | return false; |
||
81 | } |
||
82 | if ($my_model['_input_type'] !== null) { |
||
83 | self::filterValidate($my_model['_input_type'], $key, $value, $my_model['_input_format']); |
||
84 | } |
||
85 | self::checkMinMaxInOptions($my_model['_type'], $key, $value, $my_model['_min_length'], $my_model['_max_length'], $my_model['_in_options']); |
||
86 | return $value; |
||
87 | } |
||
88 | |||
89 | private static function checkMinMaxInOptions($type, $key, $value, $min_length, $max_length, $in_options) |
||
120 | |||
121 | private static function filterValidate($input_type, $key, $value, $format) |
||
180 | |||
181 | /** |
||
182 | * Fit document to given Model |
||
183 | * |
||
184 | * @param array $my_model |
||
185 | * @param array $my_doc |
||
186 | * @return array |
||
187 | */ |
||
188 | public static function fitDocToModel($my_model, $my_doc) |
||
212 | |||
213 | /** |
||
214 | * @param array $my_model |
||
215 | * @param array $my_doc |
||
216 | * |
||
217 | * @return array |
||
218 | */ |
||
219 | public static function setModelDefaults($my_model, $my_doc) |
||
284 | |||
285 | private static function setDefaultModelAttributes($my_model){ |
||
286 | |||
289 | |||
290 | /** |
||
291 | * @param mixed $value |
||
292 | * @param array $my_model |
||
293 | * |
||
294 | * @return mixed |
||
295 | */ |
||
296 | private static function sanitizeDocItem($value, $my_model) |
||
307 | |||
308 | private static function setMaxMinInOptions($type, $value, $min_length, $max_length, $in_options) |
||
332 | /** |
||
333 | * A Note: |
||
334 | * Since the built-in php function gettype returns "double" variabe type, here is the workaround function |
||
335 | * See http://php . net/manual/en/function . gettype . php => Possible values for the returned string are: |
||
336 | * "double" (for historical reasons "double" is returned in case of a float, and not simply "float") |
||
337 | * |
||
338 | * @param mixed $value |
||
339 | * @return string |
||
340 | */ |
||
341 | private static function getType($value) |
||
356 | } |
||
357 |