Completed
Pull Request — master (#3390)
by Edwin
03:05 queued 10s
created

Box::scrollable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170
        return [
171
            'title'      => $this->title,
172
            'content'    => $this->content,
173
            'tools'      => $this->tools,
174
            'attributes' => $this->formatAttributes(),
175
            'script'     => $this->script,
176
        ];
177
    }
178
179
    /**
180
     * Render box.
181
     *
182
     * @return string
183
     */
184
    public function render()
185
    {
186
        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...
187
    }
188
}
189