xoopsfaq_search()   C
last analyzed

Complexity

Conditions 11
Paths 33

Size

Total Lines 75
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 51
c 2
b 0
f 0
nc 33
nop 5
dl 0
loc 75
rs 6.9224

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 declare(strict_types=1);
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
 * @author    John Neill
18
 * @author    XOOPS Module Development Team
19
 * @copyright Copyright (c) 2001-2017 {@link https://xoops.org XOOPS Project}
20
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GNU Public License
21
 *
22
 * @see       \XoopsModules\Xoopsfaq\Helper
23
 */
24
25
use XoopsModules\Xoopsfaq\{
26
    Constants,
27
    CategoryHandler,
28
    ContentsHandler,
29
    Helper
30
};
31
32
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
33
34
/**
35
 * xoopsfaq_search()
36
 *
37
 * @param mixed $queryarray
38
 * @param mixed $andor
39
 * @param mixed $limit
40
 * @param mixed $offset
41
 * @param mixed $userid
42
 *
43
 * @return array results of search
44
 *
45
 * @see \XoopsModules\Xoopsfaq\Helper
46
 */
47
function xoopsfaq_search($queryarray, $andor, $limit, $offset, $userid)
48
{
49
    $ret = [];
50
    if (0 != $userid) {
51
        return $ret;
52
    }
53
54
    /** @var CategoryHandler $categoryHandler */
55
    /** @var ContentsHandler $contentsHandler */
56
    /** @var Helper $helper */
57
    $helper = Helper::getInstance();
58
59
    // Find the search term in the Category
60
    // Find the search term in the FAQ
61
    $categoryHandler = $helper->getHandler('Category');
62
    $contentFields   = ['category_id', 'category_title'];
63
    $criteria        = new \CriteriaCompo();
64
    $criteria->setSort('category_title');
65
    $criteria->order = 'ASC';
66
    $criteria->setLimit((int)$limit);
67
    $criteria->setStart((int)$offset);
68
69
    $queryarrayHold = $queryarray;
70
    if ((is_array($queryarray)) && !empty($queryarray)) {
71
        $criteria->add(new \Criteria('category_title', '%' . $queryarray[0] . '%', 'LIKE'));
72
        array_shift($queryarray); //get rid of first element
73
        foreach ($queryarray as $query) {
74
            $criteria->add(new \Criteria('category_title', '%' . $query . '%', 'LIKE'), $andor);
75
        }
76
    }
77
    $catArray   = $categoryHandler->getAll($criteria, $contentFields, false);
78
    $catCount   = !empty($catArray) ? count($catArray) : 0;
79
    $totalLimit = (int)$limit - $catCount;
80
    foreach ($catArray as $cId => $cat) {
81
        $ret[] = [
82
            'image' => 'assets/images/folder.png',
83
            'link'  => $helper->url('index.php?cat_id=' . $cId),
84
            'title' => $cat['category_title'],
85
        ];
86
    }
87
    unset($catArray);
88
89
    // Find the search term in the FAQ
90
    $queryarray      = $queryarrayHold;
91
    $contentsHandler = $helper->getHandler('Contents');
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