Completed
Pull Request — master (#3)
by ARCANEDEV
04:19
created

Manager::hide()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
cc 1
nc 1
nop 0
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php namespace Arcanesoft\Sidebar;
2
3
use Arcanesoft\Sidebar\Contracts\Manager as ManagerContract;
4
use function foo\func;
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
8
/**
9
 * Class     Manager
10
 *
11
 * @package  Arcanesoft\Sidebar
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class Manager implements ManagerContract
15
{
16
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
22
    /** @var  \Arcanesoft\Foundation\Helpers\Sidebar\SidebarCollection */
23
    protected $items;
24
25
    protected $shown = true;
26
27
    /* -----------------------------------------------------------------
28
     |  Constructor
29
     | -----------------------------------------------------------------
30
     */
31
32 24
    public function __construct()
33
    {
34 24
        $this->items = new SidebarCollection;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Arcanesoft\Sidebar\SidebarCollection() of type object<Arcanesoft\Sidebar\SidebarCollection> is incompatible with the declared type object<Arcanesoft\Founda...ebar\SidebarCollection> of property $items.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35 24
    }
36
37
    /* -----------------------------------------------------------------
38
     |  Getters & Setters
39
     | -----------------------------------------------------------------
40
     */
41
42
    /**
43
     * Set the selected item.
44
     *
45
     * @param  string  $name
46
     *
47
     * @return $this
48
     */
49 4
    public function setSelectedItem(string $name)
50
    {
51 4
        $this->items->setSelected($name);
52
53 4
        return $this;
54
    }
55
56
    /**
57
     * Get the sidebar items.
58
     *
59
     * @return \Arcanesoft\Sidebar\SidebarCollection
60
     */
61 20
    public function items()
62
    {
63 20
        return $this->items;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->items; (Arcanesoft\Foundation\He...debar\SidebarCollection) is incompatible with the return type declared by the interface Arcanesoft\Sidebar\Contracts\Manager::items of type Arcanesoft\Sidebar\Entities\ItemCollection.

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 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('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...
64
    }
65
66
    /* -----------------------------------------------------------------
67
     |  Main Methods
68
     | -----------------------------------------------------------------
69
     */
70
71
    /**
72
     * Load the sidebar items from config files.
73
     *
74
     * @param  array  $items
75
     *
76
     * @return $this
77
     */
78 4
    public function loadFromArray(array $items)
79
    {
80 4
        foreach ($items as $item) {
81 4
            if (is_array($item) && ! empty($item)) {
82 4
                $this->add($item);
83
            }
84
        }
85
86 4
        return $this;
87
    }
88
89
    /**
90
     * Load sidebar items from config file(s).
91
     *
92
     * @param  string  $key
93
     *
94
     * @return $this
95
     */
96 8
    public function loadFromConfig($key)
97
    {
98 8
        $items = new Collection(config()->get($key, []));
99
100 8
        if ($items->isEmpty())
101
            return $this;
102
103
        $allHasConfig = $items->every(function ($item) {
104 8
            return config()->has($item);
105 8
        });
106
107 8
        if ($allHasConfig) {
108
            $items->each(function ($item) {
109 8
                $this->loadFromConfig($item);
110 8
            });
111
        }
112
        else {
113 8
            $this->add($items->toArray());
114
        }
115
116 8
        return $this;
117
    }
118
119
    /**
120
     * Add an item from array.
121
     *
122
     * @param  array  $item
123
     *
124
     * @return $this
125
     */
126 16
    public function add(array $item)
127
    {
128 16
        $this->items->pushItem($item);
129
130 16
        return $this;
131
    }
132
133
    /**
134
     * Show the sidebar.
135
     *
136
     * @return $this
137
     */
138 4
    public function show()
139
    {
140 4
        $this->shown = true;
141
142 4
        return $this;
143
    }
144
145
    /**
146
     * Hide the sidebar.
147
     *
148
     * @return $this
149
     */
150 4
    public function hide()
151
    {
152 4
        $this->shown = false;
153
154 4
        return $this;
155
    }
156
157
    /* -----------------------------------------------------------------
158
     |  Check Methods
159
     | -----------------------------------------------------------------
160
     */
161
162
    /**
163
     * Check if the sidebar has items.
164
     *
165
     * @return bool
166
     */
167 12
    public function hasItems()
168
    {
169 12
        return $this->items->isNotEmpty();
170
    }
171
172
    /**
173
     * Check if the sidebar is shown.
174
     *
175
     * @return bool
176
     */
177 4
    public function isShown()
178
    {
179 4
        return $this->shown === true;
180
    }
181
}
182