Completed
Pull Request — master (#131)
by
unknown
03:08
created

ActiveChecker::checkExact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Http\Request;
7
use Illuminate\Contracts\Routing\UrlGenerator;
8
9
class ActiveChecker
10
{
11
    private $request;
12
13
    private $url;
14
15 32
    public function __construct(Request $request, UrlGenerator $url)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
16
    {
17 32
        $this->request = $request;
18 32
        $this->url = $url;
19 32
    }
20
21 30
    public function isActive($item)
22
    {
23 30
        if (isset($item['active'])) {
24 3
            return $this->isExplicitActive($item['active']);
25
        }
26
27 27
        if (isset($item['submenu'])) {
28 8
            return $this->containsActive($item['submenu']);
29
        }
30
31 24
        if (isset($item['href'])) {
32 14
            return $this->checkPattern($item['href']);
33
        }
34
35
        // Support URL for backwards compatibility
36 13
        if (isset($item['url'])) {
37 12
            return $this->checkPattern($item['url']);
38
        }
39
40 1
        return false;
41
    }
42
43 23
    protected function checkPattern($pattern)
44
    {
45 23
        $fullUrlPattern = $this->url->to($pattern);
46
47
        $fullUrl = $this->request->fullUrl();
48 26
49
        return Str::is($fullUrlPattern, $fullUrl);
50 26
    }
51
52
    protected function containsActive($items)
53 11
    {
54
        foreach ($items as $item) {
55 11
            if ($this->isActive($item)) {
56
                return true;
57
            }
58 26
        }
59
60 26
        return false;
61
    }
62 26
63
    private function isExplicitActive($active)
64 26
    {
65
        foreach ($active as $url) {
66
            if ($this->checkExact($url)) {
0 ignored issues
show
Bug introduced by
The method checkExact() does not seem to exist on object<JeroenNoten\Larav...Lte\Menu\ActiveChecker>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67 8
                return true;
68
            }
69 8
        }
70 5
71 3
        return false;
72
    }
73
}
74