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