| Conditions | 34 |
| Paths | 18242 |
| Total Lines | 136 |
| Code Lines | 79 |
| 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 |
||
| 215 | public function doSaveCreateWiz($is_materialized = false) |
||
| 216 | { |
||
| 217 | $data = $this->misc->getDatabaseAccessor(); |
||
| 218 | |||
| 219 | // Check that they've given a name and fields they want to select |
||
| 220 | |||
| 221 | if (!strlen($_POST['formView'])) { |
||
| 222 | return $this->doSetParamsCreate($this->lang['strviewneedsname']); |
||
| 223 | } |
||
| 224 | if (!isset($_POST['formFields']) || !count($_POST['formFields'])) { |
||
| 225 | return $this->doSetParamsCreate($this->lang['strviewneedsfields']); |
||
| 226 | } |
||
| 227 | $selFields = ''; |
||
| 228 | |||
| 229 | if (!empty($_POST['dblFldMeth'])) { |
||
| 230 | $tmpHsh = []; |
||
| 231 | } |
||
| 232 | |||
| 233 | foreach ($_POST['formFields'] as $curField) { |
||
| 234 | $arrTmp = unserialize($curField); |
||
| 235 | $data->fieldArrayClean($arrTmp); |
||
| 236 | $field_arr = [$arrTmp['schemaname'], $arrTmp['tablename'], $arrTmp['fieldname']]; |
||
| 237 | |||
| 238 | $field_element = '"'.implode('"."', $field_arr).'"'; |
||
| 239 | if (empty($_POST['dblFldMeth'])) { |
||
| 240 | // no doublon control |
||
| 241 | $selFields .= $field_element.', '; |
||
| 242 | // doublon control |
||
| 243 | } elseif (empty($tmpHsh[$arrTmp['fieldname']])) { |
||
| 244 | // field does not exist |
||
| 245 | $selFields .= $field_element.', '; |
||
| 246 | $tmpHsh[$arrTmp['fieldname']] = 1; |
||
| 247 | } elseif ('rename' == $_POST['dblFldMeth']) { |
||
| 248 | // field exist and must be renamed |
||
| 249 | ++$tmpHsh[$arrTmp['fieldname']]; |
||
| 250 | $selFields .= $field_element.' AS "'.implode('_', $field_arr).'_'.$tmpHsh[$arrTmp['fieldname']].'", '; |
||
| 251 | } // field already exist, just ignore this one |
||
| 252 | } |
||
| 253 | |||
| 254 | $selFields = substr($selFields, 0, -2); |
||
| 255 | unset($arrTmp, $tmpHsh); |
||
| 256 | $linkFields = ''; |
||
| 257 | $count = 0; |
||
| 258 | |||
| 259 | // If we have links, out put the JOIN ... ON statements |
||
| 260 | if (is_array($_POST['formLink'])) { |
||
| 261 | // Filter out invalid/blank entries for our links |
||
| 262 | $arrLinks = []; |
||
| 263 | foreach ($_POST['formLink'] as $curLink) { |
||
| 264 | if (strlen($curLink['leftlink']) && strlen($curLink['rightlink']) && strlen($curLink['operator'])) { |
||
| 265 | $arrLinks[] = $curLink; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | // We must perform some magic to make sure that we have a valid join order |
||
| 269 | $count = sizeof($arrLinks); |
||
| 270 | $arrJoined = []; |
||
| 271 | $arrUsedTbls = []; |
||
| 272 | } |
||
| 273 | // If we have at least one join condition, output it |
||
| 274 | |||
| 275 | $j = 0; |
||
| 276 | $this->prtrace('arrLinks ', $arrLinks); |
||
|
1 ignored issue
–
show
|
|||
| 277 | while ($j < $count) { |
||
| 278 | foreach ($arrLinks as $curLink) { |
||
| 279 | $arrLeftLink = unserialize($curLink['leftlink']); |
||
| 280 | $arrRightLink = unserialize($curLink['rightlink']); |
||
| 281 | $data->fieldArrayClean($arrLeftLink); |
||
| 282 | $data->fieldArrayClean($arrRightLink); |
||
| 283 | |||
| 284 | $tbl1 = "\"{$arrLeftLink['schemaname']}\".\"{$arrLeftLink['tablename']}\""; |
||
| 285 | $tbl2 = "\"{$arrRightLink['schemaname']}\".\"{$arrRightLink['tablename']}\""; |
||
| 286 | |||
| 287 | if (!((!in_array($curLink, $arrJoined, true) && in_array($tbl1, $arrUsedTbls, true)) || !count($arrJoined))) { |
||
| 288 | continue; |
||
| 289 | } |
||
| 290 | // Make sure for multi-column foreign keys that we use a table alias tables joined to more than once |
||
| 291 | // This can (and should be) more optimized for multi-column foreign keys |
||
| 292 | $adj_tbl2 = in_array($tbl2, $arrUsedTbls, true) ? "${tbl2} AS alias_ppa_".time() : $tbl2; |
||
| 293 | |||
| 294 | $clause1 = "{$curLink['operator']} ${adj_tbl2} ON ({$tbl1}.\"{$arrLeftLink['fieldname']}\" = {$tbl2}.\"{$arrRightLink['fieldname']}\") "; |
||
| 295 | $clause2 = "${tbl1} {$curLink['operator']} ${adj_tbl2} ON ({$tbl1}.\"{$arrLeftLink['fieldname']}\" = {$tbl2}.\"{$arrRightLink['fieldname']}\") "; |
||
| 296 | |||
| 297 | $linkFields .= strlen($linkFields) ? $clause1 : $clause2; |
||
| 298 | |||
| 299 | $arrJoined[] = $curLink; |
||
| 300 | if (!in_array($tbl1, $arrUsedTbls, true)) { |
||
| 301 | $arrUsedTbls[] = $tbl1; |
||
| 302 | } |
||
| 303 | |||
| 304 | if (!in_array($tbl2, $arrUsedTbls, true)) { |
||
| 305 | $arrUsedTbls[] = $tbl2; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | ++$j; |
||
| 309 | } |
||
| 310 | |||
| 311 | //if linkFields has no length then either _POST['formLink'] was not set, or there were no join conditions |
||
| 312 | //just select from all seleted tables - a cartesian join do a |
||
| 313 | if (!strlen($linkFields)) { |
||
| 314 | foreach ($_POST['formTables'] as $curTable) { |
||
| 315 | $arrTmp = unserialize($curTable); |
||
| 316 | $data->fieldArrayClean($arrTmp); |
||
| 317 | $linkFields .= (strlen($linkFields) ? ', ' : ' ')."\"{$arrTmp['schemaname']}\".\"{$arrTmp['tablename']}\""; |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | $addConditions = ''; |
||
| 322 | if (is_array($_POST['formCondition'])) { |
||
| 323 | foreach ($_POST['formCondition'] as $curCondition) { |
||
| 324 | if (strlen($curCondition['field']) && strlen($curCondition['txt'])) { |
||
| 325 | $arrTmp = unserialize($curCondition['field']); |
||
| 326 | $data->fieldArrayClean($arrTmp); |
||
| 327 | $condition = " \"{$arrTmp['schemaname']}\".\"{$arrTmp['tablename']}\".\"{$arrTmp['fieldname']}\" {$curCondition['operator']} '{$curCondition['txt']}' "; |
||
| 328 | $addConditions .= (strlen($addConditions) ? ' AND ' : ' ').$condition; |
||
| 329 | } |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | $viewQuery = "SELECT ${selFields} FROM ${linkFields} "; |
||
| 334 | |||
| 335 | //add where from additional conditions |
||
| 336 | if (strlen($addConditions)) { |
||
| 337 | $viewQuery .= ' WHERE '.$addConditions; |
||
| 338 | } |
||
| 339 | |||
| 340 | try { |
||
| 341 | $status = $data->createView($_POST['formView'], $viewQuery, false, $_POST['formComment'], $is_materialized); |
||
| 342 | if (0 == $status) { |
||
| 343 | $this->misc->setReloadBrowser(true); |
||
| 344 | |||
| 345 | return $this->doDefault($this->lang['strviewcreated']); |
||
|
1 ignored issue
–
show
|
|||
| 346 | } |
||
| 347 | |||
| 348 | return $this->doSetParamsCreate($this->lang['strviewcreatedbad']); |
||
| 349 | } catch (\PHPPgAdmin\ADOdbException $e) { |
||
| 350 | return $this->halt($e->getMessage()); |
||
|
1 ignored issue
–
show
|
|||
| 351 | } |
||
| 354 |