Completed
Pull Request — master (#477)
by
unknown
02:27
created

Builder::addIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu;
4
5
use Illuminate\Support\Arr;
6
7
class Builder
8
{
9
    public $menu = [];
10
11
    /**
12
     * @var array
13
     */
14 21
    private $filters;
15
16 21
    public function __construct(array $filters = [])
17 21
    {
18
        $this->filters = $filters;
19 21
    }
20
21 21
    public function add()
22
    {
23 21
        $items = $this->transformItems(func_get_args());
24 21
25
        foreach ($items as $item) {
26 21
            array_push($this->menu, $item);
27
        }
28 21
    }
29
30 21
    public function addAfter($itemKey, ...$newItems)
31
    {
32
        $this->addItem($itemKey, 'after', ...$newItems);
33 21
    }
34
35 21
    public function addBefore($itemKey, ...$newItems)
36 1
    {
37
        $this->addItem($itemKey, 'before', ...$newItems);
38
    }
39 20
40 19
    public function addIn($itemKey, ...$newItems)
41
    {
42
        $this->addItem($itemKey, 'in', ...$newItems);
43 20
    }
44 2
45
    public function remove($itemKey)
46
    {
47 20
        $completeArrayPath = '';
48
        $itemPath = $this->findItem($itemKey, $this->menu);
49
        if (is_array($itemPath)) {
50
            foreach ($itemPath as $key => $value) {
51
                if ($completeArrayPath === '') {
52
                    $completeArrayPath .= "$value";
53
                } else {
54
                    $completeArrayPath .= ".submenu.$value";
55
                }
56
            }
57
            $itemPath = $completeArrayPath;
58
        }
59
        Arr::forget($this->menu, $itemPath);
60
    }
61
62
    public function itemKeyExists($itemKey)
63
    {
64
        if ($this->findItem($itemKey, $this->menu)) {
65
            return true;
66
        }
67
68
        return false;
69
    }
70
71
    public function transformItems($items)
72
    {
73
        return array_filter(array_map([$this, 'applyFilters'], $items));
74
    }
75
76
    protected function addItem($itemKey, $direction, ...$newItems)
77
    {
78
        $items = $this->transformItems($newItems);
79
        $position = $this->findItem($itemKey, $this->menu);
80
81
        if ($position !== null) {
82
            $completeArrayPath = $lastKey = '';
0 ignored issues
show
Unused Code introduced by
$lastKey is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$completeArrayPath is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
84
            if (is_array($position)) {
85
                $completeArrayPath = implode('.submenu.', $position);
86
                $lastKey = end($position);
87
            } else {
88
                $completeArrayPath = $lastKey = $position;
89
            }
90
91
            if ($direction == 'in' || ! is_array($position)) {
92
                $arrayPath = $completeArrayPath;
93
            } elseif (is_array($position)) {
94
                $arrayPath = substr($completeArrayPath, 0, -(strlen(".$lastKey")));
95
            }
96
97
            if ($position == $lastKey && $direction != 'in') {
98
                array_splice($this->menu, $position + ($direction == 'after' ? 1 : 0), 0, $items);
99
            } else {
100
                $menuItems = Arr::get($this->menu, $arrayPath);
0 ignored issues
show
Bug introduced by
The variable $arrayPath 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...
101
102
                if ($direction == 'in') {
103
                    if (! isset($menuItems['submenu'])) {
104
                        $menuItems['submenu'] = [];
105
                        $menuItems = $this->transformItems([$menuItems]);
106
                        $menuItems = $menuItems[0];
107
                        $menuItems['submenu'] = [];
108
                    }
109
110
                    $menuItems['submenu'] = array_merge($menuItems['submenu'], $items);
111
                } else {
112
                    array_splice($menuItems, $lastKey + ($direction == 'after' ? 1 : 0), 0, $items);
113
                }
114
115
                Arr::set($this->menu, $arrayPath, $menuItems);
116
            }
117
        }
118
    }
119
120
    protected function findItem($itemKey, $items, $childPositionOld = null)
121
    {
122
        if (is_array($childPositionOld)) {
123
            $childPositions = $childPositionOld;
124
        } else {
125
            $childPositions = [];
126
            if ($childPositionOld) {
127
                $childPositions[] = $childPositionOld;
128
            }
129
        }
130
        foreach ($items as $key => $item) {
131
            if (isset($item['key']) && $item['key'] == $itemKey) {
132
                if ($childPositionOld) {
133
                    $childPositions[] = $key;
134
135
                    return $childPositions;
136
                }
137
138
                return $key;
139
            } elseif (isset($item['submenu'])) {
140
                if ($childPositionOld) {
141
                    $childPositions[] = $key;
142
                    $childPosition = $this->findItem($itemKey, $item['submenu'], $childPositions);
143
                    $childPositions[] = $childPosition;
144
145
                    if (is_array($childPosition)) {
146
                        $childPositions = $childPosition;
147
                    }
148
149
                    return $childPositions;
150
                } else {
151
                    $newKey = $this->findItem($itemKey, $item['submenu']);
152
153
                    $childPositions[] = $key;
154
                    if (! is_array($newKey)) {
155
                        $childPositions[] = $newKey;
156
                    } else {
157
                        $childPositions = array_merge($childPositions, $newKey);
158
                    }
159
160
                    return $childPositions;
161
                }
162
            }
163
        }
164
    }
165
166
    protected function applyFilters($item)
167
    {
168
        if (is_string($item)) {
169
            return $item;
170
        }
171
172
        foreach ($this->filters as $filter) {
173
            $item = $filter->transform($item, $this);
174
        }
175
176
        return $item;
177
    }
178
}
179