GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 2.9 ( 4f9de0...9a65a3 )
by Thorsten
08:39
created

PMF_Helper_Category::renderOptions()   D

Complexity

Conditions 9
Paths 15

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 25
nc 15
nop 1
dl 0
loc 40
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Helper class for phpMyFAQ categories.
5
 *
6
 * PHP Version 5.5
7
 *
8
 * This Source Code Form is subject to the terms of the Mozilla Public License,
9
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
10
 * obtain one at http://mozilla.org/MPL/2.0/.
11
 *
12
 * @category  phpMyFAQ
13
 *
14
 * @author    Thorsten Rinne <[email protected]>
15
 * @copyright 2009-2017 phpMyFAQ Team
16
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
17
 *
18
 * @link      http://www.phpmyfaq.de
19
 * @since     2009-09-07
20
 */
21
if (!defined('IS_VALID_PHPMYFAQ')) {
22
    exit();
23
}
24
25
/**
26
 * PMF_Helper_Category.
27
 *
28
 * @category  phpMyFAQ
29
 *
30
 * @author    Thorsten Rinne <[email protected]>
31
 * @copyright 2009-2017 phpMyFAQ Team
32
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
33
 *
34
 * @link      http://www.phpmyfaq.de
35
 * @since     2009-09-07
36
 */
37
class PMF_Helper_Category extends PMF_Helper
38
{
39
    /**
40
     * Renders the main navigation.
41
     *
42
     * @param int $activeCategory Selected category
43
     *
44
     * @return string
45
     */
46
    public function renderNavigation($activeCategory = 0)
47
    {
48
        global $sids, $PMF_LANG;
49
50
        $open = 0;
51
        $output = '';
52
        $numCategories = $this->Category->height();
53
        $numFaqs = $this->Category->getNumberOfRecordsOfCategory();
54
55
        if ($numCategories > 0) {
56
            for ($y = 0;$y < $numCategories; $y = $this->Category->getNextLineTree($y)) {
57
                list($hasChild, $name, $categoryId, $description, $active) = $this->Category->getLineDisplay($y);
58
59
                if (!$active) {
60
                    continue;
61
                }
62
63
                if ($activeCategory == $categoryId) {
64
                    $isActive = true;
65
                } else {
66
                    $isActive = false;
67
                }
68
69
                $level = $this->Category->treeTab[$y]['level'];
70
                $leveldiff = $open - $level;
71
72
                if ($this->_config->get('records.hideEmptyCategories') && !isset($numFaqs[$categoryId]) &&
73
                    '-' === $hasChild) {
74
                    continue;
75
                }
76
77 View Code Duplication
                if ($leveldiff > 1) {
78
                    $output .= '</li>';
79
                    for ($i = $leveldiff; $i > 1; --$i) {
80
                        $output .= sprintf("\n%s</ul>\n%s</li>\n",
81
                            str_repeat("\t", $level + $i + 1),
82
                            str_repeat("\t", $level + $i));
83
                    }
84
                }
85
86
                if ($level < $open) {
87
                    if (($level - $open) == -1) {
88
                        $output .= '</li>';
89
                    }
90
                    $output .= "\n".str_repeat("\t", $level + 2)."</ul>\n".str_repeat("\t", $level + 1)."</li>\n";
91
                } elseif ($level == $open && $y != 0) {
92
                    $output .= "</li>\n";
93
                }
94
95
                if ($level > $open) {
96
                    $output .= sprintf(
97
                        "\n%s<ul class=\"nav nav-list\">\n%s<li%s>",
98
                        str_repeat("\t", $level + 1),
99
                        str_repeat("\t", $level + 1),
100
                        $isActive ? ' class="active"' : ''
101
                    );
102
                } else {
103
                    $output .= sprintf(
104
                        '%s<li%s>',
105
                        str_repeat("\t", $level + 1),
106
                        $isActive ? ' class="active"' : ''
107
                    );
108
                }
109
110
                if (isset($this->Category->treeTab[$y]['symbol']) && $this->Category->treeTab[$y]['symbol'] == 'plus') {
111
                    $output .= $this->Category->addCategoryLink(
112
                        $sids, $categoryId, $name, $description, true, $isActive
113
                    );
114
                } else {
115
                    if ($this->Category->treeTab[$y]['symbol'] == 'minus') {
116
                        $name = ($this->Category->treeTab[$y]['parent_id'] === 0)
117
                                ?
118
                                $name
119
                                :
120
                                $this->Category->categoryName[$this->Category->treeTab[$y]['id']]['name'];
121
                        $output .= $this->Category->addCategoryLink(
122
                            $sids, $categoryId, $name, $description, false, $isActive
123
                        );
124
                    } else {
125
                        $output .= $this->Category->addCategoryLink(
126
                            $sids, $categoryId, $name, $description, false, $isActive
127
                        );
128
                    }
129
                }
130
                $open = $level;
131
            }
132
            if ($open > 0) {
133
                $output .= str_repeat("</li>\n\t</ul>\n\t", $open);
134
            }
135
            $output .= '</li>';
136
137
            return $output;
138
        } else {
139
            $output = '<li><a href="#">'.$PMF_LANG['no_cats'].'</a></li>';
140
        }
141
142
        return $output;
143
    }
144
145
    /**
146
     * Renders the main navigation dropdown.
147
     *
148
     * @return string
149
     */
150
    public function renderCategoryDropDown()
151
    {
152
        global $sids, $PMF_LANG;
153
154
        $open = 0;
155
        $output = '';
156
        $numCategories = $this->Category->height();
157
158
        $this->Category->expandAll();
159
160
        if ($numCategories > 0) {
161
            for ($y = 0;$y < $this->Category->height(); $y = $this->Category->getNextLineTree($y)) {
162
                list($hasChild, $categoryName, $parent, $description, $active) = $this->Category->getLineDisplay($y);
163
164
                if (!$active) {
165
                    continue;
166
                }
167
168
                $level = $this->Category->treeTab[$y]['level'];
169
                $leveldiff = $open - $level;
170
                $numChilds = $this->Category->treeTab[$y]['numChilds'];
171
172
                if (!isset($number[$parent])) {
173
                    $number[$parent] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$number was never initialized. Although not strictly required by PHP, it is generally a good practice to add $number = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
174
                }
175
176 View Code Duplication
                if ($this->_config->get('records.hideEmptyCategories') && 0 === $number[$parent] && '-' === $hasChild) {
0 ignored issues
show
Bug introduced by
The variable $number does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
177
                    continue;
178
                }
179
180 View Code Duplication
                if ($leveldiff > 1) {
181
                    $output .= '</li>';
182
                    for ($i = $leveldiff; $i > 1; --$i) {
183
                        $output .= sprintf("\n%s</ul>\n%s</li>\n",
184
                            str_repeat("\t", $level + $i + 1),
185
                            str_repeat("\t", $level + $i));
186
                    }
187
                }
188
189
                if ($level < $open) {
190
                    if (($level - $open) == -1) {
191
                        $output .= '</li>';
192
                    }
193
                    $output .= sprintf("\n%s</ul>\n%s</li>\n",
194
                        str_repeat("\t", $level + 2),
195
                        str_repeat("\t", $level + 1));
196
                } elseif ($level == $open && $y != 0) {
197
                    $output .= "</li>\n";
198
                }
199
200
                if ($level > $open) {
201
                    $output .= sprintf(
202
                        "\n%s<ul class=\"dropdown-menu\">\n%s",
203
                        str_repeat("\t", $level + 1),
204
                        str_repeat("\t", $level + 1)
205
                    );
206
                    if ($numChilds > 0) {
207
                        $output .= '<li class="dropdown-submenu">';
208
                    } else {
209
                        $output .= '<li>';
210
                    }
211
                } else {
212
                    $output .= str_repeat("\t", $level + 1);
213
                    if ($numChilds > 0) {
214
                        $output .= '<li class="dropdown-submenu">';
215
                    } else {
216
                        $output .= '<li>';
217
                    }
218
                }
219
220
                $url = sprintf(
221
                    '%s?%saction=show&amp;cat=%d',
222
                    PMF_Link::getSystemRelativeUri(),
223
                    $sids,
224
                    $parent
225
                );
226
                $oLink = new PMF_Link($url, $this->_config);
227
                $oLink->itemTitle = $categoryName;
228
                $oLink->text = $categoryName;
229
                $oLink->tooltip = $description;
230
231
                $output .= $oLink->toHtmlAnchor();
232
                $open = $level;
233
            }
234
235
            if (isset($level) && $level > 0) {
236
                $output .= str_repeat("</li>\n\t</ul>\n\t", $level);
237
            }
238
239
            return $output;
240
        } else {
241
            $output = '<li><a href="#">'.$PMF_LANG['no_cats'].'</a></li>';
242
        }
243
244
        return $output;
245
    }
246
247
    /**
248
     * Returns all top-level categories in <li> tags.
249
     *
250
     * @return string
251
     */
252
    public function renderMainCategories()
253
    {
254
        $categories = '';
255
        foreach ($this->Category->categories as $cat) {
256
            if (0 === (int) $cat['parent_id']) {
257
                $categories .= sprintf(
258
                    '<li><a href="?action=show&cat=%d">%s</a></li>',
259
                    $cat['id'],
260
                    $cat['name']
261
                    );
262
            }
263
        }
264
265
        return $categories;
266
    }
267
268
    /**
269
     * Get all categories in <option> tags.
270
     *
271
     * @param array|int $categoryId Category ID or array of category IDs
272
     *
273
     * @return string
274
     */
275
    public function renderOptions($categoryId)
276
    {
277
        $categories = '';
278
279
        if (!is_array($categoryId)) {
280
            $categoryId = array(
281
                array(
282
                    'category_id' => $categoryId,
283
                    'category_lang' => '',
284
                ),
285
            );
286
        } elseif (isset($categoryId['category_id'])) {
287
            $categoryId = array($categoryId);
288
        }
289
290
        $i = 0;
291
        foreach ($this->Category->catTree as $cat) {
292
            $indent = '';
293
            for ($j = 0; $j < $cat['indent']; ++$j) {
294
                $indent .= '....';
295
            }
296
            $categories .= "\t<option value=\"".$cat['id'].'"';
297
298
            if (0 === $i && count($categoryId) === 0) {
299
                $categories .= ' selected';
300
            } else {
301
                foreach ($categoryId as $categoryid) {
302
                    if ($cat['id'] == $categoryid['category_id']) {
303
                        $categories .= ' selected';
304
                    }
305
                }
306
            }
307
308
            $categories .= '>';
309
            $categories .= $indent.$cat['name']."</option>\n";
310
            ++$i;
311
        }
312
313
        return $categories;
314
    }
315
}
316