Text::renderWith()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 1
1
<?php
2
namespace Malezha\Menu\Element;
3
4
use Malezha\Menu\Contracts\Attributes;
5
use Malezha\Menu\Contracts\DisplayRule as DisplayRuleInterface;
6
use Malezha\Menu\Contracts\HasAttributes as HasAttributesInterface;
7
use Malezha\Menu\Contracts\MenuRender;
8
use Malezha\Menu\Traits\DisplayRule;
9
use Malezha\Menu\Traits\HasAttributes;
10
11
/**
12
 * Class Text
13
 * @package Malezha\Menu\Element
14
 * 
15
 * @property string $text
16
 * @property bool|callable $displayRule
17
 * @property-read Attributes $attributes
18
 */
19
class Text extends AbstractElement implements DisplayRuleInterface, HasAttributesInterface
20
{
21
    use DisplayRule, HasAttributes;
22
    
23
    /**
24
     * @var string
25
     */
26
    protected $text;
27
28
    /**
29
     * Text constructor.
30
     * @param $text
31
     * @param Attributes $attributes
32
     * @param $view
33
     * @param MenuRender $render
34
     */
35 8
    public function __construct($text, Attributes $attributes, $view, MenuRender $render)
36
    {
37 8
        $this->text = $text;
38 8
        $this->render = $render;
39 8
        $this->view = $view;
40 8
        $this->attributes = $attributes;
41 8
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46 3
    public function render($view = null)
47
    {
48 3
        return $this->render->make($this->view)
49 3
            ->with($this->renderWith())
50 3
            ->render();
51
    }
52
53
    /**
54
     * @return array
55
     */
56 3
    protected function renderWith()
57
    {
58
        return [
59 3
            'text' => $this->text,
60 3
            'canDisplay' => $this->canDisplay(),
61 3
            'attributes' => $this->buildAttributes(),
62
        ];
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68 2
    public function toArray()
69
    {
70 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...
71 2
            'text' => $this->text,
72 2
            'attributes' => $this->attributes->toArray(),
73 2
            'displayRule' => $this->canDisplay(),
74
        ]);
75
    }
76
}