Completed
Pull Request — master (#1350)
by
unknown
02:44
created

Box   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 169
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A content() 0 10 2
A addTools() 0 6 1
A title() 0 6 1
A collapsable() 0 7 1
A removable() 0 7 1
A style() 0 14 2
A solid() 0 4 1
A variables() 0 9 1
A render() 0 4 1
1
<?php
2
3
namespace Encore\Admin\Widgets;
4
5
use Illuminate\Contracts\Support\Renderable;
6
7
class Box extends Widget implements Renderable
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $view = 'admin::widgets.box';
13
14
    /**
15
     * @var string
16
     */
17
    protected $title = 'Box header';
18
19
    /**
20
     * @var string
21
     */
22
    protected $content = 'here is the box content.';
23
24
    /**
25
     * @var array
26
     */
27
    protected $tools = [];
28
29
    /**
30
     * Box constructor.
31
     *
32
     * @param string $title
33
     * @param string $content
34
     */
35
    public function __construct($title = '', $content = '')
36
    {
37
        if ($title) {
38
            $this->title($title);
39
        }
40
41
        if ($content) {
42
            $this->content($content);
43
        }
44
45
        $this->class('box');
46
    }
47
48
    /**
49
     * Set box content.
50
     *
51
     * @param string $content
52
     *
53
     * @return $this
54
     */
55
    public function content($content)
56
    {
57
        if ($content instanceof Renderable) {
58
            $this->content = $content->render();
59
        } else {
60
            $this->content = (string) $content;
61
        }
62
63
        return $this;
64
    }
65
    /**
66
     * add tools .
67
     *
68
     * @param $content
69
     * @return $this
70
     * @internal param string $title
71
     *
72
     */
73
    public function addTools($content)
74
    {
75
        $this->tools[] = $content;
76
77
        return $this;
78
    }
79
    /**
80
     * Set box title.
81
     *
82
     * @param string $title
83
     *
84
     * @return $this
85
     */
86
    public function title($title)
87
    {
88
        $this->title = $title;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Set box as collapsable.
95
     *
96
     * @return $this
97
     */
98
    public function collapsable()
99
    {
100
        $this->tools[] =
101
            '<button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>';
102
103
        return $this;
104
    }
105
106
    /**
107
     * Set box as removable.
108
     *
109
     * @return $this
110
     */
111
    public function removable()
112
    {
113
        $this->tools[] =
114
            '<button class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>';
115
116
        return $this;
117
    }
118
119
    /**
120
     * Set box style.
121
     *
122
     * @param string $styles
123
     *
124
     * @return $this|Box
125
     */
126
    public function style($styles)
127
    {
128
        if (is_string($styles)) {
129
            return $this->style([$styles]);
130
        }
131
132
        $styles = array_map(function ($style) {
133
            return 'box-'.$style;
134
        }, $styles);
135
136
        $this->class = $this->class.' '.implode(' ', $styles);
137
138
        return $this;
139
    }
140
141
    /**
142
     * Add `box-solid` class to box.
143
     *
144
     * @return $this
145
     */
146
    public function solid()
147
    {
148
        return $this->style('solid');
149
    }
150
151
    /**
152
     * Variables in view.
153
     *
154
     * @return array
155
     */
156
    protected function variables()
157
    {
158
        return [
159
            'title'      => $this->title,
160
            'content'    => $this->content,
161
            'tools'      => $this->tools,
162
            'attributes' => $this->formatAttributes(),
163
        ];
164
    }
165
166
    /**
167
     * Render box.
168
     *
169
     * @return string
170
     */
171
    public function render()
172
    {
173
        return view($this->view, $this->variables())->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
174
    }
175
}
176