Conditions | 24 |
Paths | 840 |
Total Lines | 129 |
Code Lines | 82 |
Lines | 8 |
Ratio | 6.2 % |
Changes | 1 | ||
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 |
||
12 | function lx_search( $queryarray, $andor, $limit, $offset, $userid ) { |
||
13 | |||
14 | global $xoopsDB, $xoopsUser; |
||
15 | // -- search comments + highlighter |
||
16 | $highlight = false; |
||
17 | $searchincomments = false ; |
||
18 | include_once XOOPS_ROOT_PATH.'/modules/lexikon/include/common.inc.php'; |
||
19 | include_once XOOPS_ROOT_PATH."/modules/lexikon/include/functions.php"; |
||
20 | $highlight = lx_getmoduleoption('config_highlighter'); |
||
21 | //if ( is_object($xoopsUser) ) { |
||
22 | $searchincomments = CONFIG_SEARCH_COMMENTS; |
||
23 | // } else { |
||
24 | // $searchincomments = false ; |
||
25 | // } |
||
26 | $module_handler =& xoops_gethandler('module'); |
||
27 | $module =& $module_handler->getByDirname('lexikon'); |
||
28 | $module_id = $module->getVar('mid'); |
||
29 | // Permissions |
||
30 | $gperm_handler =& xoops_gethandler('groupperm'); |
||
31 | $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS; |
||
32 | $allowed_cats = $gperm_handler->getItemIds("lexikon_view", $groups, $module_id); |
||
33 | $catids= implode(',', $allowed_cats); |
||
34 | |||
35 | $sql = "SELECT entryID, categoryID, term, definition, ref, uid, datesub FROM " . $xoopsDB -> prefix( "lxentries" ) . " WHERE submit = 0 AND offline = 0 "; |
||
36 | $sql .= " AND categoryID IN ($catids) "; |
||
37 | |||
38 | if ( $userid != 0 ) { |
||
39 | $sql .= " AND uid=".$userid." "; |
||
40 | } |
||
41 | if ($highlight) { |
||
42 | if ($queryarray == ''){ |
||
43 | $keywords= ''; |
||
44 | $hightlight_key = ''; |
||
45 | } else { |
||
46 | $keywords=implode('+', $queryarray); |
||
47 | //$keywords='&keywords='.urlencode(trim(implode(' ',$queryarray))); |
||
48 | $hightlight_key = "&keywords=" . $keywords; |
||
49 | } |
||
50 | } |
||
51 | // because count() returns 1 even if a supplied variable |
||
52 | // is not an array, we must check if $querryarray is really an array |
||
53 | $count = count( $queryarray ); |
||
54 | if ( $count > 0 && is_array( $queryarray ) ) { |
||
55 | $sql .= "AND ((term LIKE '%$queryarray[0]%' OR definition LIKE '%$queryarray[0]%' OR ref LIKE '%$queryarray[0]%')"; |
||
56 | View Code Duplication | for ( $i = 1; $i < $count; $i++ ) { |
|
57 | $sql .= " $andor "; |
||
58 | $sql .= "(term LIKE '%$queryarray[$i]%' OR definition LIKE '%$queryarray[$i]%' OR ref LIKE '%$queryarray[$i]%')"; |
||
59 | } |
||
60 | $sql .= ") "; |
||
61 | |||
62 | } |
||
63 | $sql .= "ORDER BY entryID DESC"; |
||
64 | $result = $xoopsDB -> query( $sql, $limit, $offset ); |
||
65 | $ret = array(); |
||
66 | $i = 0; |
||
67 | |||
68 | while ( $myrow = $xoopsDB -> fetchArray( $result ) ) { |
||
69 | $display = true; |
||
70 | if($module_id && $gperm_handler) { |
||
71 | if (!$gperm_handler->checkRight("lexikon_view", $myrow['categoryID'], $groups, $module_id)) { |
||
72 | //if (!$gperm_handler->checkRight("lexikon_view", $categoryID, $groups, $module_id)) { |
||
73 | $display = false; |
||
74 | } |
||
75 | } |
||
76 | if ($display) { |
||
77 | $ret[$i]['image'] = "images/lx.png"; |
||
78 | $ret[$i]['link'] = "entry.php?entryID=" . $myrow['entryID'] . $hightlight_key; |
||
79 | $ret[$i]['title'] = $myrow['term']; |
||
80 | $ret[$i]['time'] = $myrow['datesub']; |
||
81 | $ret[$i]['uid'] = $myrow['uid']; |
||
82 | $i++; |
||
83 | } |
||
84 | } |
||
85 | //return $ret; |
||
86 | //} |
||
87 | // --- comments search --- |
||
88 | if ($searchincomments && (isset($limit) && $i<=$limit)) { |
||
89 | include_once XOOPS_ROOT_PATH.'/include/comment_constants.php'; |
||
90 | $ind = $i; |
||
91 | $sql = "SELECT com_id, com_modid, com_itemid, com_created, com_uid, com_title, com_text, com_status |
||
92 | FROM ".$xoopsDB->prefix("xoopscomments")." |
||
93 | WHERE (com_id>0) AND (com_modid=$module_id) AND (com_status=".XOOPS_COMMENT_ACTIVE.") "; |
||
94 | if ( $userid != 0 ) { |
||
95 | $sql .= " AND com_uid=".$userid." "; |
||
96 | } |
||
97 | |||
98 | if ( is_array($queryarray) && $count = count($queryarray) ) { |
||
99 | $sql .= " AND ((com_title LIKE '%$queryarray[0]%' OR com_text LIKE '%$queryarray[0]%')"; |
||
100 | View Code Duplication | for ($i=1;$i<$count;$i++) { |
|
101 | $sql .= " $andor "; |
||
102 | $sql .= "(com_title LIKE '%$queryarray[$i]%' OR com_text LIKE '%$queryarray[$i]%')"; |
||
103 | } |
||
104 | $sql .= ") "; |
||
105 | } |
||
106 | $i=$ind; |
||
107 | $sql .= "ORDER BY com_created DESC"; |
||
108 | $result = $xoopsDB->query( $sql, $limit, $offset ); |
||
109 | while ($myrow = $xoopsDB->fetchArray($result)) { |
||
110 | $display=true; |
||
111 | //permission |
||
112 | /*if($module_id && $gperm_handler) { |
||
113 | if (!$gperm_handler->checkRight("lexikon_view", $myrow['com_itemid'], $groups, $module_id)) { |
||
114 | //if (!$gperm_handler->checkRight("lexikon_view", $myrow['categoryID'], $groups, $xoopsModule -> getVar('mid'))) { |
||
115 | $display = false; |
||
116 | } |
||
117 | }*/ |
||
118 | list( $entryID, $offline ) = $xoopsDB->fetchRow($xoopsDB->query(" |
||
119 | SELECT entryID, offline |
||
120 | FROM ".$xoopsDB->prefix("lxentries")." WHERE entryID = ".$myrow['com_itemid']."")); |
||
121 | if ($offline == 1) { |
||
122 | $display = false; |
||
123 | } |
||
124 | if ($i+1>$limit) { |
||
125 | $display = false; |
||
126 | } |
||
127 | |||
128 | if ($display) { |
||
129 | $ret[$i]['image'] = "images/lx.png"; |
||
130 | $ret[$i]['link'] = "entry.php?entryID=".$myrow['com_itemid'] . $hightlight_key; |
||
131 | $ret[$i]['title'] = $myrow['com_title']; |
||
132 | $ret[$i]['time'] = $myrow['com_created']; |
||
133 | $ret[$i]['uid'] = $myrow['com_uid']; |
||
134 | $i++; |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | return $ret; |
||
140 | } |
||
141 |
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.