| Conditions | 14 | 
| Paths | 192 | 
| Total Lines | 75 | 
| 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  | 
            ||
| 38 | function smallworld_search($queryarray, $andor, $limit, $offset, $userid, $sortby = 'created DESC')  | 
            ||
| 39 | { | 
            ||
| 40 | $moduleDirName = basename(dirname(__DIR__));  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 41 | |||
| 42 |     if (file_exists(XOOPS_ROOT_PATH . '/modules/smallworld/language/' . $GLOBALS['xoopsConfig']['language'] . '/main.php')) { | 
            ||
| 43 | require_once XOOPS_ROOT_PATH . '/modules/smallworld/language/' . $GLOBALS['xoopsConfig']['language'] . '/main.php';  | 
            ||
| 44 |     } else { | 
            ||
| 45 | require_once XOOPS_ROOT_PATH . '/modules/smallworld/language/english/main.php';  | 
            ||
| 46 | }  | 
            ||
| 47 | |||
| 48 |     $moduleHandler = xoops_getHandler('module'); | 
            ||
| 49 |     $module        = $moduleHandler->getByDirname('smallworld'); | 
            ||
| 50 |     $modid         = $module->getVar('mid'); | 
            ||
| 51 | $searchparam = '';  | 
            ||
| 52 | $highlight = false;  | 
            ||
| 53 | |||
| 54 |     $gpermHandler = xoops_getHandler('groupperm'); | 
            ||
| 55 |     if ($GLOBALS['xoopsUser'] && ($GLOBALS['xoopsUser'] instanceof \XoopsUser)) { | 
            ||
| 56 | $groups = $GLOBALS['xoopsUser']->getGroups();  | 
            ||
| 57 |         $id        = $GLOBALS['xoopsUser']->getVar('uid'); | 
            ||
| 58 | $wall = new Smallworld\WallUpdates();  | 
            ||
| 59 | $followers = smallworld_array_flatten($wall->getFollowers($id), 0);  | 
            ||
| 60 |     } else { | 
            ||
| 61 | $id = 0;  | 
            ||
| 62 | $groups = XOOPS_GROUP_ANONYMOUS;  | 
            ||
| 63 | $followers = [];  | 
            ||
| 64 | }  | 
            ||
| 65 | |||
| 66 | //@todo figure out why this if statement is here both conditions yield the same result  | 
            ||
| 67 |     if ($id > 0 && '' != $id) { | 
            ||
| 68 |         $sql = 'SELECT M.msg_id, M.uid_fk, M.message, M.created, M.priv, U.username FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . ' M, ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . ' U WHERE M.uid_fk=U.userid'; | 
            ||
| 69 |     } else { | 
            ||
| 70 |         $sql = 'SELECT M.msg_id, M.uid_fk, M.message, M.created, M.priv, U.username FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . ' M, ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . ' U WHERE M.uid_fk=U.userid'; | 
            ||
| 71 | }  | 
            ||
| 72 | |||
| 73 |     if (0 != (int)$userid) { | 
            ||
| 74 | $sql .= ' AND M.uid_fk = ' . $userid . ' ';  | 
            ||
| 75 | }  | 
            ||
| 76 | //@todo this needs to be refactored to address when $queryarray has more than 1 element  | 
            ||
| 77 |     if (is_array($queryarray) && $count = count($queryarray)) { | 
            ||
| 78 | $sql .= " AND (M.message LIKE '%$queryarray[0]%' OR M.message LIKE '%$queryarray[0]%' OR U.username LIKE '%$queryarray[0]%'";  | 
            ||
| 79 | $sql .= ') ';  | 
            ||
| 80 | // keywords highlighting  | 
            ||
| 81 |         if ($highlight) { | 
            ||
| 82 |             $searchparam = '&keywords=' . urlencode(trim(implode(' ', $queryarray))); | 
            ||
| 83 | }  | 
            ||
| 84 | }  | 
            ||
| 85 | $sql .= 'ORDER BY created DESC';  | 
            ||
| 86 | $result = $GLOBALS['xoopsDB']->query($sql, $limit, $offset);  | 
            ||
| 87 | $ret = [];  | 
            ||
| 88 | $i = 0;  | 
            ||
| 89 |     while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) { | 
            ||
| 90 |         if (in_array($myrow['uid_fk'], $followers) || $myrow['uid_fk'] == $id) { | 
            ||
| 91 | $ret[$i]['image'] = 'assets/images/smallworld_icn.png';  | 
            ||
| 92 | $ret[$i]['link'] = 'permalink.php?ownerid=' . $myrow['uid_fk'] . '&updid=' . $myrow['msg_id'];  | 
            ||
| 93 |             if (preg_match('/UPLIMAGE/', $myrow['message'])) { | 
            ||
| 94 | // @todo - figure this out, doesn't look right - title value is overwrittern so 'message' never gets displayed  | 
            ||
| 95 |                 $ownmsg           = str_replace('UPLIMAGE ', '', $myrow['message']); | 
            ||
| 96 | $ret[$i]['title'] = $ownmsg;  | 
            ||
| 97 |                 $ret[$i]['title'] = Smallworld\Helper::getInstance()->getHandler('SwUser')->getName($myrow['uid_fk']) . ' -> ' . _SMALLWORLD_GALLERY; | 
            ||
| 98 | //$ret[$i]['title'] = smallworld_getName($myrow['uid_fk']) . ' -> ' . _SMALLWORLD_GALLERY;  | 
            ||
| 99 | $ret[$i]['title'] = str_replace(['<', '>'], ['<', '>'], $ret[$i]['title']);  | 
            ||
| 100 |             } else { | 
            ||
| 101 | $ret[$i]['title'] = smallworld_shortenText($myrow['message'], 60);  | 
            ||
| 102 | }  | 
            ||
| 103 | $ret[$i]['time'] = $myrow['created'];  | 
            ||
| 104 | $ret[$i]['uid'] = $myrow['uid_fk'];  | 
            ||
| 105 |         } else { | 
            ||
| 106 | --$i;  | 
            ||
| 107 | }  | 
            ||
| 108 | ++$i;  | 
            ||
| 109 | }  | 
            ||
| 110 | |||
| 111 | return $ret;  | 
            ||
| 112 | }  | 
            ||
| 113 | 
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.