Completed
Push — master ( 3afbe4...79027c )
by Oleg
07:17
created

Link::propertiesForSerialization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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