Completed
Push — master ( 4895be...67409c )
by Oleg
04:41
created

Link::isUrlEqual()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
namespace Malezha\Menu\Element;
3
4
use Illuminate\Container\Container;
5
use Illuminate\Http\Request;
6
use Malezha\Menu\Contracts\Attributes;
7
use Malezha\Menu\Contracts\ComparativeUrl;
8
use Malezha\Menu\Contracts\DisplayRule as DisplayRuleInterface;
9
use Malezha\Menu\Contracts\HasAttributes as HasAttributesInterface;
10
use Malezha\Menu\Contracts\HasActiveAttributes as HasActiveAttributesInterface;
11
use Malezha\Menu\Contracts\MenuRender;
12
use Malezha\Menu\Support\MergeAttributes;
13
use Malezha\Menu\Traits\DisplayRule;
14
use Malezha\Menu\Traits\HasActiveAttributes;
15
use Malezha\Menu\Traits\HasAttributes;
16
17
/**
18
 * Class Link
19
 * @package Malezha\Menu\Element
20
 *
21
 * @property string $title
22
 * @property string $url
23
 * @property-read Attributes $attributes
24
 * @property-read Attributes $linkAttributes
25
 * @property-read Attributes $activeAttributes
26
 * @property bool|callable $displayRule
27
 */
28
class Link extends AbstractElement implements DisplayRuleInterface, HasAttributesInterface, HasActiveAttributesInterface
29
{
30
    use DisplayRule, HasAttributes, HasActiveAttributes;
31
32
    /**
33
     * @var string
34
     */
35
    protected $title;
36
37
    /**
38
     * @var string
39
     */
40
    protected $url;
41
42
    /**
43
     * @var Attributes
44
     */
45
    protected $linkAttributes;
46
47
    /**
48
     * @var ComparativeUrl
49
     */
50
    protected $comparativeUrl;
51
52
    /**
53
     * Link constructor.
54
     * @param string $title
55
     * @param string $url
56
     * @param Attributes $attributes
57
     * @param Attributes $activeAttributes
58
     * @param Attributes $linkAttributes
59
     * @param string $view
60
     * @param ComparativeUrl $comparativeUrl
61
     * @param MenuRender $render
62
     */
63 19
    public function __construct($title, $url, Attributes $attributes, Attributes $activeAttributes, 
64
                                Attributes $linkAttributes, $view, ComparativeUrl $comparativeUrl, MenuRender $render)
65
    {
66 19
        $this->title = $title;
67 19
        $this->url = $url;
68 19
        $this->attributes = $attributes;
69 19
        $this->activeAttributes = $activeAttributes;
70 19
        $this->linkAttributes = $linkAttributes;
71 19
        $this->view = $view;
72 19
        $this->comparativeUrl = $comparativeUrl;
73 19
        $this->render = $render;
74 19
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79 8
    public function render($view = null)
80
    {
81 8
        return $this->render->make($this->view)
82 8
            ->with($this->renderWith())
83 8
            ->render();
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89 8
    public function buildAttributes($attributes = [])
90
    {
91 8
        $attributes = $this->comparativeUrl->isEquals($this->url) ?
92 8
            (new MergeAttributes($this->activeAttributes->all(), $attributes))->merge() : $attributes;
93
94 8
        return $this->attributes->build($attributes);
95
    }
96
97
    /**
98
     * @return array
99
     */
100 8
    protected function renderWith()
101
    {
102
        return [
103 8
            'title' => $this->title,
104 8
            'url' => $this->url,
105 8
            'attributes' => $this->buildAttributes(),
106 8
            'linkAttributes' => $this->linkAttributes->build(),
107 8
            'canDisplay' => $this->canDisplay(),
108
            'renderView' => null,
109
        ];
110
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115 2 View Code Duplication
    public function toArray()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117 2
        return array_merge(parent::toArray(), [
0 ignored issues
show
Best Practice introduced by
The expression return array_merge(paren... $this->canDisplay())); seems to be an array, but some of its elements' types (array|boolean) are incompatible with the return type of the parent method Malezha\Menu\Element\AbstractElement::toArray of type array<string,string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
118 2
            'title' => $this->title,
119 2
            'url' => $this->url,
120 2
            'attributes' => $this->attributes->toArray(),
121 2
            'activeAttributes' => $this->activeAttributes->toArray(),
122 2
            'linkAttributes' => $this->linkAttributes->toArray(),
123 2
            'displayRule' => $this->canDisplay(),
124
        ]);
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130 2
    public function unserialize($serialized)
131
    {
132 2
        $this->comparativeUrl = Container::getInstance()->make(ComparativeUrl::class);
133
134 2
        parent::unserialize($serialized);
135 2
    }
136
137 2 View Code Duplication
    protected function propertiesForSerialization()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139 2
        return array_merge(parent::propertiesForSerialization(), [
0 ignored issues
show
Best Practice introduced by
The expression return array_merge(paren...his->serializeRule())); seems to be an array, but some of its elements' types (Malezha\Menu\Contracts\Attributes) are incompatible with the return type of the parent method Malezha\Menu\Element\Abs...pertiesForSerialization of type array<string,string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
140 2
            'title' => $this->title,
141 2
            'url' => $this->url,
142 2
            'attributes' => $this->attributes,
143 2
            'activeAttributes' => $this->activeAttributes,
144 2
            'linkAttributes' => $this->linkAttributes,
145 2
            'displayRule' => $this->serializeRule(),
146
        ]);
147
    }
148
}