Conditions | 4 |
Paths | 3 |
Total Lines | 24 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
23 | public function usermenu() |
||
24 | { |
||
25 | $helper = Xoops::getModuleHelper('userconfigs'); |
||
26 | $subMenu = array(); |
||
27 | // Prevent wasting resources |
||
28 | if ($helper->isCurrentModule()) { |
||
29 | if ($plugins = \Xoops\Module\Plugin::getPlugins('userconfigs')) { |
||
30 | foreach (array_keys($plugins) as $dirName) { |
||
31 | $mHelper = Xoops::getModuleHelper($dirName); |
||
32 | $subMenu[] = [ |
||
33 | 'name' => $mHelper->getModule()->getVar('name'), |
||
34 | 'link' => 'index.php?op=showmod&mid=' . $mHelper->getModule()->getVar('mid') |
||
35 | ]; |
||
36 | } |
||
37 | } |
||
38 | } |
||
39 | |||
40 | $ret[] = [ |
||
|
|||
41 | 'name' => $helper->getModule()->getVar('name'), |
||
42 | 'link' => $helper->url('index.php'), |
||
43 | 'subMenu' => $subMenu, |
||
44 | ]; |
||
45 | return $ret; |
||
46 | } |
||
47 | } |
||
48 |
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:
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 thebar
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.