publisher_search()   C
last analyzed

Complexity

Conditions 14
Paths 18

Size

Total Lines 68
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 49
c 1
b 0
f 0
nc 18
nop 9
dl 0
loc 68
rs 6.2666

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php declare(strict_types=1);
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright       XOOPS Project (https://xoops.org)
14
 * @license         https://www.fsf.org/copyleft/gpl.html GNU public license
15
 * @since           1.0
16
 * @author          trabis <[email protected]>
17
 */
18
19
use XoopsModules\Publisher\Helper;
20
use XoopsModules\Publisher\Item;
21
use XoopsModules\Publisher\ItemHandler;
22
23
/** @var ItemHandler $itemHandler */
24
require_once __DIR__ . '/common.php';
25
26
/**
27
 * @param array        $queryArray
28
 * @param              $andor
29
 * @param              $limit
30
 * @param              $offset
31
 * @param              $userid
32
 * @param array        $categories
33
 * @param int          $sortby
34
 * @param string       $searchin
35
 * @param string       $extra
36
 *
37
 * @return array
38
 */
39
function publisher_search($queryArray, $andor, $limit, $offset, $userid, $categories = [], $sortby = 0, $searchin = '', $extra = '')
40
{
41
    $helper        = Helper::getInstance();
42
    $ret           = $item = [];
43
    $hightlightKey = '';
44
45
    if (is_array($queryArray)) {
0 ignored issues
show
introduced by
The condition is_array($queryArray) is always true.
Loading history...
46
        if (0 === count($queryArray)) {
47
            $hightlightKey = '';
48
        } else {
49
            $keywords      = implode('+', $queryArray);
50
            $hightlightKey = '&amp;keywords=' . $keywords;
51
        }
52
    }
53
54
    $itemHandler      = $helper->getHandler('Item');
55
    $itemsObjs        = $itemHandler->getItemsFromSearch($queryArray, $andor, $limit, $offset, $userid, $categories, $sortby, $searchin, $extra);
56
    $withCategoryPath = $helper->getConfig('search_cat_path');
57
    //xoops_load("xoopslocal");
58
    $usersIds = [];
59
    /** @var Item $obj */
60
    if (0 !== count($itemsObjs)) {
61
        foreach ($itemsObjs as $obj) {
62
            $item['image'] = 'assets/images/item_icon.gif';
63
            $item['link']  = $obj->getItemUrl();
64
            $item['link']  .= (!empty($hightlightKey) && (false === mb_strpos($item['link'], '.php?'))) ? '?' . ltrim($hightlightKey, '&amp;') : $hightlightKey;
65
            if ($withCategoryPath) {
66
                $item['title'] = $obj->getCategoryPath(false) . ' > ' . $obj->getTitle();
67
            } else {
68
                $item['title'] = $obj->getTitle();
69
            }
70
            $item['time'] = $obj->getVar('datesub'); //must go has unix timestamp
71
            $item['uid']  = $obj->uid();
72
            //"Fulltext search/highlight
73
            $text          = $obj->getBody();
74
            $sanitizedText = '';
75
            $textLower     = \mb_strtolower($text);
76
            $queryArray    = is_array($queryArray) ? $queryArray : [$queryArray];
77
78
            if ('' != $queryArray[0] && count($queryArray) > 0) {
79
                foreach ($queryArray as $query) {
80
                    $pos           = \mb_stripos($textLower, $query); //xoops_local("strpos", $textLower, \mb_strtolower($query));
81
                    $start         = max($pos - 100, 0);
82
                    $length        = \mb_strlen($query) + 200; //xoops_local("strlen", $query) + 200;
83
                    $context       = $obj->highlight(xoops_substr($text, $start, $length, ' [...]'), $query);
84
                    $sanitizedText .= '<p>[...] ' . $context . '</p>';
85
                }
86
            }
87
            //End of highlight
88
            $item['text']      = $sanitizedText;
89
            $item['author']    = $obj->author_alias();
90
            $item['datesub']   = $obj->getDatesub($helper->getConfig('format_date'));
91
            $objUid            = $obj->uid();
92
            $usersIds[$objUid] = $objUid;
93
            $ret[]             = $item;
94
            unset($item, $sanitizedText);
95
        }
96
    }
97
    xoops_load('XoopsUserUtility');
98
    $usersNames = \XoopsUserUtility::getUnameFromIds($usersIds, $helper->getConfig('format_realname'), true);
99
    foreach ($ret as $key => $item) {
100
        if ('' == $item['author']) {
101
            $ret[$key]['author'] = $usersNames[$item['uid']] ?? '';
102
        }
103
    }
104
    unset($usersNames, $usersIds);
105
106
    return $ret;
107
}
108