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
Pull Request — master (#1192)
by
unknown
14:48
created

PMF_Helper_Category::renderNavigation()   F

Complexity

Conditions 21
Paths 263

Size

Total Lines 94
Code Lines 62

Duplication

Lines 8
Ratio 8.51 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 8
loc 94
rs 3.6155
c 1
b 0
f 1
cc 21
eloc 62
nc 263
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Helper class for phpMyFAQ categories
4
 *
5
 * PHP Version 5.4
6
 *
7
 * This Source Code Form is subject to the terms of the Mozilla Public License,
8
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
9
 * obtain one at http://mozilla.org/MPL/2.0/.
10
 *
11
 * @category  phpMyFAQ
12
 * @package   Helper
13
 * @author    Thorsten Rinne <[email protected]>
14
 * @copyright 2009-2014 phpMyFAQ Team
15
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
16
 * @link      http://www.phpmyfaq.de
17
 * @since     2009-09-07
18
 */
19
20
if (!defined('IS_VALID_PHPMYFAQ')) {
21
    exit();
22
}
23
24
/**
25
 * PMF_Helper_Category
26
 *
27
 * @category  phpMyFAQ
28
 * @package   Helper
29
 * @author    Thorsten Rinne <[email protected]>
30
 * @copyright 2009-2014 phpMyFAQ Team
31
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
32
 * @link      http://www.phpmyfaq.de
33
 * @since     2009-09-07
34
 */
35
class PMF_Helper_Category extends PMF_Helper 
36
{
37
    /**
38
     * Constructor
39
     *
40
     * @return PMF_Helper_Category
41
     */
42
    public function __construct()
43
    {
44
    }
45
46
    /**
47
     * Renders the main navigation
48
     *
49
     * @param  integer $activeCategory Selected category
50
     *
51
     * @return string
52
     */
53
    public function renderNavigation($activeCategory = 0)
54
    {
55
        global $sids, $PMF_LANG;
56
57
        $open          = 0;
58
        $output        = '';
59
        $numCategories = $this->Category->height();
60
        $numFaqs       = $this->Category->getNumberOfRecordsOfCategory();
61
        
62
        if ($numCategories > 0) {
63
            for ($y = 0 ;$y < $numCategories; $y = $this->Category->getNextLineTree($y)) {
64
                
65
                list($hasChild, $name, $categoryId, $description) = $this->Category->getLineDisplay($y);
66
67
                if ($activeCategory == $categoryId) {
68
                    $isActive = true;
69
                } else {
70
                    $isActive = false;
71
                }
72
73
                $level     = $this->Category->treeTab[$y]['level'];
74
                $leveldiff = $open - $level;
75
76
                if ($this->_config->get('records.hideEmptyCategories') && !isset($numFaqs[$categoryId]) &&
77
                    '-' === $hasChild) {
78
                    continue;
79
                }
80
81 View Code Duplication
                if ($leveldiff > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
                    $output .= '</li>';
83
                    for ($i = $leveldiff; $i > 1; $i--) {
84
                        $output .= sprintf("\n%s</ul>\n%s</li>\n",
85
                            str_repeat("\t", $level + $i + 1),
86
                            str_repeat("\t", $level + $i));
87
                    }
88
                }
89
90
                if ($level < $open) {
91
                    if (($level - $open) == -1) {
92
                        $output .= '</li>';
93
                    }
94
                    $output .= "\n".str_repeat("\t", $level + 2)."</ul>\n".str_repeat("\t", $level + 1)."</li>\n";
95
                } elseif ($level == $open && $y != 0) {
96
                    $output .= "</li>\n";
97
                }
98
99
                if ($level > $open) {
100
                    $output .= sprintf(
101
                        "\n%s<ul class=\"nav nav-list\">\n%s<li%s>",
102
                        str_repeat("\t", $level + 1),
103
                        str_repeat("\t", $level + 1),
104
                        $isActive ? ' class="active"' : ''
105
                    );
106
                } else {
107
                    $output .= sprintf(
108
                        "%s<li%s>",
109
                        str_repeat("\t", $level + 1),
110
                        $isActive ? ' class="active"' : ''
111
                    );
112
                }
113
                
114
                if (isset($this->Category->treeTab[$y]['symbol']) && $this->Category->treeTab[$y]['symbol'] == 'plus') {
115
                    $output .= $this->Category->addCategoryLink(
116
                        $sids, $categoryId, $name, $description, true, $isActive
117
                    );
118
                } else {
119
                    if ($this->Category->treeTab[$y]['symbol'] == 'minus') {
120
                        $name = ($this->Category->treeTab[$y]['parent_id'] == 0) 
121
                                ? 
122
                                $name 
123
                                : 
124
                                $this->Category->categoryName[$this->Category->treeTab[$y]['id']]['name'];
125
                        $output .= $this->Category->addCategoryLink(
126
                            $sids, $this->Category->treeTab[$y]['parent_id'], $name, $description, false, $isActive
127
                        );
128
                    } else {
129
                        $output .= $this->Category->addCategoryLink(
130
                            $sids, $categoryId, $name, $description, false, $isActive
131
                        );
132
                    }
133
                }
134
                $open = $level;
135
            }
136
            if ($open > 0) {
137
                $output .= str_repeat("</li>\n\t</ul>\n\t", $open);
138
            }
139
            $output .= "</li>";
140
            return $output;
141
142
        } else {
143
            $output = '<li><a href="#">'.$PMF_LANG['no_cats'].'</a></li>';
144
        }
145
        return $output;
146
    }
147
148
    /**
149
     * Renders the main navigation dropdown
150
     *
151
     * @return string
152
     */
153
    public function renderCategoryDropDown()
154
    {
155
156
        global $sids, $PMF_LANG;
157
158
        $open          = 0;
159
        $output        = '';
160
        $numCategories = $this->Category->height();
161
162
        $this->Category->expandAll();
163
164
        if ($numCategories > 0) {
165
166
            for ($y = 0 ;$y < $this->Category->height(); $y = $this->Category->getNextLineTree($y)) {
167
168
                list($hasChild, $categoryName, $parent, $description) = $this->Category->getLineDisplay($y);
169
                $level     = $this->Category->treeTab[$y]['level'];
170
                $leveldiff = $open - $level;
171
                $numChilds = $this->Category->treeTab[$y]['numChilds'];
172
173
                if (!isset($number[$parent])) {
174
                    $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...
175
                }
176
177 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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
                    continue;
179
                }
180
181 View Code Duplication
                if ($leveldiff > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
                    $output .= '</li>';
183
                    for ($i = $leveldiff; $i > 1; $i--) {
184
                        $output .= sprintf("\n%s</ul>\n%s</li>\n",
185
                            str_repeat("\t", $level + $i + 1),
186
                            str_repeat("\t", $level + $i));
187
                    }
188
                }
189
190 View Code Duplication
                if ($level < $open) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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