LetterChoice::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 2
nop 8
dl 0
loc 23
rs 9.9332
c 1
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\Lexikon\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     Lexikon
22
 * @since       1.00
23
 * @version     $Id:$
24
 *
25
 * Example:
26
 * $choicebyletter = new Lexikon\LetterChoice($objHandler, null, null, range('a', 'z'), 'letter');
27
 * echo $choicebyletter->render();
28
 */
29
30
use XoopsModules\Lexikon\{
31
    Helper
32
};
33
/** @var Helper $this ->helper */
34
35
// require_once \dirname(__DIR__, 2) . '/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
     * @param mixed      $objHandler
62
     * @param null|mixed $criteria
63
     * @param null|mixed $field_name
64
     * @param mixed      $arg_name
65
     * @param null|mixed $url
66
     * @param mixed      $extra_arg
67
     * @param mixed      $caseSensitive
68
     */
69
70
    /**
71
     * Constructor
72
     *
73
     * @param \XoopsPersistableObjectHandler $objHandler {@link XoopsPersistableObjectHandler}
74
     * @param \CriteriaElement               $criteria   {@link CriteriaElement}
75
     * @param string                         $field_name search by field
76
     * @param array                          $alphabet   array of alphabet letters
77
     * @param string                         $arg_name   item on the current page
78
     * @param string                         $url
79
     * @param string                         $extra_arg  Additional arguments to pass in the URL
80
     * @param bool                           $caseSensitive
81
     */
82
    public function __construct(
83
        $objHandler,
84
        $criteria = null,
85
        $field_name = null,
86
        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

86
        /** @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...
87
        $arg_name = 'letter',
88
        $url = null,
89
        $extra_arg = '',
90
        $caseSensitive = false
91
    ) {
92
        $this->helper     = Helper::getInstance();
93
        $this->objHandler = $objHandler;
94
        $this->criteria   = $criteria ?? new \CriteriaCompo();
95
        $this->field_name = $field_name ?? $this->objHandler->identifierName;
96
        //        $this->alphabet   = (count($alphabet) > 0) ? $alphabet : range('a', 'z'); // is there a way to get locale alphabet?
97
        //        $this->alphabet       = getLocalAlphabet();
98
        $this->alphabet = require_once \dirname(__DIR__, 2) . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/alphabet.php';
99
        $this->arg_name = $arg_name;
100
        $this->url      = $url ?? $_SERVER['SCRIPT_NAME'];
101
        if ('' !== $extra_arg && ('&amp;' !== mb_substr($extra_arg, -5) || '&' !== mb_substr($extra_arg, -1))) {
102
            $this->extra = '&amp;' . $extra_arg;
103
        }
104
        $this->caseSensitive = $caseSensitive;
105
    }
106
107
    /**
108
     * Create choice by letter
109
     *
110
     * @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...
111
     * @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...
112
     * @return string
113
     */
114
    public function render($alphaCount = null, $howmanyother = null)
115
    {
116
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
117
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
118
        \xoops_loadLanguage('common', $moduleDirName);
119
        \xoops_loadLanguage('alphabet', $moduleDirName);
120
        $all   = \constant('CO_' . $moduleDirNameUpper . '_ALL');
121
        $other = \constant('CO_' . $moduleDirNameUpper . '_OTHER');
122
123
        $ret = '';
124
125
        if (!$this->caseSensitive) {
126
            $this->criteria->setGroupBy('UPPER(LEFT(' . $this->field_name . ',1))');
127
        } else {
128
            $this->criteria->setGroupBy('LEFT(' . $this->field_name . ',1)');
129
        }
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
            } else {
155
                if (isset($countsByLetters[$letter])) {
156
                    $letter_array['letter'] = $letter;
157
                    $letter_array['count']  = $countsByLetters[$letter];
158
                    $letter_array['url']    = $this->url . '?' . $this->arg_name . '=' . $letter . $this->extra;
159
                } else {
160
                    $letter_array['letter'] = $letter;
161
                    $letter_array['count']  = 0;
162
                    $letter_array['url']    = '';
163
                }
164
            }
165
            $alphabetArray[$letter] = $letter_array;
166
            unset($letter_array);
167
        }
168
169
        $letter_array['letter'] = $other;
170
        $letter_array['count']  = $howmanyother;
171
        $letter_array['url']    = $this->url . '?init=Other';
172
        $alphabetArray[$letter] = $letter_array;
173
174
        // render output
175
        if (!isset($GLOBALS['xoTheme']) || !\is_object($GLOBALS['xoTheme'])) {
176
            require_once $GLOBALS['xoops']->path('/class/theme.php');
177
            $GLOBALS['xoTheme'] = new \xos_opal_Theme();
178
        }
179
        require_once $GLOBALS['xoops']->path('/class/template.php');
180
        $choiceByLetterTpl          = new \XoopsTpl();
181
        $choiceByLetterTpl->caching = false; // Disable cache
0 ignored issues
show
Documentation Bug introduced by
The property $caching was declared of type integer, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
182
        $choiceByLetterTpl->assign('alphabet', $alphabetArray);
183
        $ret .= $choiceByLetterTpl->fetch("db:{$this->helper->getDirname()}_letterschoice.tpl");
184
        unset($choiceByLetterTpl);
185
186
        return $ret;
187
    }
188
}
189