Completed
Push — master ( c622f7...33a06a )
by Richard
09:52 queued 02:32
created

CodexUsermenuPlugin::usermenu()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 2
nop 0
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
14
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
15
 * @author          trabis <[email protected]>
16
 */
17
18
class CodexUsermenuPlugin implements UsermenuPluginInterface
19
{
20
21
    /**
22
     * @return array
23
     */
24
    public function usermenu()
25
    {
26
        $helper = \Xoops::getModuleHelper('codex');
27
        $subMenu = array();
28
        // Prevent wasting resources
29
        if ($helper->isCurrentModule()) {
30
            $files = \Xoops\Core\Lists\File::getList($helper->path('/'));
31
            $i = 0;
32
            foreach ($files as $file) {
33
                if (!in_array($file, array('xoops_version.php', 'index.php'))) {
34
                    $fileName = ucfirst(str_replace('.php', '', $file));
35
                    $subMenu[] = [
36
                        'name' => $fileName,
37
                        'link' => $file,
38
                    ];
39
                    ++$i;
40
                }
41
            }
42
        }
43
44
        $ret[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = 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...
45
            'name' => $helper->getModule()->getVar('name'),
46
            'link' => $helper->url('index.php'),
47
            'subMenu' => $subMenu,
48
        ];
49
50
        return $ret;
51
    }
52
}