Completed
Push — master ( 69a7c6...5199b6 )
by Song
02:55
created

Content::dump()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Layout;
4
5
use Closure;
6
use Illuminate\Contracts\Support\Renderable;
7
use Illuminate\Support\Arr;
8
9
class Content implements Renderable
10
{
11
    /**
12
     * Content title.
13
     *
14
     * @var string
15
     */
16
    protected $title = ' ';
17
18
    /**
19
     * Content description.
20
     *
21
     * @var string
22
     */
23
    protected $description = ' ';
24
25
    /**
26
     * Page breadcrumb.
27
     *
28
     * @var array
29
     */
30
    protected $breadcrumb = [];
31
32
    /**
33
     * @var Row[]
34
     */
35
    protected $rows = [];
36
37
    /**
38
     * Content constructor.
39
     *
40
     * @param Closure|null $callback
41
     */
42
    public function __construct(\Closure $callback = null)
43
    {
44
        if ($callback instanceof Closure) {
45
            $callback($this);
46
        }
47
    }
48
49
    /**
50
     * Alias of method `title`.
51
     *
52
     * @param string $header
53
     *
54
     * @return $this
55
     */
56
    public function header($header = '')
57
    {
58
        return $this->title($header);
59
    }
60
61
    /**
62
     * @param string $title
63
     *
64
     * @return $this
65
     */
66
    public function title($title)
67
    {
68
        $this->title = $title;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Set description of content.
75
     *
76
     * @param string $description
77
     *
78
     * @return $this
79
     */
80
    public function description($description = '')
81
    {
82
        $this->description = $description;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Set breadcrumb of content.
89
     *
90
     * @param array ...$breadcrumb
91
     *
92
     * @return $this
93
     */
94
    public function breadcrumb(...$breadcrumb)
95
    {
96
        $this->validateBreadcrumb($breadcrumb);
97
98
        $this->breadcrumb = (array) $breadcrumb;
99
100
        return $this;
101
    }
102
103
    /**
104
     * Validate content breadcrumb.
105
     *
106
     * @param array $breadcrumb
107
     *
108
     * @throws \Exception
109
     *
110
     * @return bool
111
     */
112
    protected function validateBreadcrumb(array $breadcrumb)
113
    {
114
        foreach ($breadcrumb as $item) {
115
            if (!is_array($item) || !Arr::has($item, 'text')) {
116
                throw new  \Exception('Breadcrumb format error!');
117
            }
118
        }
119
120
        return true;
121
    }
122
123
    /**
124
     * Alias of method row.
125
     *
126
     * @param mixed $content
127
     *
128
     * @return Content
129
     */
130
    public function body($content)
131
    {
132
        return $this->row($content);
133
    }
134
135
    /**
136
     * Add one row for content body.
137
     *
138
     * @param $content
139
     *
140
     * @return $this
141
     */
142
    public function row($content)
143
    {
144
        if ($content instanceof Closure) {
145
            $row = new Row();
146
            call_user_func($content, $row);
147
            $this->addRow($row);
148
        } else {
149
            $this->addRow(new Row($content));
150
        }
151
152
        return $this;
153
    }
154
155
    /**
156
     * Render giving view as content body.
157
     *
158
     * @param string $view
159
     * @param array  $data
160
     *
161
     * @return Content
162
     */
163
    public function view($view, $data)
164
    {
165
        return $this->body(view($view, $data));
166
    }
167
168
    /**
169
     * @param $var
170
     * @return Content
171
     */
172
    public function dump($var)
0 ignored issues
show
Unused Code introduced by
The parameter $var is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
173
    {
174
        return $this->row(admin_dump(...func_get_args()));
175
    }
176
177
    /**
178
     * Add Row.
179
     *
180
     * @param Row $row
181
     */
182
    protected function addRow(Row $row)
183
    {
184
        $this->rows[] = $row;
185
    }
186
187
    /**
188
     * Build html of content.
189
     *
190
     * @return string
191
     */
192
    public function build()
193
    {
194
        ob_start();
195
196
        foreach ($this->rows as $row) {
197
            $row->build();
198
        }
199
200
        $contents = ob_get_contents();
201
202
        ob_end_clean();
203
204
        return $contents;
205
    }
206
207
    /**
208
     * Set success message for content.
209
     *
210
     * @param string $title
211
     * @param string $message
212
     *
213
     * @return $this
214
     */
215
    public function withSuccess($title = '', $message = '')
216
    {
217
        admin_success($title, $message);
218
219
        return $this;
220
    }
221
222
    /**
223
     * Set error message for content.
224
     *
225
     * @param string $title
226
     * @param string $message
227
     *
228
     * @return $this
229
     */
230
    public function withError($title = '', $message = '')
231
    {
232
        admin_error($title, $message);
233
234
        return $this;
235
    }
236
237
    /**
238
     * Set warning message for content.
239
     *
240
     * @param string $title
241
     * @param string $message
242
     *
243
     * @return $this
244
     */
245
    public function withWarning($title = '', $message = '')
246
    {
247
        admin_warning($title, $message);
248
249
        return $this;
250
    }
251
252
    /**
253
     * Set info message for content.
254
     *
255
     * @param string $title
256
     * @param string $message
257
     *
258
     * @return $this
259
     */
260
    public function withInfo($title = '', $message = '')
261
    {
262
        admin_info($title, $message);
263
264
        return $this;
265
    }
266
267
    /**
268
     * Render this content.
269
     *
270
     * @return string
271
     */
272
    public function render()
273
    {
274
        $items = [
275
            'header'      => $this->title,
276
            'description' => $this->description,
277
            'breadcrumb'  => $this->breadcrumb,
278
            'content'     => $this->build(),
279
        ];
280
281
        return view('admin::content', $items)->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...
282
    }
283
}
284