Completed
Push — master ( fbfcb9...4fd533 )
by Song
02:36
created

Content::title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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