| Total Complexity | 62 | 
| Total Lines | 283 | 
| 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()  | 
            ||
| 38 | }  | 
            ||
| 39 | |||
| 40 | /**  | 
            ||
| 41 | * @return array  | 
            ||
| 42 | */  | 
            ||
| 43 | public function getDataArray()  | 
            ||
| 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 |         foreach ($data as $key => $value) {//loop the data | 
            ||
| 61 | |||
| 62 | $table_name = is_numeric($key) ? $this->main_table_name : $key;  | 
            ||
| 63 | |||
| 64 |             if (is_array($value) && is_object($value[0])) {//check whether it's a array and it's firs element is a object | 
            ||
| 65 | |||
| 66 | $table_data = $this->getTable($prefix . $table_name, $value);//get table sql  | 
            ||
| 67 | $this->table[$table_data['name']] = $table_data['column'];  | 
            ||
| 68 | |||
| 69 | $this->toMysqlTables($this->getHighestColumnArray($value), $prefix . $table_name . '_');//get it inside tables  | 
            ||
| 70 |             } elseif (is_array($value) || is_object($value)) {//if it's a array and  firs element is not a object | 
            ||
| 71 | |||
| 72 | $table_data = $this->getTable($prefix . $table_name, $value);  | 
            ||
| 73 | $this->table[$table_data['name']] = $table_data['column'];  | 
            ||
| 74 | }  | 
            ||
| 75 | |||
| 76 | }  | 
            ||
| 77 | }  | 
            ||
| 78 | |||
| 79 | |||
| 80 | public function toMysqlData($data = false, $prefix = '')  | 
            ||
| 81 |     { | 
            ||
| 82 | |||
| 83 |         if (!$data) {//if this is not a recursive | 
            ||
| 84 | $data = $this->json->toObject();  | 
            ||
| 85 | }  | 
            ||
| 86 |         foreach ($data as $table_name => $value) { | 
            ||
| 87 |             if ($this->snake_case_table) { | 
            ||
| 88 | $table_name = $this->snakeCase($prefix . $table_name);  | 
            ||
| 89 | }  | 
            ||
| 90 | |||
| 91 |             if (is_array($value)) {//if it's a array and  firs element is not a object | 
            ||
| 92 | $this->toMysqlData($value, $table_name . '_');  | 
            ||
| 93 |             } elseif (is_object($value)) { | 
            ||
| 94 | $this->toMysqlData($value, $table_name . '_');  | 
            ||
| 95 | }  | 
            ||
| 96 | |||
| 97 | |||
| 98 | }  | 
            ||
| 99 | }  | 
            ||
| 100 | |||
| 101 | //TODO  | 
            ||
| 102 |     public function getTableData(){ | 
            ||
| 103 | |||
| 104 | }  | 
            ||
| 105 | |||
| 106 | |||
| 107 | /**  | 
            ||
| 108 | * @param $table  | 
            ||
| 109 | * @param $data  | 
            ||
| 110 | * @return array  | 
            ||
| 111 | */  | 
            ||
| 112 | public function getTable($table, $data)  | 
            ||
| 131 | ];  | 
            ||
| 132 | }  | 
            ||
| 133 | |||
| 134 | /**  | 
            ||
| 135 | * @param $data  | 
            ||
| 136 | *  | 
            ||
| 137 | * @return array  | 
            ||
| 138 | */  | 
            ||
| 139 | public function getColumn($data)  | 
            ||
| 140 |     { | 
            ||
| 141 | $Columns = [];  | 
            ||
| 142 | |||
| 143 |         if (is_object($data)) { | 
            ||
| 144 |             foreach ($data ?? [] as $Column => $Value) { | 
            ||
| 145 |                 if (!is_array($Value) && !is_object($Value) && !empty($Column) && !is_numeric($Column)) { | 
            ||
| 146 | $Columns[] = ['name' => $Column, 'type' => gettype($this->getActualDataType($Value, ""))];  | 
            ||
| 147 | }  | 
            ||
| 148 | }  | 
            ||
| 149 |         } elseif (is_array($data)) { | 
            ||
| 150 | $Columns[] = ['name' => 'value', 'type' => gettype($this->getActualDataType($data[0], ""))];  | 
            ||
| 151 | }  | 
            ||
| 152 | |||
| 153 | return $Columns;  | 
            ||
| 154 | }  | 
            ||
| 155 | |||
| 156 | /**  | 
            ||
| 157 | * @param $data  | 
            ||
| 158 | * @param $column  | 
            ||
| 159 | * @return array  | 
            ||
| 160 | */  | 
            ||
| 161 | public function getData($data, $column)  | 
            ||
| 162 |     { | 
            ||
| 163 | |||
| 164 | $values = [];  | 
            ||
| 165 | |||
| 166 | $index = 0;  | 
            ||
| 167 |         foreach ($data as $row_item) { | 
            ||
| 168 |             foreach ($column as $column_item) { | 
            ||
| 169 |                 switch (is_object($row_item)) { | 
            ||
| 170 | case true:  | 
            ||
| 171 |                         $values[$index][$column_item['name']] = $this->getActualDataType(($row_item->{$column_item['name']}) ?? null, null); | 
            ||
| 172 | break;  | 
            ||
| 173 | case false:  | 
            ||
| 174 |                         $values[$index][$column_item['name']] = $this->getActualDataType(($data->{$column_item['name']}) ?? null, null); | 
            ||
| 175 | break;  | 
            ||
| 176 | }  | 
            ||
| 177 | }  | 
            ||
| 178 | $index++;  | 
            ||
| 179 | }  | 
            ||
| 180 | |||
| 181 | |||
| 182 | return $values;  | 
            ||
| 183 | }  | 
            ||
| 184 | |||
| 185 | /**  | 
            ||
| 186 | * @param $input  | 
            ||
| 187 | *  | 
            ||
| 188 | * @return string  | 
            ||
| 189 | */  | 
            ||
| 190 | public static function snakeCase($input)  | 
            ||
| 191 |     { | 
            ||
| 192 |         preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); | 
            ||
| 193 | $ret = $matches[0];  | 
            ||
| 194 | |||
| 195 |         foreach ($ret as &$match) { | 
            ||
| 196 | $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);  | 
            ||
| 197 | }  | 
            ||
| 198 | |||
| 199 |         return implode('_', $ret); | 
            ||
| 200 | }  | 
            ||
| 201 | |||
| 202 | |||
| 203 | /**  | 
            ||
| 204 | * @param $array  | 
            ||
| 205 | * @return bool|mixed  | 
            ||
| 206 | */  | 
            ||
| 207 | public static function getHighestColumnArray($array)  | 
            ||
| 208 |     { | 
            ||
| 209 |         if (is_object($array) || (is_array($array) && !is_object($array[0]) && !is_array($array[0]))) { | 
            ||
| 210 | return $array;  | 
            ||
| 211 | }  | 
            ||
| 212 | |||
| 213 | $Highest = false;  | 
            ||
| 214 | $ColumnCount = false;  | 
            ||
| 215 | $HighestSubCount = false;  | 
            ||
| 216 | |||
| 217 |         foreach ($array as $array_item) { | 
            ||
| 218 | $current_sub = 0;  | 
            ||
| 219 | //check how many array/object have  | 
            ||
| 220 |             foreach ($array_item as $SubTableName => $SubArrayItem) { | 
            ||
| 221 |                 if (is_array($SubArrayItem) || is_object($SubArrayItem)) { | 
            ||
| 222 | $current_sub = $current_sub + 2;  | 
            ||
| 223 | }  | 
            ||
| 224 | }  | 
            ||
| 225 | //check how many column have  | 
            ||
| 226 |             if ($ColumnCount <= count($array_item) || !$Highest) { | 
            ||
| 227 |                 if ($current_sub > $HighestSubCount || !$Highest) { | 
            ||
| 228 | $Highest = $array_item;  | 
            ||
| 229 | $HighestSubCount = $current_sub;  | 
            ||
| 230 | $ColumnCount = count($array_item);  | 
            ||
| 231 | }  | 
            ||
| 232 | }  | 
            ||
| 233 | }  | 
            ||
| 234 | |||
| 235 | |||
| 236 | return $Highest;  | 
            ||
| 237 | }  | 
            ||
| 238 | |||
| 239 | /**  | 
            ||
| 240 | * @param $array  | 
            ||
| 241 | * @return string  | 
            ||
| 242 | */  | 
            ||
| 243 | public static function getStringFromData($array)  | 
            ||
| 262 | }  | 
            ||
| 263 | |||
| 264 | /**  | 
            ||
| 265 | * @param $array  | 
            ||
| 266 | * @return string  | 
            ||
| 267 | */  | 
            ||
| 268 | public static function getStringFromColumns($array)  | 
            ||
| 269 |     { | 
            ||
| 270 | $String = [];  | 
            ||
| 271 |         foreach ($array as $column) { | 
            ||
| 272 | $String[] = "`" . JsonExtractor::snakeCase($column['name']) . "`";  | 
            ||
| 273 | }  | 
            ||
| 274 | |||
| 275 |         return implode(",", $String); | 
            ||
| 276 | }  | 
            ||
| 277 | |||
| 278 | |||
| 279 | /**  | 
            ||
| 280 | * @param $Data  | 
            ||
| 281 | * @param $empty_val  | 
            ||
| 282 | * @return int|string  | 
            ||
| 283 | */  | 
            ||
| 284 | public static function getActualDataType($Data, $empty_val)  | 
            ||
| 293 | }  | 
            ||
| 294 | }  | 
            ||
| 295 | }  | 
            ||
| 296 |