| Conditions | 11 |
| Paths | 33 |
| Total Lines | 75 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 declare(strict_types=1); |
||
| 47 | function xoopsfaq_search($queryarray, $andor, $limit, $offset, $userid): array |
||
| 48 | {
|
||
| 49 | $ret = []; |
||
| 50 | if (0 != $userid) {
|
||
| 51 | return $ret; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** @var Helper $helper */ |
||
| 55 | $helper = Helper::getInstance(); |
||
| 56 | |||
| 57 | // Find the search term in the Category |
||
| 58 | // Find the search term in the FAQ |
||
| 59 | /** @var CategoryHandler $categoryHandler */ |
||
| 60 | $categoryHandler = $helper->getHandler('Category');
|
||
| 61 | /** @var ContentsHandler $contentsHandler */ |
||
| 62 | $contentsHandler = $helper->getHandler('Category');
|
||
| 63 | $contentFields = ['category_id', 'category_title']; |
||
| 64 | $criteria = new \CriteriaCompo(); |
||
| 65 | $criteria->setSort('category_title');
|
||
| 66 | $criteria->order = 'ASC'; |
||
| 67 | $criteria->setLimit((int)$limit); |
||
| 68 | $criteria->setStart((int)$offset); |
||
| 69 | |||
| 70 | $queryarrayHold = $queryarray; |
||
| 71 | if ((is_array($queryarray)) && !empty($queryarray)) {
|
||
| 72 | $criteria->add(new \Criteria('category_title', '%' . $queryarray[0] . '%', 'LIKE'));
|
||
| 73 | array_shift($queryarray); //get rid of first element |
||
| 74 | foreach ($queryarray as $query) {
|
||
| 75 | $criteria->add(new \Criteria('category_title', '%' . $query . '%', 'LIKE'), $andor);
|
||
| 76 | } |
||
| 77 | } |
||
| 78 | $catArray = $categoryHandler->getAll($criteria, $contentFields, false); |
||
| 79 | $catCount = !empty($catArray) ? count($catArray) : 0; |
||
| 80 | $totalLimit = (int)$limit - $catCount; |
||
| 81 | foreach ($catArray as $cId => $cat) {
|
||
| 82 | $ret[] = [ |
||
| 83 | 'image' => 'assets/images/folder.png', |
||
| 84 | 'link' => $helper->url('index.php?cat_id=' . $cId),
|
||
| 85 | 'title' => $cat['category_title'], |
||
| 86 | ]; |
||
| 87 | } |
||
| 88 | unset($catArray); |
||
| 89 | |||
| 90 | // Find the search term in the FAQ |
||
| 91 | $queryarray = $queryarrayHold; |
||
| 92 | $contentFields = ['contents_id', 'contents_cid', 'contents_title', 'contents_contents', 'contents_publish']; |
||
| 93 | $criteria = new \CriteriaCompo(); |
||
| 94 | $criteria->add(new \Criteria('contents_active', Constants::ACTIVE, '='));
|
||
| 95 | $criteria->setSort('contents_id');
|
||
| 96 | $criteria->order = 'DESC'; |
||
| 97 | $criteria->setLimit($totalLimit); |
||
| 98 | $criteria->setStart((int)$offset); |
||
| 99 | |||
| 100 | if ((is_array($queryarray)) && !empty($queryarray)) {
|
||
| 101 | $criteria->add(new \Criteria('contents_title', '%' . $queryarray[0] . '%', 'LIKE'));
|
||
| 102 | $criteria->add(new \Criteria('contents_contents', '%' . $queryarray[0] . '%', 'LIKE'), 'OR');
|
||
| 103 | array_shift($queryarray); //get rid of first element |
||
| 104 | |||
| 105 | foreach ($queryarray as $query) {
|
||
| 106 | $criteria->add(new \Criteria('contents_title', '%' . $query . '%', 'LIKE'), $andor);
|
||
| 107 | $criteria->add(new \Criteria('contents_contents', '%' . $query . '%', 'LIKE'), 'OR');
|
||
| 108 | } |
||
| 109 | } |
||
| 110 | $contentArray = $contentsHandler->getAll($criteria, $contentFields, false); |
||
| 111 | foreach ($contentArray as $content) {
|
||
| 112 | $ret[] = [ |
||
| 113 | 'image' => 'assets/images/question2.gif', |
||
| 114 | 'link' => $helper->url('index.php?cat_id=' . $content['contents_cid'] . '#q' . $content['contents_id']),
|
||
| 115 | 'title' => $content['contents_title'], |
||
| 116 | 'time' => $content['contents_publish'], |
||
| 117 | ]; |
||
| 118 | } |
||
| 119 | unset($contentArray); |
||
| 120 | |||
| 121 | return $ret; |
||
| 122 | } |
||
| 123 |