| Total Complexity | 55 |
| Total Lines | 266 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like JsonExtractor 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 JsonExtractor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class JsonExtractor |
||
| 11 | { |
||
| 12 | public $json; |
||
| 13 | |||
| 14 | private $table = []; |
||
| 15 | private $data = []; |
||
| 16 | |||
| 17 | public $need_id = true; |
||
| 18 | public $snake_case_column = true; |
||
| 19 | public $snake_case_table = true; |
||
| 20 | public $main_table_name = "main"; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * JsonExtractor constructor. |
||
| 24 | * |
||
| 25 | * @param Json $json |
||
| 26 | */ |
||
| 27 | public function __construct(Json $json) |
||
| 28 | { |
||
| 29 | $this->json = $json; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @return array |
||
| 34 | */ |
||
| 35 | public function getTablesArray() |
||
| 36 | { |
||
| 37 | return $this->table; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @return array |
||
| 42 | */ |
||
| 43 | public function getDataArray() |
||
| 44 | { |
||
| 45 | return $this->data; |
||
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * @param bool $data |
||
| 51 | * @param string $prefix |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | public function toMysqlTables($data = false, $prefix = '') |
||
| 55 | { |
||
| 56 | if (!$data) {//if this is not a recursive |
||
| 57 | $data = $this->json->toObject(); |
||
| 58 | } |
||
| 59 | |||
| 60 | |||
| 61 | foreach ($data as $key => $value) {//loop the data |
||
| 62 | |||
| 63 | $table_name = is_numeric($key) ? $this->main_table_name : $key; |
||
| 64 | |||
| 65 | if (is_array($value) && is_object($value[0])) {//check whether it's a array and it's firs element is a object |
||
| 66 | |||
| 67 | $table_data = $this->getTable($prefix . $table_name, $value);//get table sql |
||
| 68 | $this->table[$table_data['tables']['name']] = $table_data['tables']['column']; |
||
| 69 | $this->data[$table_data['data']['table']] = $table_data['data']['data']; |
||
| 70 | |||
| 71 | $this->toMysqlTables($this->getHighestColumnArray($value), $prefix . $table_name . '_');//get it inside tables |
||
| 72 | } elseif (is_array($value) || is_object($value)) {//if it's a array and firs element is not a object |
||
| 73 | |||
| 74 | $table_data = $this->getTable($prefix . $table_name, $value); |
||
| 75 | $this->table[$table_data['tables']['name']] = $table_data['tables']['column']; |
||
| 76 | $this->data[$table_data['data']['table']] = $table_data['data']['data']; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * @param $table |
||
| 84 | * @param $data |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | public function getTable($table, $data) |
||
| 114 | ] |
||
| 115 | ]; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param $data |
||
| 120 | * |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function getColumn($data) |
||
| 124 | { |
||
| 125 | $Columns = []; |
||
| 126 | |||
| 127 | if (is_object($data)) { |
||
| 128 | foreach ($data ?? [] as $Column => $Value) { |
||
| 129 | if (!is_array($Value) && !is_object($Value) && !empty($Column) && !is_numeric($Column)) { |
||
| 130 | $Columns[] = ['name' => $Column, 'type' => gettype($this->getActualDataType($Value, ""))]; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } elseif (is_array($data)) { |
||
| 134 | $Columns[] = ['name' => 'value', 'type' => gettype($this->getActualDataType($data[0], ""))]; |
||
| 135 | } |
||
| 136 | |||
| 137 | return $Columns; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $data |
||
| 142 | * @param $column |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | public function getData($data, $column) |
||
| 146 | { |
||
| 147 | $values = []; |
||
| 148 | |||
| 149 | $index = 0; |
||
| 150 | foreach ($data as $row_item) { |
||
| 151 | foreach ($column as $column_item) { |
||
| 152 | switch (is_object($row_item)) { |
||
| 153 | case true: |
||
| 154 | $values[$index][$column_item['name']] = $this->getActualDataType(($row_item->{$column_item['name']}) ?? null, null); |
||
| 155 | break; |
||
| 156 | case false: |
||
| 157 | $values[$index][$column_item['name']] = $this->getActualDataType(($data->{$column_item['name']}) ?? null, null); |
||
| 158 | break; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | $index++; |
||
| 162 | } |
||
| 163 | |||
| 164 | |||
| 165 | return $values; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param $input |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public static function snakeCase($input) |
||
| 174 | { |
||
| 175 | preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); |
||
| 176 | $ret = $matches[0]; |
||
| 177 | |||
| 178 | foreach ($ret as &$match) { |
||
| 179 | $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); |
||
| 180 | } |
||
| 181 | |||
| 182 | return implode('_', $ret); |
||
| 183 | } |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * @param $array |
||
| 188 | * @return bool|mixed |
||
| 189 | */ |
||
| 190 | public static function getHighestColumnArray($array) |
||
| 191 | { |
||
| 192 | if (is_object($array) || (is_array($array) && !is_object($array[0]) && !is_array($array[0]))) { |
||
| 193 | return $array; |
||
| 194 | } |
||
| 195 | |||
| 196 | $Highest = false; |
||
| 197 | $ColumnCount = false; |
||
| 198 | $HighestSubCount = false; |
||
| 199 | |||
| 200 | foreach ($array as $array_item) { |
||
| 201 | $current_sub = 0; |
||
| 202 | //check how many array/object have |
||
| 203 | foreach ($array_item as $SubTableName => $SubArrayItem) { |
||
| 204 | if (is_array($SubArrayItem) || is_object($SubArrayItem)) { |
||
| 205 | $current_sub = $current_sub + 2; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | //check how many column have |
||
| 209 | if ($ColumnCount <= count($array_item) || !$Highest) { |
||
| 210 | if ($current_sub > $HighestSubCount || !$Highest) { |
||
| 211 | $Highest = $array_item; |
||
| 212 | $HighestSubCount = $current_sub; |
||
| 213 | $ColumnCount = count($array_item); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | |||
| 219 | return $Highest; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param $array |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public static function getStringFromData($array) |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param $array |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public static function getStringFromColumns($array) |
||
| 252 | { |
||
| 253 | $String = []; |
||
| 254 | foreach ($array as $column) { |
||
| 255 | $String[] = "`" . JsonExtractor::snakeCase($column['name']) . "`"; |
||
| 256 | } |
||
| 257 | |||
| 258 | return implode(",", $String); |
||
| 259 | } |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * @param $Data |
||
| 264 | * @param $empty_val |
||
| 265 | * @return int|string |
||
| 266 | */ |
||
| 267 | public static function getActualDataType($Data, $empty_val) |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } |
||
| 279 |