Conditions | 11 |
Paths | 42 |
Total Lines | 38 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
124 | private function addForeign($array, $parent_table = null, $parent_key = false) |
||
125 | { |
||
126 | if (!is_array($array)) { |
||
127 | $array = json_decode($array, true); |
||
128 | } |
||
129 | |||
130 | $return_array = $array; |
||
131 | foreach ($array ?? [] as $key => $array_item) { |
||
132 | if (!is_numeric($key) && $parent_table) {//single array table, no column |
||
133 | $table_name = $parent_table . '_' . $key; |
||
134 | } elseif ($parent_table) {//multiple array items |
||
135 | $table_name = $parent_table; |
||
136 | } elseif (!is_numeric($key)) { //first time loop |
||
137 | $table_name = $key; |
||
138 | } else {//first time loop |
||
139 | $table_name = $this->main_table_name; |
||
140 | } |
||
141 | |||
142 | |||
143 | if (is_array($array_item)) { |
||
144 | $array_item = $this->addForeign($array_item, $table_name, $array_item['id'] ?? ($parent_key ?? false)); |
||
145 | $return_array[$key] = $array_item; |
||
146 | |||
147 | |||
148 | if ($parent_key && empty($array_item[0])) { |
||
149 | $return_array[$key] = $array_item + ['foreign_key' => $parent_key]; |
||
150 | } |
||
151 | |||
152 | if (!is_numeric($key)) { |
||
153 | $this->foreign_keys[JsonExtractor::snakeCase($table_name)] = [ |
||
154 | 'ref' => $parent_table, |
||
155 | 'name' => JsonExtractor::snakeCase('foreign_key') |
||
156 | ]; |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | return $return_array; |
||
162 | } |
||
164 |