Module   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getMenuItems() 0 31 6
A getControllers() 0 16 4
1
<?php
2
3
namespace app\modules\backend;
4
5
/**
6
 * @link http://www.diemeisterei.de/
7
 *
8
 * @copyright Copyright (c) 2014 diemeisterei GmbH, Stuttgart
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
use yii\helpers\ArrayHelper;
15
16
/**
17
 * Class Module.
18
 *
19
 * @author Tobias Munk <[email protected]>
20
 */
21
class Module extends \yii\base\Module
22
{
23
    public function getMenuItems()
24
    {
25
        $menuItemPresets = [
26
            'admin' => ['label' => '<i class="fa fa-dashboard"></i> <span>Dashboard</span>', 'url' => ['/admin']],
27
            'user' => ['label' => '<i class="fa fa-users"></i> <span>Users</span>', 'url' => ['/user/admin']],
28
            'packaii' => ['label' => '<i class="fa fa-cubes"></i> <span>Packages</span>', 'url' => ['/packaii']],
29
            'gii' => ['label' => '<i class="fa fa-code"></i> <span>Code Generation</span>', 'url' => ['/gii']],
30
        ];
31
32
        $autoMenuItems = [];
33
        foreach (\Yii::$app->getModules() as $name => $m) {
34
            switch ($name) {
35
                case 'admin':
36
                case 'user':
37
                case 'packaii':
38
                case 'gii':
39
                    $menuItems[] = $menuItemPresets[$name];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$menuItems was never initialized. Although not strictly required by PHP, it is generally a good practice to add $menuItems = 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...
40
                    break;
41
                default:
42
                    $module = \Yii::$app->getModule($name);
43
                    $autoMenuItems[] = [
44
                        'label' => '<i class="fa fa-cube"></i> <span>'.ucfirst($name).'</span>',
45
                        'url' => ['/'.$module->id],
46
                    ];
47
            }
48
        }
49
50
        $menuItems = ArrayHelper::merge($menuItems, $autoMenuItems);
0 ignored issues
show
Bug introduced by
The variable $menuItems 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...
51
52
        return $menuItems;
53
    }
54
55
    public function getControllers($module = null)
56
    {
57
        if ($module === null) {
58
            $module = \Yii::$app;
59
        } else {
60
            $module = \Yii::$app->getModule($module);
61
        }
62
        foreach (scandir($module->getControllerPath()) as $i => $name) {
63
            if (substr($name, 0, 1) == '.') {
64
                continue;
65
            }
66
            $controllers[] = \yii\helpers\Inflector::camel2id(str_replace('Controller.php', '', $name));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$controllers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $controllers = 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...
67
        }
68
69
        return $controllers;
0 ignored issues
show
Bug introduced by
The variable $controllers 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...
70
    }
71
}
72