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 | // Does doc has a array that does not exist in model definition. |
||
| 44 | if (!isset($my_model[$key])) { |
||
| 45 | if ($my_key !== null) { |
||
| 46 | $v_key = strval($my_key).".".strval($key); |
||
| 47 | } |
||
| 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 | if ($my_key !== null) { |
||
| 52 | $v_key = strval($my_key).".".strval($key); |
||
| 53 | } |
||
| 54 | // Validate this array too. |
||
| 55 | $my_doc[$key] = self::validateDoc($my_model[$key], $my_doc[$key], $v_key); |
||
| 56 | if (self::getType($my_doc[$key]) != "array") { |
||
| 57 | return $my_doc[$key]; |
||
| 58 | } |
||
| 59 | } // Is the value of the array[key] have same variable type |
||
| 60 | //that stated in the definition of the model array. |
||
| 61 | elseif ($my_doc_key_type != $my_model[$key]['_type']) { |
||
| 62 | if ($my_key !== null) { |
||
| 63 | $v_key = $my_key." . ".$key; |
||
| 64 | } |
||
| 65 | throw new \Exception("Error for key '".$v_key."'".", ".$my_doc_key_type. |
||
| 66 | " given but it must be ".$my_model[$key]['_type']); |
||
| 67 | } else { |
||
| 68 | if ($my_key !== null) { |
||
| 69 | $v_key = $my_key." . ".$key; |
||
| 70 | } |
||
| 71 | $my_doc[$key] = self::validateDocItem($my_doc[$key], $my_model[$key], $v_key); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | return $my_doc; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param mixed $value |
||
| 79 | * @param array $my_model |
||
| 80 | * @param string $key |
||
| 81 | * |
||
| 82 | * @return mixed |
||
| 83 | * @throws \Exception |
||
| 84 | */ |
||
| 85 | private static function validateDocItem($value, $my_model, $key) |
||
| 86 | { |
||
| 87 | $my_model = self::setDefaultModelAttributes($my_model); |
||
| 88 | if (self::getType($value) != $my_model['_type']) { |
||
| 89 | return false; |
||
| 90 | } |
||
| 91 | if ($my_model['_input_type'] !== null) { |
||
| 92 | self::filterValidate($my_model['_input_type'], $key, $value, $my_model['_input_format']); |
||
| 93 | } |
||
| 94 | self::checkMinMaxInOptions($my_model['_type'], $key, $value, $my_model['_min_length'], $my_model['_max_length'], $my_model['_in_options']); |
||
| 95 | return $value; |
||
| 96 | } |
||
| 97 | |||
| 98 | private static function checkMinMaxInOptions($type, $key, $value, $min_length, $max_length, $in_options) |
||
| 99 | { |
||
| 100 | switch ($type) { |
||
| 101 | case 'integer': |
||
| 102 | case 'float': |
||
| 103 | if ($min_length !== null && ($value<$min_length)) { |
||
| 104 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
| 105 | "validation: Must be bigger than ".$min_length." "); |
||
| 106 | |||
| 107 | } |
||
| 108 | if ($max_length !== null && ($value>$max_length)) { |
||
| 109 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
| 110 | "validation: Must be smallerr than ".$max_length." "); |
||
| 111 | } |
||
| 112 | break; |
||
| 113 | default: |
||
| 114 | if ($max_length !== null && (strlen($value)>$max_length)) { |
||
| 115 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
| 116 | "validation: It's length must be smaller than ".$max_length." "); |
||
| 117 | } |
||
| 118 | if ($min_length !== null && (strlen($value)<$min_length)) { |
||
| 119 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
| 120 | "validation: It's length must be longer than ".$min_length." "); |
||
| 121 | } |
||
| 122 | break; |
||
| 123 | } |
||
| 124 | if ($in_options !== null && (!in_array($value, $in_options))) { |
||
| 125 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the validation: ". |
||
| 126 | "It's length must be one of the these values: ".implode(", ", $in_options)." "); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | private static function filterValidate($input_type, $key, $value, $format) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Fit document to given Model |
||
| 192 | * |
||
| 193 | * @param array $my_model |
||
| 194 | * @param array $my_doc |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | public static function fitDocToModel($my_model, $my_doc) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param array $my_model |
||
| 224 | * @param array $my_doc |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public static function setModelDefaults($my_model, $my_doc) |
||
| 293 | |||
| 294 | private static function setDefaultModelAttributes($my_model){ |
||
| 295 | |||
| 296 | return array_merge(static::$field_attributes, $my_model); |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param mixed $value |
||
| 301 | * @param array $my_model |
||
| 302 | * |
||
| 303 | * @return mixed |
||
| 304 | */ |
||
| 305 | private static function sanitizeDocItem($value, $my_model) |
||
| 316 | |||
| 317 | private static function setMaxMinInOptions($type, $value, $min_length, $max_length, $in_options) |
||
| 341 | /** |
||
| 342 | * A Note: |
||
| 343 | * Since the built-in php function gettype returns "double" variabe type, here is the workaround function |
||
| 344 | * See http://php . net/manual/en/function . gettype . php => Possible values for the returned string are: |
||
| 345 | * "double" (for historical reasons "double" is returned in case of a float, and not simply "float") |
||
| 346 | * |
||
| 347 | * @param mixed $value |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | private static function getType($value) |
||
| 365 | } |
||
| 366 |