Conditions | 13 |
Paths | 1152 |
Total Lines | 106 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | 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 |
||
304 | public function completeCallback() |
||
305 | { |
||
306 | |||
307 | //can this field be searched |
||
308 | $search = $this->getValue('hassearch'); |
||
309 | if ($search === 'hassearch') { |
||
310 | $search = '1'; |
||
311 | $searchname = $this->getValue('searchname'); |
||
312 | $searchexplain = $this->getValue('searchexplain'); |
||
313 | } else { |
||
314 | $search = '0'; |
||
315 | $searchname = ''; |
||
316 | $searchexplain = ''; |
||
317 | } |
||
318 | //show in pedigree |
||
319 | $viewinpedigree = $this->getValue('viewinpedigree'); |
||
320 | if ($viewinpedigree === 'viewinpedigree') { |
||
321 | $viewinpedigree = '1'; |
||
322 | } else { |
||
323 | $viewinpedigree = '0'; |
||
324 | } |
||
325 | //show in advanced |
||
326 | $viewinadvanced = $this->getValue('viewinadvanced'); |
||
327 | if ($viewinadvanced === 'viewinadvanced') { |
||
328 | $viewinadvanced = '1'; |
||
329 | } else { |
||
330 | $viewinadvanced = '0'; |
||
331 | } |
||
332 | //show in pie |
||
333 | $viewinpie = $this->getValue('viewinpie'); |
||
334 | if ($viewinpie === 'viewinpie') { |
||
335 | $viewinpie = '1'; |
||
336 | } else { |
||
337 | $viewinpie = '0'; |
||
338 | } |
||
339 | //view in list |
||
340 | $viewinlist = $this->getValue('viewinlist'); |
||
341 | if ($viewinlist === 'viewinlist') { |
||
342 | $viewinlist = '1'; |
||
343 | } else { |
||
344 | $viewinlist = '0'; |
||
345 | } |
||
346 | //add a litter |
||
347 | $Litter = $this->getValue('Litter'); |
||
348 | if ($Litter === 'Litter') { |
||
349 | $Litter = '1'; |
||
350 | } else { |
||
351 | $Litter = '0'; |
||
352 | } |
||
353 | //general litter |
||
354 | $Generallitter = $this->getValue('Generallitter'); |
||
355 | if ($Generallitter === 'Generallitter') { |
||
356 | $Generallitter = '1'; |
||
357 | } else { |
||
358 | $Generallitter = '0'; |
||
359 | } |
||
360 | |||
361 | if (!$this->getValue('field') == 0) { |
||
362 | // field allready exists (editing mode) |
||
363 | |||
364 | $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " SET fieldName = '" . htmlspecialchars($this->getValue('name')) . "', fieldType = '" . $this->getValue('fieldType') . "', defaultValue = '" . $this->getValue('defaultValue') . "', fieldExplanation = '" . $this->getValue('explain') . "', hasSearch = '" . $search . "', Litter = '" . $litter . "', generallitter = '" . $generallitter . "', searchName = '" . $searchname . "', searchExplanation = '" . $searchexplain . "', viewInPedigree = '" . $viewinpedigree . "', viewInAdvanced = '" . $viewinadvanced . "', viewInPie = '" . $viewinpie . "', viewInList = '" . $viewinlist . "' WHERE ID ='" . $this->getValue('field') . "'"; |
||
365 | $GLOBALS['xoopsDB']->queryF($sql); |
||
366 | //possible change defaultvalue for userfield |
||
367 | $sql = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' CHANGE `user' . $this->getValue('field') . '` `user' . $this->getValue('field') . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'"; |
||
368 | $GLOBALS['xoopsDB']->queryF($sql); |
||
369 | $sql = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . ' CHANGE `user' . $this->getValue('field') . '` `user' . $this->getValue('field') . "` VARCHAR( 1024 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'"; |
||
370 | $GLOBALS['xoopsDB']->queryF($sql); |
||
371 | $sql = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_trash') . ' CHANGE `user' . $this->getValue('field') . '` `user' . $this->getValue('field') . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'"; |
||
372 | $GLOBALS['xoopsDB']->queryF($sql); |
||
373 | } else { //this is a new field |
||
374 | $sql = 'SELECT MAX(ID) AS lid from ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' LIMIT 1'; |
||
375 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
376 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
377 | $nextfieldnum = $row['lid'] + 1; |
||
378 | } |
||
379 | //add userfield to various tables as a new field. |
||
380 | //allways add at the end of the table |
||
381 | $tables = array('pedigree_tree', 'pedigree_temp', 'pedigree_trash'); |
||
382 | foreach ($tables as $table) { |
||
383 | $SQL = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix($table) . ' ADD `user' . $nextfieldnum . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'"; |
||
384 | $GLOBALS['xoopsDB']->queryF($SQL); |
||
385 | } |
||
386 | //is a lookup table present |
||
387 | $lookup = $this->getValue('lookup1'); |
||
388 | if ($lookup == '') { |
||
389 | $lookup = '0'; |
||
390 | } else { |
||
391 | $lookup = '1'; |
||
392 | //create table for lookupfield |
||
393 | $createtable = 'CREATE TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $nextfieldnum) . ' (`ID` INT( 10 ) NOT NULL ,`value` VARCHAR( 255 ) NOT NULL, `order` INT( 10 )) ENGINE = MyISAM'; |
||
394 | $GLOBALS['xoopsDB']->queryF($createtable); |
||
395 | //fill table |
||
396 | $count = $this->getValue('fc'); |
||
397 | for ($x = 1; $x < $count + 1; ++$x) { |
||
398 | $y = $x - 1; |
||
399 | $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $nextfieldnum) . " ( `ID` , `value`, `order`) VALUES ('" . $y . "', '" . $this->getValue('lookup' . $x) . "','" . $y . "')"; |
||
400 | $GLOBALS['xoopsDB']->queryF($sql); |
||
401 | } |
||
402 | } |
||
403 | |||
404 | //Insert new record into pedigree_config |
||
405 | // $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " VALUES ('" . $nextfieldnum . "', '1', '" . htmlspecialchars($this->getValue('name')) . "', '" . $this->getValue('fieldtype') . "', '" . $lookup . "', '" . $this->getValue('defaultvalue') . "', '" . $this->getValue('explain') . "', '" . $search . "', '" . $Litter . "', '" . $Generallitter . "', '" . $searchname . "', '" . $searchexplain . "', '" . $viewinpedigree . "', '" . $viewinadvanced . "', '" . $viewinpie . "', '" . $viewinlist . "','','" . $nextfieldnum . "')"; |
||
406 | $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " VALUES ('" . $nextfieldnum . "', '1', '" . $GLOBALS['xoopsDB']->escape(htmlSpecialChars($this->getValue('name'))) . "', '" . $GLOBALS['xoopsDB']->escape($this->getValue('fieldtype')) . "', '" . $GLOBALS['xoopsDB']->escape($lookup) . "', '" . $GLOBALS['xoopsDB']->escape($this->getValue('defaultvalue')) . "', '" . $GLOBALS['xoopsDB']->escape($this->getValue('explain')) . "', '" . $GLOBALS['xoopsDB']->escape($search) . "', '" . $GLOBALS['xoopsDB']->escape($Litter) . "', '" . $GLOBALS['xoopsDB']->escape($Generallitter) . "', '" . $GLOBALS['xoopsDB']->escape($searchname) . "', '" . $GLOBALS['xoopsDB']->escape($searchexplain) . "', '" . $GLOBALS['xoopsDB']->escape($viewinpedigree) . "', '" . $GLOBALS['xoopsDB']->escape($viewinadvanced) . "', '" . $GLOBALS['xoopsDB']->escape($viewinpie) . "', '" . $GLOBALS['xoopsDB']->escape($viewinlist) . "','','" . $GLOBALS['xoopsDB']->escape($nextfieldnum) . "')"; |
||
407 | $GLOBALS['xoopsDB']->queryF($sql); |
||
408 | } |
||
409 | } |
||
410 | |||
424 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.