Completed
Push — master ( d56194...c8d3eb )
by Oleg
11:26
created

SubMenu::getBuilder()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
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\Builder;
6
use Malezha\Menu\Contracts\ComparativeUrl;
7
use Malezha\Menu\Contracts\HasBuilder;
8
use Malezha\Menu\Contracts\MenuRender;
9
10
/**
11
 * Class SubMenu
12
 * @package Malezha\Menu\Element
13
 * 
14
 * @property-read Attributes $activeAttributes
15
 * @property-read Builder $builder
16
 * @inheritdoc
17
 */
18
class SubMenu extends Link implements HasBuilder
19
{
20
    /**
21
     * @var Builder
22
     */
23
    protected $builder;
24
25
    /**
26
     * SubMenu constructor.
27
     * @param string $title
28
     * @param string $url
29
     * @param Attributes $attributes
30
     * @param Attributes $activeAttributes
31
     * @param Attributes $linkAttributes
32
     * @param string $view
33
     * @param ComparativeUrl $comparativeUrl
34
     * @param MenuRender $render
35
     * @param Builder $builder
36
     */
37 6
    public function __construct($title, $url, Attributes $attributes, Attributes $activeAttributes,
38
                                Attributes $linkAttributes, $view, ComparativeUrl $comparativeUrl, 
39
                                MenuRender $render, Builder $builder)
40
    {
41 6
        parent::__construct($title, $url, $attributes, $activeAttributes, $linkAttributes, $view, $comparativeUrl, $render);
42
        
43 6
        $this->builder = $builder;
44 6
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 1
    public function getBuilder()
50
    {
51 1
        return $this->builder;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 6
    public function renderWith()
58
    {
59 6
        $renderView = func_get_arg(0);
60
61 6
        return array_merge(parent::renderWith(), [
62 6
            'builder' => $this->builder,
63 6
            'renderView' => $renderView,
64
        ]);
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 6
    public function render($view = null)
71
    {
72 6
        return $this->render->make($this->view)
73 6
            ->with($this->renderWith($view))
74 6
            ->render();
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80 2
    public function toArray()
81
    {
82 2
        return array_merge(parent::toArray(), [
83 2
            'builder' => $this->builder->toArray(),
84
        ]);
85
    }
86
87
    protected function propertiesForSerialization()
88
    {
89
        return array_merge(parent::propertiesForSerialization(), [
0 ignored issues
show
Best Practice introduced by
The expression return array_merge(paren...r' => $this->builder)); seems to be an array, but some of its elements' types (Malezha\Menu\Contracts\Builder) are incompatible with the return type of the parent method Malezha\Menu\Element\Lin...pertiesForSerialization of type array<string,string|Male...u\Contracts\Attributes>.

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...
90
            'builder' => $this->builder,
91
        ]);
92
    }
93
}