MenuItem::hasActiveSubmenu()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.5555
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
namespace Aminetiyal\LaravelTemplate\Components\Lte;
4
5
use Illuminate\View\Component;
6
7
class MenuItem extends Component
8
{
9
    public $menu = [];
10
    public $hasTreeView = false;
11
    public $submenus = [];
12
    public $link = '';
13
    public $active = false;
14
15
    public function __construct($menu = [])
16
    {
17
        $this->setMenu($menu);
18
19
        $this->renderLink();
20
21
        $this->renderTreeView();
22
23
        $this->isActive();
24
    }
25
26
    public function setMenu(array $menu): void
27
    {
28
        if (!array_key_exists('link', $menu)) {
29
            $menu['link'] = [
30
                'type' => 'url',
31
                'value' => ''
32
            ];
33
        } else {
34
            if (!array_key_exists('type', $menu['link'])) {
35
                $menu['link']['type'] = 'url';
36
            }
37
            if (!array_key_exists('value', $menu['link'])) {
38
                $menu['link']['value'] = '';
39
            }
40
        }
41
        $this->menu = $menu;
42
    }
43
44
    public function renderLink(array $menu = null)
45
    {
46
        $external = !is_null($menu);
47
48
        $menu = $menu ?? $this->menu;
49
50
        if ($menu['link']['type'] == 'route') {
51
            if (is_array($menu['link']['value'])) {
52
                $link = route(current($menu['link']['value']), end($menu['link']['value']));
53
            } else {
54
                $link = route($menu['link']['value']);
55
            }
56
        } elseif ($menu['link']['type'] == 'url') {
57
            $link = url($menu['link']['value']);
58
        }
59
60
        if ($external) {
61
            return $link;
0 ignored issues
show
Bug introduced by
The variable $link 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...
62
        }
63
64
        $this->link = $link;
0 ignored issues
show
Documentation Bug introduced by
It seems like $link can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>. However, the property $link is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
65
    }
66
67
    public function renderTreeView()
68
    {
69
        $this->hasTreeView = (array_key_exists('submenus', $this->menu) && count($this->menu['submenus']) > 0);
70
71
        if ($this->hasTreeView) {
72
            $this->submenus = $this->menu['submenus'];
73
            $this->link = '';
74
        }
75
    }
76
77
    public function isActive()
78
    {
79
        $this->active = request()->fullUrl() == $this->link;
80
81
        $this->hasActiveSubmenu($this->menu);
82
    }
83
84
    public function hasActiveSubmenu($menu)
85
    {
86
        if (array_key_exists('submenus', $menu) && count($menu['submenus']) > 0) {
87
            foreach ($menu['submenus'] as $submenu) {
88
                if (request()->fullUrl() == $this->getLink($submenu)) {
89
                    $this->active = true;
90
                    break;
91
                }
92
                $this->hasActiveSubmenu($submenu);
93
            }
94
        }
95
    }
96
97
    public function getLink($menu)
98
    {
99
        if (
100
            array_key_exists('link', $menu) &&
101
            array_key_exists('value', $menu['link']) &&
102
            array_key_exists('type', $menu['link']) &&
103
            in_array($menu['link']['type'], ['url', 'route'])
104
        ) {
105
            $link = $this->renderLink($menu);
106
        }
107
108
        return $link ?? null;
109
    }
110
111
    public function render()
112
    {
113
        return view('template::lte.components._layouts.menu-item');
114
    }
115
}
116