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 | /** |
||
18 | * Validate given documents |
||
19 | * |
||
20 | * @param array $my_model |
||
21 | * @param array $my_doc |
||
22 | * @param string $my_key |
||
23 | * @return array |
||
24 | * @throws \Exception |
||
25 | */ |
||
26 | public static function validateDoc($my_model, $my_doc, $my_key = null) |
||
27 | { |
||
28 | $my_keys = array_keys($my_doc); |
||
29 | foreach ($my_keys as $key) { |
||
30 | // Does doc has a array that does not exist in model definition . |
||
31 | if (!isset($my_model[$key])) { |
||
32 | if ($my_key !== null) { |
||
33 | $my_key = strval($my_key)." . ".strval($key); |
||
34 | } else { |
||
35 | $my_key = $key; |
||
36 | } |
||
37 | throw new \Exception("Error for key '".$my_key."' that does not exist in the model"); |
||
38 | } // Is the value of the array[key] again another array? . |
||
39 | elseif (self::getType($my_doc[$key]) == "array") { |
||
40 | if ($my_key !== null) { |
||
41 | $my_key = strval($my_key)." . ".strval($key); |
||
42 | } else { |
||
43 | $my_key = $key; |
||
44 | } |
||
45 | // Validate this array too . |
||
46 | $my_doc[$key] = self::validateDoc($my_model[$key], $my_doc[$key], $my_key); |
||
47 | if (self::getType($my_doc[$key]) != "array") { |
||
48 | return $my_doc[$key]; |
||
49 | } |
||
50 | } // Is the value of the array[key] have same variable type |
||
51 | //that stated in the definition of the model array. |
||
52 | elseif (self::getType($my_doc[$key]) != $my_model[$key]['_type']) { |
||
53 | if ($my_key !== null) { |
||
54 | $my_key = $my_key." . ".$key; |
||
55 | } else { |
||
56 | $my_key = $key; |
||
57 | } |
||
58 | throw new \Exception("Error for key '".$my_key."'".", ".self::getType($my_doc[$key]). |
||
59 | " given but it must be ".$my_model[$key]['_type']); |
||
60 | } else { |
||
61 | $v_key = $key; |
||
62 | if ($my_key !== null) { |
||
63 | $v_key = $my_key." . ".$key; |
||
64 | } |
||
65 | $my_doc[$key] = self::validateDocItem($my_doc[$key], $my_model[$key], $v_key); |
||
66 | } |
||
67 | } |
||
68 | return $my_doc; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Fit document to given Model |
||
73 | * |
||
74 | * @param array $my_model |
||
75 | * @param array $my_doc |
||
76 | * @return array |
||
77 | */ |
||
78 | public static function fitDocToModel($my_model, $my_doc) |
||
79 | { |
||
80 | $my_keys = array_keys($my_doc); |
||
81 | foreach ($my_keys as $key) { |
||
82 | // If array has a key that is not presented in the model definition, unset it . |
||
83 | if (!isset($my_model[$key])) { |
||
84 | unset($my_doc[$key]); |
||
85 | } // If array[$key] is again an array, recursively fit this array too . |
||
86 | elseif (self::getType($my_doc[$key]) == "array" && !isset($my_model[$key]['_type'])) { |
||
87 | $my_doc[$key] = self::fitDocToModel($my_model[$key], $my_doc[$key]); |
||
88 | // If returned value is not an array, return it . |
||
89 | if (self::getType($my_doc[$key]) != "array") { |
||
90 | return $my_doc[$key]; |
||
91 | } |
||
92 | } elseif (self::getType($my_doc[$key]) == "array" && $my_model[$key]['_type'] != "array") { |
||
93 | $my_doc[$key] = $my_model[$key]['_default']; |
||
94 | } // If array[key] is not an array and not has same variable type that stated in the model definition . |
||
95 | else { |
||
96 | $my_doc[$key] = self::sanitizeDocItem($my_doc[$key], $my_model[$key]); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | return $my_doc; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param array $my_model |
||
105 | * @param array $my_doc |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public static function setModelDefaults($my_model, $my_doc) |
||
110 | { |
||
111 | $my_keys = array_keys($my_model); |
||
112 | $new_doc = []; |
||
113 | foreach ($my_keys as $key) { |
||
114 | $item_keys = array_keys($my_model[$key]); |
||
115 | // If one of the keys of $my_model[$key] is _type this is a definition, not a defined key |
||
116 | if (in_array("_type", $item_keys)) { |
||
117 | // If array does not have this key, set the default value . |
||
118 | if (!isset($my_doc[$key])) { |
||
119 | if (isset($my_model[$key]['_input_type'])) { |
||
120 | switch ($my_model[$key]['_input_type']) { |
||
121 | case 'uid': |
||
122 | $shortid = ShortId::create(); |
||
123 | $new_doc[$key] = $shortid->generate(); |
||
124 | break; |
||
125 | case 'date': |
||
126 | if ($my_model[$key]['_default'] == 'today') { |
||
127 | $new_doc[$key] = date("Y-m-d"); |
||
128 | } else { |
||
129 | $new_doc[$key] = $my_model[$key]['_default']; |
||
130 | } |
||
131 | break; |
||
132 | case 'timestamp': |
||
133 | $model_default = $my_model[$key]['_default']; |
||
134 | $model_type = $my_model[$key]['_type']; |
||
135 | if (($model_default == "now") && ($model_type == "integer")) { |
||
136 | $new_doc[$key] = time(); |
||
137 | } elseif ($model_default == "now" && ($model_type == "string")) { |
||
138 | $new_doc[$key] = date("Y-m-d H:i:s"); |
||
139 | } else { |
||
140 | $new_doc[$key] = $model_default; |
||
141 | } |
||
142 | break; |
||
143 | |||
144 | default: |
||
145 | $new_doc[$key] = $my_model[$key]['_default']; |
||
146 | } |
||
147 | } else { |
||
148 | $new_doc[$key] = $my_model[$key]['_default']; |
||
149 | } |
||
150 | } // If array has this key |
||
151 | else { |
||
152 | // If model definition stated this key's default value is not Null |
||
153 | // and has a wrong variable type, fix it. |
||
154 | if ($my_model[$key]['_default'] !== null) { |
||
155 | $key_type = self::getType($my_doc[$key]); |
||
156 | if ($key_type != $my_model[$key]['_type'] && $key_type == "array") { |
||
157 | $my_doc[$key] = $my_model[$key]['_default']; |
||
158 | } |
||
159 | settype($my_doc[$key], $my_model[$key]['_type']); |
||
160 | } |
||
161 | $new_doc[$key] = $my_doc[$key]; |
||
162 | } |
||
163 | $new_doc[$key] = self::sanitizeDocItem($new_doc[$key], $my_model[$key]); |
||
164 | } // If one of the keys is not _type, this is a defined key, recursively get sub keys . |
||
165 | else { |
||
166 | if (!isset($my_doc[$key])) { |
||
167 | $my_doc[$key] = ""; |
||
168 | } |
||
169 | $new_doc[$key] = self::setModelDefaults($my_model[$key], $my_doc[$key]); |
||
170 | } |
||
171 | } |
||
172 | return $new_doc; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param mixed $value |
||
177 | * @param array $my_model |
||
178 | * @param string $key |
||
179 | * |
||
180 | * @return mixed |
||
181 | * @throws \Exception |
||
182 | */ |
||
183 | public static function validateDocItem($value, $my_model, $key) |
||
313 | |||
314 | /** |
||
315 | * @param mixed $value |
||
316 | * @param array $my_model |
||
317 | * |
||
318 | * @return mixed |
||
319 | */ |
||
320 | public static function sanitizeDocItem($value, $my_model) |
||
371 | |||
372 | /** |
||
373 | * A Note: |
||
374 | * Since the built-in php function gettype returns "double" variabe type, here is the workaround function |
||
375 | * See http://php . net/manual/en/function . gettype . php => Possible values for the returned string are: |
||
376 | * "double" (for historical reasons "double" is returned in case of a float, and not simply "float") |
||
377 | * |
||
378 | * @param mixed $value |
||
379 | * @return string |
||
380 | */ |
||
381 | public static function getType($value) |
||
403 | } |
||
404 |