| Conditions | 12 |
| Paths | 32 |
| Total Lines | 39 |
| Code Lines | 22 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 77 | private function addID($array, $parent_table = false) |
||
| 78 | { |
||
| 79 | if (!is_array($array)) { |
||
| 80 | $array = json_decode($array, true); |
||
| 81 | }//if this is not the first time decode the text |
||
| 82 | |||
| 83 | $return_array = $array;//return array |
||
| 84 | |||
| 85 | foreach ($array ?? [] as $key => $array_item) { |
||
| 86 | if (!is_numeric($key) && $parent_table) {//single array table, no column |
||
| 87 | $table_name = $parent_table . '_' . $key; |
||
|
|
|||
| 88 | } elseif ($parent_table) {//multiple array items |
||
| 89 | $table_name = $parent_table; |
||
| 90 | } else {//first time loop |
||
| 91 | $table_name = $this->main_table_name; |
||
| 92 | } |
||
| 93 | |||
| 94 | if (is_array($array_item)) {//if this is a array |
||
| 95 | $array_item = $this->addID($array_item, $table_name, $this->increment);//recursive the array |
||
| 96 | if (empty($array_item['id']) && empty($array_item[0])) {//if this is a single array with no child |
||
| 97 | $array_item = ['id' => $this->increment++] + $array_item;//add id |
||
| 98 | } |
||
| 99 | |||
| 100 | if (isset($array_item[0]) && !is_array($array_item[0]) && !is_object($array_item[0])) {//reference table |
||
| 101 | |||
| 102 | |||
| 103 | $array_item = (object)array_map(function ($item) { |
||
| 104 | return (object)[ |
||
| 105 | 'id' => $this->increment++, |
||
| 106 | 'value'=>$item |
||
| 107 | ]; |
||
| 108 | }, $array_item); |
||
| 109 | } |
||
| 110 | |||
| 111 | $return_array[$key] = $array_item; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return $return_array; |
||
| 116 | } |
||
| 164 |