Completed
Push — master ( e84808...5a9d2e )
by Oleg
07:58
created

SubMenu   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 75
ccs 21
cts 21
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getBuilder() 0 4 1
A renderWith() 0 9 1
A render() 0 6 1
A toArray() 0 6 1
A propertiesForSerialization() 0 6 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\HasBuilder;
7
use Malezha\Menu\Contracts\MenuRender;
8
9
/**
10
 * Class SubMenu
11
 * @package Malezha\Menu\Element
12
 * 
13
 * @property-read Attributes $activeAttributes
14
 * @property-read Builder $builder
15
 * @inheritdoc
16
 */
17
class SubMenu extends Link implements HasBuilder
18
{
19
    /**
20
     * @var Builder
21
     */
22
    protected $builder;
23
24
    /**
25
     * SubMenu constructor.
26
     * @param string $title
27
     * @param string $url
28
     * @param Attributes $attributes
29
     * @param Attributes $activeAttributes
30
     * @param Attributes $linkAttributes
31
     * @param string $view
32
     * @param string $currentUrl
33
     * @param MenuRender $render
34
     * @param Builder $builder
35
     */
36 7
    public function __construct($title, $url, Attributes $attributes, Attributes $activeAttributes,
37
                                Attributes $linkAttributes, $view, $currentUrl, MenuRender $render, Builder $builder)
38
    {
39 7
        parent::__construct($title, $url, $attributes, $activeAttributes, $linkAttributes, $view, $currentUrl, $render);
40
        
41 7
        $this->builder = $builder;
42 7
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47 1
    public function getBuilder()
48
    {
49 1
        return $this->builder;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 4
    public function renderWith()
56
    {
57 4
        $renderView = func_get_arg(0);
58
59 4
        return array_merge(parent::renderWith(), [
60 4
            'builder' => $this->builder,
61 4
            'renderView' => $renderView,
62
        ]);
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 4
    public function render($view = null)
69
    {
70 4
        return $this->render->make($this->view)
71 4
            ->with($this->renderWith($view))
72 4
            ->render();
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78 2
    public function toArray()
79
    {
80 2
        return array_merge(parent::toArray(), [
81 2
            'builder' => $this->builder->toArray(),
82
        ]);
83
    }
84
85 1
    protected function propertiesForSerialization()
86
    {
87 1
        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...
88 1
            'builder' => $this->builder,
89
        ]);
90
    }
91
}