Completed
Push — master ( 363ead...e52e2c )
by Oleg
06:33
created

Item::isUrlEqual()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 6
1
<?php
2
namespace Malezha\Menu\Entity;
3
4
use Illuminate\Http\Request;
5
use Malezha\Menu\Contracts\Attributes as AttributesContract;
6
use Malezha\Menu\Contracts\Builder as BuilderContract;
7
use Malezha\Menu\Contracts\Link as LinkContract;
8
use Malezha\Menu\Contracts\Item as ItemContract;
9
use Malezha\Menu\Traits\DisplayRule;
10
use Malezha\Menu\Traits\HasAttributes;
11
use Malezha\Menu\Support\MergeAttributes;
12
13
/**
14
 * Class Item
15
 * @package Malezha\Menu\Entity
16
 */
17
class Item implements ItemContract
18
{
19
    use HasAttributes, DisplayRule;
20
21
    /**
22
     * @var LinkContract
23
     */
24
    protected $link;
25
26
    /**
27
     * @var BuilderContract
28
     */
29
    protected $builder;
30
31 10
    /**
32
     * @var Request
33 10
     */
34 10
    protected $request;
35 10
36 10
    /**
37 10
     * Item constructor.
38
     * @param BuilderContract $builder
39
     * @param AttributesContract $attributes
40
     * @param LinkContract $link
41
     * @param Request $request
42 5
     */
43
    public function __construct(BuilderContract $builder, AttributesContract $attributes, 
44 5
                                LinkContract $link, Request $request)
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...
45
    {
46
        $this->builder = $builder;
47
        $this->attributes = $attributes;
48
        $this->link = $link;
49
        $this->request = $request;
50
    }
51 3
52
    /**
53 3
     * @return Link
54 3
     */
55 3
    public function getLink()
56
    {
57 3
        return $this->link;
58
    }
59
60
    /**
61
     * @param array $attributes
62
     * @return string
63 3
     */
64
    public function buildAttributes($attributes = [])
65 3
    {
66 3
        $attributes = $this->isActive() ?
67
            (new MergeAttributes($this->builder->activeAttributes()->all(), $attributes))->merge() :
68 3
            $attributes;
69
70
        return $this->attributes->build($attributes);
71
    }
72
73
    /**
74
     * @return bool
75
     */
76
    protected function isActive()
77
    {
78
        $currentUrl = $this->request->url();
79
        $url = url($this->getLink()->getUrl());
80
        
81
        return $this->isUrlEqual($url, $currentUrl);
0 ignored issues
show
Bug introduced by
It seems like $url defined by url($this->getLink()->getUrl()) on line 79 can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Entity\Item::isUrlEqual() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
82
    }
83
84
    /**
85
     * Check is two url equal
86
     *
87
     * @param string $first
88
     * @param string $second
89
     * @return bool
90
     */
91
    protected function isUrlEqual($first, $second)
92
    {
93
        $uriForTrim = [
94
            '#',
95
            '/index',
96
            '/'
97
        ];
98
        
99
        foreach ($uriForTrim as $trim) {
100
            $first = rtrim($first, $trim);
101
            $second = rtrim($second, $trim);
102
        }
103
104
        return $first == $second;
105
    }
106
}