Completed
Push — master ( 782d0f...3c99c3 )
by Song
02:55
created

Content::withInfo()   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 2
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
8
class Content implements Renderable
9
{
10
    /**
11
     * Content header.
12
     *
13
     * @var string
14
     */
15
    protected $header = '';
16
17
    /**
18
     * Content description.
19
     *
20
     * @var string
21
     */
22
    protected $description = '';
23
24
    /**
25
     * Page breadcrumb.
26
     *
27
     * @var array
28
     */
29
    protected $breadcrumb = [];
30
31
    /**
32
     * @var Row[]
33
     */
34
    protected $rows = [];
35
36
    /**
37
     * Content constructor.
38
     *
39
     * @param Closure|null $callback
40
     */
41
    public function __construct(\Closure $callback = null)
42
    {
43
        if ($callback instanceof Closure) {
44
            $callback($this);
45
        }
46
    }
47
48
    /**
49
     * Set header of content.
50
     *
51
     * @param string $header
52
     *
53
     * @return $this
54
     */
55
    public function header($header = '')
56
    {
57
        $this->header = $header;
58
59
        return $this;
60
    }
61
62
    /**
63
     * Set description of content.
64
     *
65
     * @param string $description
66
     *
67
     * @return $this
68
     */
69
    public function description($description = '')
70
    {
71
        $this->description = $description;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Set breadcrumb of content.
78
     *
79
     * @param array ...$breadcrumb
80
     *
81
     * @return $this
82
     */
83
    public function breadcrumb(...$breadcrumb)
84
    {
85
        $this->validateBreadcrumb($breadcrumb);
86
87
        $this->breadcrumb = (array) $breadcrumb;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Validate content breadcrumb.
94
     *
95
     * @param array $breadcrumb
96
     *
97
     * @throws \Exception
98
     *
99
     * @return bool
100
     */
101
    protected function validateBreadcrumb(array $breadcrumb)
102
    {
103
        foreach ($breadcrumb as $item) {
104
            if (!is_array($item) || !array_has($item, 'text')) {
105
                throw new  \Exception('Breadcrumb format error!');
106
            }
107
        }
108
109
        return true;
110
    }
111
112
    /**
113
     * Alias of method row.
114
     *
115
     * @param mixed $content
116
     *
117
     * @return Content
118
     */
119
    public function body($content)
120
    {
121
        return $this->row($content);
122
    }
123
124
    /**
125
     * Add one row for content body.
126
     *
127
     * @param $content
128
     *
129
     * @return $this
130
     */
131
    public function row($content)
132
    {
133
        if ($content instanceof Closure) {
134
            $row = new Row();
135
            call_user_func($content, $row);
136
            $this->addRow($row);
137
        } else {
138
            $this->addRow(new Row($content));
139
        }
140
141
        return $this;
142
    }
143
144
    /**
145
     * Add Row.
146
     *
147
     * @param Row $row
148
     */
149
    protected function addRow(Row $row)
150
    {
151
        $this->rows[] = $row;
152
    }
153
154
    /**
155
     * Build html of content.
156
     *
157
     * @return string
158
     */
159
    public function build()
160
    {
161
        ob_start();
162
163
        foreach ($this->rows as $row) {
164
            $row->build();
165
        }
166
167
        $contents = ob_get_contents();
168
169
        ob_end_clean();
170
171
        return $contents;
172
    }
173
174
    /**
175
     * Set success message for content.
176
     *
177
     * @param string $title
178
     * @param string $message
179
     *
180
     * @return $this
181
     */
182
    public function withSuccess($title = '', $message = '')
183
    {
184
        admin_success($title, $message);
185
186
        return $this;
187
    }
188
189
    /**
190
     * Set error message for content.
191
     *
192
     * @param string $title
193
     * @param string $message
194
     *
195
     * @return $this
196
     */
197
    public function withError($title = '', $message = '')
198
    {
199
        admin_error($title, $message);
200
201
        return $this;
202
    }
203
204
    /**
205
     * Set warning message for content.
206
     *
207
     * @param string $title
208
     * @param string $message
209
     *
210
     * @return $this
211
     */
212
    public function withWarning($title = '', $message = '')
213
    {
214
        admin_warning($title, $message);
215
216
        return $this;
217
    }
218
219
    /**
220
     * Set info message for content.
221
     *
222
     * @param string $title
223
     * @param string $message
224
     *
225
     * @return $this
226
     */
227
    public function withInfo($title = '', $message = '')
228
    {
229
        admin_info($title, $message);
230
231
        return $this;
232
    }
233
234
    /**
235
     * Render this content.
236
     *
237
     * @return string
238
     */
239
    public function render()
240
    {
241
        $items = [
242
            'header'      => $this->header,
243
            'description' => $this->description,
244
            'breadcrumb'  => $this->breadcrumb,
245
            'content'     => $this->build(),
246
        ];
247
248
        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...
249
    }
250
}
251