Passed
Pull Request — master (#7)
by Michael
03:59
created

search.inc.php ➔ xoopsfaq_search()   C

Complexity

Conditions 11
Paths 33

Size

Total Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 33
nop 5
dl 0
loc 77
rs 6.3551
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits of
4
 supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit
6
 authors.
7
8
 This program is distributed in the hope that it will be useful, but
9
 WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * XoopsFAQ module
15
 * Description: Search function for XOOPS FAQ Module
16
 *
17
 * @package   module\xoopsfaq\search
18
 * @author    John Neill
19
 * @author    XOOPS Module Development Team
20
 * @copyright Copyright (c) 2001-2017 {@link http://xoops.org XOOPS Project}
21
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
22
 *
23
 * @see       \XoopsModules\Xoopsfaq\Helper
24
 */
25
26
use XoopsModules\Xoopsfaq;
27
28
defined('XOOPS_ROOT_PATH') || die('Restricted access');
29
30
/**
31
 * xoopsfaq_search()
32
 *
33
 * @param mixed $queryarray
34
 * @param mixed $andor
35
 * @param mixed $limit
36
 * @param mixed $offset
37
 * @param mixed $userid
38
 *
39
 * @return array results of search
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