| Conditions | 4 | 
| Paths | 3 | 
| Total Lines | 25 | 
| Code Lines | 16 | 
| Lines | 12 | 
| Ratio | 48 % | 
| Changes | 0 | ||
| 1 | <?php | ||
| 22 | public function mainmenu() | ||
| 23 |     { | ||
| 24 | $helper = \Xoops::getModuleHelper(basename(dirname(dirname(__DIR__)))); | ||
| 25 | $subMenu = array(); | ||
| 26 | // Prevent wasting resources | ||
| 27 | View Code Duplication |         if ($helper->isCurrentModule()) { | |
| 28 | $xoops = \Xoops::getInstance(); | ||
| 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' => $helper->url('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(), | ||
| 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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.