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