Passed
Push — master ( 02dd48...6499a1 )
by Goffy
36s queued 11s
created

LetterChoice::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 2
nop 8
dl 0
loc 23
rs 9.9332
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\Xoopstube\Common;
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
/**
16
 * LetterChoice class
17
 *
18
 * @copyright   XOOPS Project (https://xoops.org)
19
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
20
 * @author      lucio <[email protected]>
21
 * @package     xoopstube
22
 * @since       1.00
23
 *
24
 * Example:
25
 * $choicebyletter = new Xoopstube\LetterChoice($objHandler, null, null, range('a', 'z'), 'letter');
26
 * echo $choicebyletter->render();
27
 */
28
29
use CriteriaCompo;
30
use XoopsModules\Xoopstube\{
31
    Helper
32
};
33
/** @var Helper $helper */
34
35
// require_once  dirname(dirname(__DIR__)) . '/include/common.php';
36
37
/**
38
 * Class LetterChoice
39
 */
40
class LetterChoice
41
{
42
    /**
43
     * @access public
44
     */
45
    public $helper = null;
46
    /**
47
     * *#@+
48
     *
49
     * @access private
50
     */
51
    private $objHandler;
52
    private $criteria;
53
    private $field_name;
54
    private $alphabet;
55
    private $arg_name;
56
    private $url;
57
    private $extra;
58
    private $caseSensitive;
59
60
//    /**
61
//     * *#@-
62
//     * @param mixed      $objHandler
63
//     * @param null|mixed $criteria
64
//     * @param null|mixed $field_name
65
//     * @param mixed      $arg_name
66
//     * @param null|mixed $url
67
//     * @param mixed      $extra_arg
68
//     * @param mixed      $caseSensitive
69
//     */
70
71
    /**
72
     * Constructor
73
     *
74
     * @param \XoopsPersistableObjectHandler $objHandler {@link XoopsPersistableObjectHandler}
75
     * @param \CriteriaElement               $criteria   {@link CriteriaElement}
76
     * @param string                         $field_name search by field
77
     * @param array                          $alphabet   array of alphabet letters
78
     * @param string                         $arg_name   item on the current page
79
     * @param string                         $url
80
     * @param string                         $extra_arg  Additional arguments to pass in the URL
81
     * @param bool                           $caseSensitive
82
     */
83
    public function __construct(
84
        $objHandler,
85
        $criteria = null,
86
        $field_name = null,
87
        array $alphabet = [],
0 ignored issues
show
Unused Code introduced by
The parameter $alphabet is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

87
        /** @scrutinizer ignore-unused */ array $alphabet = [],

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
        $arg_name = 'letter',
89
        $url = null,
90
        $extra_arg = '',
91
        $caseSensitive = false
92
    ) {
93
        $this->helper     = Helper::getInstance();
94
        $this->objHandler = $objHandler;
95
        $this->criteria   = $criteria ?? new CriteriaCompo();
96
        $this->field_name = $field_name ?? $this->objHandler->identifierName;
97
        //        $this->alphabet   = (count($alphabet) > 0) ? $alphabet : range('a', 'z'); // is there a way to get locale alphabet?
98
        //        $this->alphabet       = getLocalAlphabet();
99
        $this->alphabet = require_once dirname(__DIR__, 2) . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/alphabet.php';
100
        $this->arg_name = $arg_name;
101
        $this->url      = $url ?? $_SERVER['SCRIPT_NAME'];
102
        if ('' !== $extra_arg && ('&amp;' !== mb_substr($extra_arg, -5) || '&' !== mb_substr($extra_arg, -1))) {
103
            $this->extra = '&amp;' . $extra_arg;
104
        }
105
        $this->caseSensitive = $caseSensitive;
106
    }
107
108
    /**
109
     * Create choice by letter
110
     *
111
     * @param null $alphaCount
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $alphaCount is correct as it would always require null to be passed?
Loading history...
112
     * @param null $howmanyother
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $howmanyother is correct as it would always require null to be passed?
Loading history...
113
     * @return string
114
     */
115
    public function render($alphaCount = null, $howmanyother = null)
116
    {
117
        $moduleDirName      = basename(dirname(__DIR__, 2));
118
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
119
        xoops_loadLanguage('common', $moduleDirName);
120
        xoops_loadLanguage('alphabet', $moduleDirName);
121
        $all   = constant('CO_' . $moduleDirNameUpper . '_ALL');
122
        $other = constant('CO_' . $moduleDirNameUpper . '_OTHER');
123
124
        $ret = '';
125
126
        if (!$this->caseSensitive) {
127
            $this->criteria->setGroupBy('UPPER(LEFT(' . $this->field_name . ',1))');
128
        } else {
129
            $this->criteria->setGroupBy('LEFT(' . $this->field_name . ',1)');
130
        }
131
        $countsByLetters = $this->objHandler->getCounts($this->criteria);
132
        // fill alphabet array
133
        $alphabetArray = [];
134
        $letter_array  = [];
135
136
        $letter                 = 'All';
137
        $letter_array['letter'] = $all;
138
        $letter_array['count']  = $alphaCount;
139
        $letter_array['url']    = $this->url;
140
        $alphabetArray[$letter] = $letter_array;
141
142
        foreach ($this->alphabet as $letter) {
143
//            $letter_array = [];
144
            if (!$this->caseSensitive) {
145
                if (isset($countsByLetters[mb_strtoupper($letter)])) {
146
                    $letter_array['letter'] = $letter;
147
                    $letter_array['count']  = $countsByLetters[mb_strtoupper($letter)];
148
                    $letter_array['url']    = $this->url . '?' . $this->arg_name . '=' . $letter . $this->extra;
149
                } else {
150
                    $letter_array['letter'] = $letter;
151
                    $letter_array['count']  = 0;
152
                    $letter_array['url']    = '';
153
                }
154
            } elseif (isset($countsByLetters[$letter])) {
155
                $letter_array['letter'] = $letter;
156
                $letter_array['count']  = $countsByLetters[$letter];
157
                $letter_array['url']    = $this->url . '?' . $this->arg_name . '=' . $letter . $this->extra;
158
            } else {
159
                $letter_array['letter'] = $letter;
160
                $letter_array['count']  = 0;
161
                $letter_array['url']    = '';
162
            }
163
            $alphabetArray[$letter] = $letter_array;
164
            unset($letter_array);
165
        }
166
167
        $letter_array['letter'] = $other;
168
        $letter_array['count']  = $howmanyother;
169
        $letter_array['url']    = $this->url . '?init=Other';
170
        $alphabetArray[$letter] = $letter_array;
171
172
        // render output
173
        if (!isset($GLOBALS['xoTheme']) || !is_object($GLOBALS['xoTheme'])) {
174
            require_once $GLOBALS['xoops']->path('/class/theme.php');
175
            $GLOBALS['xoTheme'] = new \xos_opal_Theme();
176
        }
177
        require_once $GLOBALS['xoops']->path('/class/template.php');
178
        $choiceByLetterTpl          = new \XoopsTpl();
179
        $choiceByLetterTpl->caching = 0; // Disable cache
180
        $choiceByLetterTpl->assign('alphabet', $alphabetArray);
181
        $ret .= $choiceByLetterTpl->fetch("db:{$this->helper->getDirname()}_letterschoice.tpl");
182
        unset($choiceByLetterTpl);
183
184
        return $ret;
185
    }
186
}
187