| Conditions | 21 |
| Paths | 9216 |
| Total Lines | 109 |
| Code Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 36 | function mymodule3_search($queryarray, $andor, $limit, $offset, $userid) |
||
| 37 | { |
||
| 38 | $ret = []; |
||
| 39 | $helper = \XoopsModules\Mymodule3\Helper::getInstance(); |
||
| 40 | |||
| 41 | // search in table articles |
||
| 42 | // search keywords |
||
| 43 | $elementCount = 0; |
||
| 44 | $articlesHandler = $helper->getHandler('articles'); |
||
| 45 | if (is_array($queryarray)) { |
||
| 46 | $elementCount = count($queryarray); |
||
| 47 | } |
||
| 48 | if ($elementCount > 0) { |
||
| 49 | $criteriaKeywords = new \CriteriaCompo(); |
||
| 50 | for($i = 0; $i < $elementCount; $i++) { |
||
| 51 | $criteriaKeyword = new \CriteriaCompo(); |
||
| 52 | $criteriaKeyword->add( new \Criteria( 'art_cat', '%' . $queryarray[$i] . '%', 'LIKE' ), 'OR' ); |
||
| 53 | $criteriaKeyword->add( new \Criteria( 'art_title', '%' . $queryarray[$i] . '%', 'LIKE' ), 'OR' ); |
||
| 54 | $criteriaKeyword->add( new \Criteria( 'art_descr', '%' . $queryarray[$i] . '%', 'LIKE' ), 'OR' ); |
||
| 55 | $criteriaKeywords->add( $criteriaKeyword, $andor ); |
||
| 56 | unset($criteriaKeyword); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | // search user(s) |
||
| 60 | if ($userid && is_array($userid)) { |
||
| 61 | $userid = array_map('intval', $userid); |
||
| 62 | $criteriaUser = new \CriteriaCompo(); |
||
| 63 | $criteriaUser->add( new \Criteria( 'art_submitter', '(' . implode(',', $userid) . ')', 'IN' ), 'OR' ); |
||
| 64 | } elseif (is_numeric($userid) && $userid > 0) { |
||
| 65 | $criteriaUser = new \CriteriaCompo(); |
||
| 66 | $criteriaUser->add( new \Criteria( 'art_submitter', $userid ), 'OR' ); |
||
| 67 | } |
||
| 68 | $criteriaSearch = new \CriteriaCompo(); |
||
| 69 | if (isset($criteriaKeywords)) { |
||
| 70 | $criteriaSearch->add( $criteriaKeywords, 'AND' ); |
||
| 71 | } |
||
| 72 | if (isset($criteriaUser)) { |
||
| 73 | $criteriaSearch->add( $criteriaUser, 'AND' ); |
||
| 74 | } |
||
| 75 | $criteriaSearch->setStart( $offset ); |
||
| 76 | $criteriaSearch->setLimit( $limit ); |
||
| 77 | $criteriaSearch->setSort( 'art_created' ); |
||
| 78 | $criteriaSearch->setOrder( 'DESC' ); |
||
| 79 | $articlesAll = $articlesHandler->getAll($criteriaSearch); |
||
|
|
|||
| 80 | foreach(array_keys($articlesAll) as $i) { |
||
| 81 | $ret[] = [ |
||
| 82 | 'image' => 'assets/icons/16/articles.png', |
||
| 83 | 'link' => 'articles.php?op=show&art_id=' . $articlesAll[$i]->getVar('art_id'), |
||
| 84 | 'title' => $articlesAll[$i]->getVar('art_title'), |
||
| 85 | 'time' => $articlesAll[$i]->getVar('art_created') |
||
| 86 | ]; |
||
| 87 | } |
||
| 88 | unset($criteriaKeywords); |
||
| 89 | unset($criteriaKeyword); |
||
| 90 | unset($criteriaUser); |
||
| 91 | unset($criteriaSearch); |
||
| 92 | |||
| 93 | // search in table testfields |
||
| 94 | // search keywords |
||
| 95 | $elementCount = 0; |
||
| 96 | $testfieldsHandler = $helper->getHandler('testfields'); |
||
| 97 | if (is_array($queryarray)) { |
||
| 98 | $elementCount = count($queryarray); |
||
| 99 | } |
||
| 100 | if ($elementCount > 0) { |
||
| 101 | $criteriaKeywords = new \CriteriaCompo(); |
||
| 102 | for($i = 0; $i < $elementCount; $i++) { |
||
| 103 | $criteriaKeyword = new \CriteriaCompo(); |
||
| 104 | $criteriaKeyword->add( new \Criteria( 'tf_text', '%' . $queryarray[$i] . '%', 'LIKE' ), 'OR' ); |
||
| 105 | $criteriaKeyword->add( new \Criteria( 'tf_textarea', '%' . $queryarray[$i] . '%', 'LIKE' ), 'OR' ); |
||
| 106 | $criteriaKeywords->add( $criteriaKeyword, $andor ); |
||
| 107 | unset($criteriaKeyword); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | // search user(s) |
||
| 111 | if ($userid && is_array($userid)) { |
||
| 112 | $userid = array_map('intval', $userid); |
||
| 113 | $criteriaUser = new \CriteriaCompo(); |
||
| 114 | $criteriaUser->add( new \Criteria( 'tf_submitter', '(' . implode(',', $userid) . ')', 'IN' ), 'OR' ); |
||
| 115 | } elseif (is_numeric($userid) && $userid > 0) { |
||
| 116 | $criteriaUser = new \CriteriaCompo(); |
||
| 117 | $criteriaUser->add( new \Criteria( 'tf_submitter', $userid ), 'OR' ); |
||
| 118 | } |
||
| 119 | $criteriaSearch = new \CriteriaCompo(); |
||
| 120 | if (isset($criteriaKeywords)) { |
||
| 121 | $criteriaSearch->add( $criteriaKeywords, 'AND' ); |
||
| 122 | } |
||
| 123 | if (isset($criteriaUser)) { |
||
| 124 | $criteriaSearch->add( $criteriaUser, 'AND' ); |
||
| 125 | } |
||
| 126 | $criteriaSearch->setStart( $offset ); |
||
| 127 | $criteriaSearch->setLimit( $limit ); |
||
| 128 | $criteriaSearch->setSort( 'tf_datetime' ); |
||
| 129 | $criteriaSearch->setOrder( 'DESC' ); |
||
| 130 | $testfieldsAll = $testfieldsHandler->getAll($criteriaSearch); |
||
| 131 | foreach(array_keys($testfieldsAll) as $i) { |
||
| 132 | $ret[] = [ |
||
| 133 | 'image' => 'assets/icons/16/testfields.png', |
||
| 134 | 'link' => 'testfields.php?op=show&tf_id=' . $testfieldsAll[$i]->getVar('tf_id'), |
||
| 135 | 'title' => $testfieldsAll[$i]->getVar('tf_text'), |
||
| 136 | 'time' => $testfieldsAll[$i]->getVar('tf_datetime') |
||
| 137 | ]; |
||
| 138 | } |
||
| 139 | unset($criteriaKeywords); |
||
| 140 | unset($criteriaKeyword); |
||
| 141 | unset($criteriaUser); |
||
| 142 | unset($criteriaSearch); |
||
| 143 | |||
| 144 | return $ret; |
||
| 145 | |||
| 147 |