Completed
Push — master ( e52e2c...9baf6c )
by Oleg
08:13
created

Item   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 7
c 3
b 1
f 0
lcom 1
cbo 7
dl 0
loc 90
ccs 26
cts 26
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLink() 0 4 1
A __construct() 0 8 1
A buildAttributes() 0 8 2
A isActive() 0 7 1
A isUrlEqual() 0 15 2
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
    /**
32
     * @var Request
33
     */
34
    protected $request;
35
36
    /**
37
     * Item constructor.
38
     * @param BuilderContract $builder
39
     * @param AttributesContract $attributes
40
     * @param LinkContract $link
41
     * @param Request $request
42
     */
43 10
    public function __construct(BuilderContract $builder, AttributesContract $attributes, 
44
                                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 10
        $this->builder = $builder;
47 10
        $this->attributes = $attributes;
48 10
        $this->link = $link;
49 10
        $this->request = $request;
50 10
    }
51
52
    /**
53
     * @return Link
54
     */
55 6
    public function getLink()
56
    {
57 6
        return $this->link;
58
    }
59
60
    /**
61
     * @param array $attributes
62
     * @return string
63
     */
64 4
    public function buildAttributes($attributes = [])
65
    {
66 4
        $attributes = $this->isActive() ?
67 4
            (new MergeAttributes($this->builder->activeAttributes()->all(), $attributes))->merge() :
68 4
            $attributes;
69
70 4
        return $this->attributes->build($attributes);
71
    }
72
73
    /**
74
     * @return bool
75
     */
76 4
    protected function isActive()
77
    {
78 4
        $currentUrl = $this->request->url();
79 4
        $url = url($this->getLink()->getUrl());
80
        
81 4
        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 4
    protected function isUrlEqual($first, $second)
92
    {
93
        $uriForTrim = [
94 4
            '#',
95 4
            '/index',
96
            '/'
97 4
        ];
98
        
99 4
        foreach ($uriForTrim as $trim) {
100 4
            $first = rtrim($first, $trim);
101 4
            $second = rtrim($second, $trim);
102 4
        }
103
104 4
        return $first == $second;
105
    }
106
}