| Conditions | 4 | 
| Paths | 2 | 
| Total Lines | 28 | 
| Code Lines | 18 | 
| Lines | 14 | 
| Ratio | 50 % | 
| Changes | 0 | ||
| 1 | <?php | ||
| 24 | public function mainmenu() | ||
| 25 |     { | ||
| 26 | $helper = \Xoops::getModuleHelper(basename(dirname(dirname(__DIR__)))); | ||
| 27 | $subMenu = array(); | ||
| 28 | // Prevent wasting resources | ||
| 29 | View Code Duplication |         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[] = [ | ||
|  | |||
| 45 |             'name' => $helper->getModule()->getVar('name'), | ||
| 46 | 'link' => $helper->url(), | ||
| 47 | 'subMenu' => $subMenu, | ||
| 48 | ]; | ||
| 49 | |||
| 50 | return $ret; | ||
| 51 | } | ||
| 52 | } | 
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.