Completed
Pull Request — master (#599)
by Richard
17:07
created

PublisherSearchPlugin::searchAdvanced()   B

Complexity

Conditions 9
Paths 54

Size

Total Lines 42
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 32
nc 54
nop 9
dl 0
loc 42
rs 8.0555
c 0
b 0
f 0

How to fix   Many Parameters   

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
2
3
//namespace XoopsModules\Publisher\Plugin;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
use SearchPluginInterface;
16
use Xmf\Metagen;
17
use Xoops\Module\Plugin\PluginAbstract;
18
use XoopsModules\Publisher;
19
use XoopsModules\Publisher\Helper;
20
use XoopsUserUtility;
21
22
/**
23
 *  Publisher class
24
 *
25
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
26
 * @license         GNU GPL V2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
27
 * @package         Class
28
 * @subpackage      Utils
29
 * @since           1.0
30
 * @author          trabis <[email protected]>
31
 * @version         $Id$
32
 */
33
class PublisherSearchPlugin extends PluginAbstract implements SearchPluginInterface
34
{
35
    /**
36
     * @param string[] $queryarray
37
     * @param string   $andor
38
     * @param int      $limit
39
     * @param int      $offset
40
     * @param int      $userid
41
     * @return array
42
     */
43
    public function search($queryarray, $andor, $limit, $offset, $userid)
44
    {
45
        $categories = [];
46
        $sortby = 0;
47
        $searchin = '';
48
        $extra = '';
49
50
        return self::searchAdvanced($queryarray, $andor, $limit, $offset, $userid, $categories, $sortby, $searchin, $extra);
51
    }
52
53
    /**
54
     * @param        $queryarray
55
     * @param        $andor
56
     * @param        $limit
57
     * @param        $offset
58
     * @param        $userid
59
     * @param array  $categories
60
     * @param int    $sortby
61
     * @param string $searchin
62
     * @param string $extra
63
     */
64
    public static function searchAdvanced($queryarray, $andor, $limit, $offset, $userid, $categories = [], $sortby = 0, $searchin = '', $extra = ''): array
65
    {
66
        $helper = Helper::getInstance();
67
        $ret = [];
68
        if ('' == $queryarray || 0 == \count($queryarray)) {
69
            $hightlight_key = '';
70
        } else {
71
            $keywords = \implode('+', $queryarray);
72
            $hightlight_key = '&amp;keywords=' . $keywords;
73
        }
74
        $itemsObjs = $helper->getItemHandler()->getItemsFromSearch($queryarray, $andor, $limit, $offset, $userid, $categories, $sortby, $searchin, $extra);
75
        $withCategoryPath = $helper->getConfig('search_cat_path');
76
77
        $usersIds = [];
78
        /* @var Publisher\Item $obj */
79
        foreach ($itemsObjs as $obj) {
80
            $item['image'] = 'images/item_icon.gif';
81
            $item['link'] = $obj->getItemUrl();
82
            $item['link'] .= (!empty($hightlight_key) && (false === \mb_strpos($item['link'], '.php?'))) ? '?' . \ltrim($hightlight_key, '&amp;') : $hightlight_key;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $item seems to be defined later in this foreach loop on line 80. Are you sure it is defined here?
Loading history...
83
            if ($withCategoryPath) {
84
                $item['title'] = $obj->getCategoryPath(false) . ' > ' . $obj->title();
85
            } else {
86
                $item['title'] = $obj->title();
87
            }
88
            $item['time'] = $obj->getVar('datesub'); //must go has unix timestamp
89
            $item['uid'] = $obj->getVar('uid');
90
            $item['content'] = Metagen::getSearchSummary($obj->body(), $queryarray);
91
            $item['author'] = $obj->getVar('author_alias');
92
            $item['datesub'] = $obj->datesub($helper->getConfig('format_date'));
93
            $usersIds[$obj->getVar('uid')] = $obj->getVar('uid');
94
            $ret[] = $item;
95
            unset($item, $sanitized_text);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sanitized_text seems to be never defined.
Loading history...
96
        }
97
        $usersNames = XoopsUserUtility::getUnameFromIds($usersIds, $helper->getConfig('format_realname'), true);
98
        foreach ($ret as $key => $item) {
99
            if ('' == $item['author']) {
100
                $ret[$key]['author'] = @$usersNames[$item['uid']];
101
            }
102
        }
103
        unset($usersNames, $usersIds);
104
105
        return $ret;
106
    }
107
}
108