newbb_selectText()   B
last analyzed

Complexity

Conditions 9
Paths 16

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 26
nc 16
nop 8
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 declare(strict_types=1);
2
/**
3
 * NewBB 4.3x, the forum module for XOOPS project
4
 *
5
 * @copyright      XOOPS Project (https://xoops.org)
6
 * @license        GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
7
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>, irmtfan <[email protected]>
8
 * @since          4.3
9
 */
10
define('NEWBB_FUNCTIONS_TEXT_LOADED', true);
11
12
if (!defined('NEWBB_FUNCTIONS_TEXT')) {
13
    define('NEWBB_FUNCTIONS_TEXT', 1);
14
    /**
15
     * function for select from a text where it have some keywords
16
     *
17
     * @param string       $text
18
     * @param mixed[]|string $queryarray
19
     * @param int          $selectstartlag
20
     * @param int          $selectlength
21
     * @param bool         $striptags
22
     * @param string       $excludetags
23
     * @param string       $start_trimmarker
24
     * @param string       $end_trimmarker
25
     * @return string
26
     */
27
    function newbb_selectText(
28
        string $text,
29
               $queryarray,
30
        int    $selectstartlag = 100,
31
        int    $selectlength = 200,
32
        bool   $striptags = true,
33
        string $excludetags = '<br>',
34
        string $start_trimmarker = '[...]',
35
        string $end_trimmarker = '[...]'
36
    ): string {
37
        $sanitized_text       = $striptags ? strip_tags($text, $excludetags) : $text;
38
        $queryarray           = newbb_str2array($queryarray);
39
        $text_i               = mb_strtolower($sanitized_text);
40
        $queryarray           = array_map('\strtolower', $queryarray);
41
        $lengtharray          = array_map('\strlen', $queryarray);
42
        $maxlengthquery       = max($lengtharray);
43
        $lengthend_trimmarker = mb_strlen($end_trimmarker);
44
        $select_text          = '';
45
        $startpos             = 0;
46
        $endpos               = mb_strlen($sanitized_text);
47
        while ($startpos < $endpos) {
48
            $pos = $endpos;
49
            foreach ($queryarray as $query) {
50
                if (false !== ($thispos = mb_strpos($text_i, $query, $startpos))) {
51
                    $pos = min($thispos, $pos);
52
                }
53
            }
54
            if ($pos == $endpos) {
55
                break;
56
            }
57
            $start       = max($pos - $selectstartlag, $startpos - $maxlengthquery, 0); // $startpos is the last position in the previous select text
58
            $length      = $maxlengthquery + $selectlength; //xoops_local("strlen", $query) + 200;
59
            $select_text .= '<p>';
60
            $select_text .= ($start > 0) ? $start_trimmarker . ' ' : ' ';
61
            $select_text .= xoops_substr($sanitized_text, $start, $length + $lengthend_trimmarker + 1, ' ' . $end_trimmarker) . '</p>';
62
            $startpos    = $start + $length + 1; // start searching from next position.
63
        }
64
        if (($select_text === '' || $select_text === '0')) {
65
            return '';
66
        } // if no text return empty string
67
68
        return '<span class="newbb_select_text">' . $select_text . '</span>';
69
    }
70
71
    /**
72
     *  function for highlight a text when it have some keywords
73
     *
74
     * @param string|mixed[] $text
75
     * @param mixed[]|string $queryarray
76
     *
77
     * @return string
78
     */
79
    function newbb_highlightText($text, $queryarray): string
80
    {
81
        if (empty($GLOBALS['xoopsModuleConfig']['highlight_search_enable'])) {
82
            return $text;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $text could return the type array<mixed,mixed> which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
83
        }
84
        $queryarray = newbb_str2array($queryarray);
85
        // if $queryarray is string
86
        $highlight_text = $text;
87
        foreach ($queryarray as $key => $query) {
88
            // use preg_replace instead of str_replace to exclude all $queries inside html span tag
89
            $highlight_text = preg_replace('/(?!(?:[^<]+>|[^>]+<\/a>))(' . preg_quote((string) $query, '/') . ')/si', newbb_highlighter($query, $key), $highlight_text);
90
        }
91
92
        return $highlight_text;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $highlight_text could return the type array<mixed,mixed> which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
93
    }
94
95
    /**
96
     * function for highlighting search results
97
     *
98
     * @param string $query
99
     * @param int    $i
100
     * @return string
101
     */
102
    function newbb_highlighter(string $query, int $i): string
103
    {
104
        return '<span class="newbb_highlight term' . $i . '">' . $query . '</span>';
105
    }
106
107
    /**
108
     * function for convert string to array
109
     * @param string|mixed[] $str
110
     * @return array
111
     */
112
    function newbb_str2array($str): array
113
    {
114
        if (is_array($str)) {
115
            return $str;
116
        }
117
118
        // split the phrase by any number of commas or space characters,
119
        // which include " ", \r, \t, \n and \f
120
        $temp_str = preg_split('/[\s,]+/', $str);
121
        $strarray = [];
122
        foreach ($temp_str as $s) {
123
            $strarray[] = addslashes((string) $s);
124
        }
125
126
        return $strarray;
127
    }
128
}
129