Conditions | 12 |
Paths | 32 |
Total Lines | 40 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
81 | private function addID($array, $parent_table = false, $last_increment = false) |
||
82 | { |
||
83 | |||
84 | if (!is_array($array)) |
||
85 | $array = json_decode($array, true);//if this is not the first time decode the text |
||
86 | |||
87 | $return_array = $array;//return array |
||
88 | |||
89 | foreach ($array ?? [] as $key => $array_item) { |
||
90 | |||
91 | if (!is_numeric($key) && $parent_table)//single array table, no column |
||
92 | $table_name = $parent_table . '_' . $key; |
||
93 | elseif ($parent_table)//multiple array items |
||
94 | $table_name = $parent_table; |
||
95 | else {//first time loop |
||
96 | $table_name = $this->main_table_name; |
||
97 | } |
||
98 | |||
99 | if (is_array($array_item)) {//if this is a array |
||
100 | $array_item = $this->addID($array_item, $table_name, $this->increment);//recursive the array |
||
101 | if (empty($array_item['id']) && empty($array_item[0])) {//if this is a single array with no child |
||
102 | $array_item = ['id' => $this->increment++] + $array_item;//add id |
||
103 | } |
||
104 | |||
105 | if(isset($array_item[0]) && !is_array($array_item[0]) && !is_object($array_item[0])){//reference table |
||
106 | |||
107 | |||
108 | $array_item = (object)array_map(function ($item){ |
||
109 | return (object)[ |
||
110 | 'id' => $this->increment++, |
||
111 | 'value'=>$item |
||
112 | ]; |
||
113 | },$array_item); |
||
114 | } |
||
115 | |||
116 | $return_array[$key] = $array_item; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | return $return_array; |
||
121 | |||
175 |