Completed
Pull Request — master (#20)
by Paul
02:06
created

Html::ensureModelIsAvailable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace Spatie\Html;
4
5
use Spatie\Html\Elements\A;
6
use Illuminate\Http\Request;
7
use Spatie\Html\Elements\Div;
8
use Spatie\Html\Elements\Form;
9
use Spatie\Html\Elements\Span;
10
use Spatie\Html\Elements\Input;
11
use Spatie\Html\Elements\Label;
12
use Spatie\Html\Elements\Button;
13
use Spatie\Html\Elements\Legend;
14
use Spatie\Html\Elements\Option;
15
use Spatie\Html\Elements\Select;
16
use Spatie\Html\Elements\Element;
17
use Illuminate\Support\Collection;
18
use Illuminate\Support\HtmlString;
19
use Spatie\Html\Elements\Fieldset;
20
use Spatie\Html\Elements\Textarea;
21
use Illuminate\Contracts\Support\Htmlable;
22
23
class Html
24
{
25
    /** @var \Illuminate\Http\Request */
26
    protected $request;
27
28
    /** @var \ArrayAccess|array */
29
    protected $model;
30
31
    public function __construct(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
32
    {
33
        $this->request = $request;
34
    }
35
36
    /**
37
     * @param string $href
38
     * @param string $text
0 ignored issues
show
Bug introduced by
There is no parameter named $text. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
39
     *
40
     * @return \Spatie\Html\Elements\A
41
     */
42
    public function a(?string $href = '', ?string $contents = '')
43
    {
44
        return A::create()
45
            ->attributeIf($href, 'href', $href)
0 ignored issues
show
Documentation introduced by
$href is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
            ->html($contents);
47
    }
48
49
    /**
50
     * @param string $type
51
     * @param string $text
0 ignored issues
show
Bug introduced by
There is no parameter named $text. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
52
     *
53
     * @return \Spatie\Html\Elements\Button
54
     */
55
    public function button(?string $contents = '', ?string $type = '')
56
    {
57
        return Button::create()
58
            ->attributeIf($type, 'type', $type)
0 ignored issues
show
Documentation introduced by
$type is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
            ->html($contents);
60
    }
61
62
    /**
63
     * @param string|array|\Illuminate\Support\Collection $classes
64
     *
65
     * @return \Illuminate\Contracts\Support\Htmlable
66
     */
67
    public function class($classes): Htmlable
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
68
    {
69
        if ($classes instanceof Collection) {
70
            $classes = $classes->toArray();
71
        }
72
73
        $attributes = new Attributes();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $attributes.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
74
        $attributes->addClass($classes);
0 ignored issues
show
Bug introduced by
It seems like $classes can also be of type array; however, Spatie\Html\Attributes::addClass() does only seem to accept string|object<Spatie\Html\iterable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $attributes.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
75
76
        return new HtmlString(
77
            $attributes->render()
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $attributes.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
78
        );
79
    }
80
81
    /**
82
     * @param string $value
83
     *
84
     * @return \Spatie\Html\Elements\Input
85
     */
86
    public function checkbox(string $name = '', ?bool $checked = false, ?string $value = '1')
87
    {
88
        return $this->input('checkbox', $name, $value)
89
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
0 ignored issues
show
Documentation introduced by
$checked is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
    }
91
92
    /**
93
     * @param \Spatie\Html\HtmlElement|string $contents
94
     *
95
     * @return \Spatie\Html\Elements\Div
96
     */
97
    public function div($contents = null)
98
    {
99
        return Div::create()->children($contents);
100
    }
101
102
    /**
103
     * @param string $value
104
     *
105
     * @return \Spatie\Html\Elements\Input
106
     */
107
    public function email(string $name = '', ?string $value = '')
108
    {
109
        return $this->input('email', $name, $value);
110
    }
111
112
    /**
113
     * @param string $tag
114
     *
115
     * @return \Spatie\Html\Elements\Element
116
     */
117
    public function element(string $tag)
118
    {
119
        return Element::withTag($tag);
120
    }
121
122
    /**
123
     * @param string $type
124
     * @param string $name
125
     * @param string $value
126
     *
127
     * @return \Spatie\Html\Elements\Input
128
     */
129
    public function input(?string $type = '', string $name = '', ?string $value = '')
130
    {
131
        return Input::create()
132
            ->attributeIf($type, 'type', $type)
0 ignored issues
show
Documentation introduced by
$type is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
133
            ->attributeIf($name, 'name', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
134
            ->attributeIf($name, 'id', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
            ->attributeIf($name && $this->old($name, $value), 'value', $this->old($name, $value));
0 ignored issues
show
Bug introduced by
It seems like $this->old($name, $value) targeting Spatie\Html\Html::old() can also be of type array or null; however, Spatie\Html\BaseElement::attributeIf() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
136
    }
137
138
    /**
139
     * @param \Spatie\Html\HtmlElement|string $legend
140
     *
141
     * @return \Spatie\Html\Elements\Fieldset
142
     */
143
    public function fieldset($legend = null)
144
    {
145
        return $legend ?
146
            Fieldset::create()->legend($legend) :
147
            Fieldset::create();
148
    }
149
150
    /**
151
     * @param string $method
152
     * @param string $action
153
     * @param array $parameters
0 ignored issues
show
Bug introduced by
There is no parameter named $parameters. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
154
     *
155
     * @return \Spatie\Html\Elements\Form
156
     */
157
    public function form(string $method = 'POST', string $action = '')
158
    {
159
        $method = strtoupper($method);
160
        $form = Form::create();
161
162
        // If Laravel needs to spoof the form's method, we'll append a hidden
163
        // field containing the actual method
164
        if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) {
165
            $form = $form->addChild($this->hidden('_method')->value($method));
166
        }
167
168
        // On any other method that get, the form needs a CSRF token
169
        if ($method !== 'GET') {
170
            $form = $form->addChild($this->token());
171
        }
172
173
        return $form
174
            ->method($method === 'GET' ? 'GET' : 'POST')
175
            ->attributeIf($action, 'action', $action);
0 ignored issues
show
Documentation introduced by
$action is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
176
    }
177
178
    /**
179
     * @param string $name
180
     * @param string $value
181
     *
182
     * @return \Spatie\Html\Elements\Input
183
     */
184
    public function hidden(string $name = '', ?string $value = '')
185
    {
186
        return $this->input('hidden', $name, $value);
187
    }
188
189
    /**
190
     * @param \Spatie\Html\HtmlElement|iterable|string|null $contents
191
     * @param string $for
192
     *
193
     * @return \Spatie\Html\Elements\Label
194
     */
195
    public function label($contents = null, string $for = '')
196
    {
197
        return Label::create()
198
            ->attributeIf($for, 'for', $this->fieldName($for))
0 ignored issues
show
Documentation introduced by
$for is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
199
            ->children($contents);
200
    }
201
202
    /**
203
     * @param \Spatie\Html\HtmlElement|string $contents
204
     *
205
     * @return \Spatie\Html\Elements\Legend
206
     */
207
    public function legend($contents = null)
208
    {
209
        return Legend::create()->html($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 207 can also be of type null or object<Spatie\Html\HtmlElement>; however, Spatie\Html\BaseElement::html() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
210
    }
211
212
    /**
213
     * @param string $email
214
     * @param string $text
215
     *
216
     * @return \Spatie\Html\Elements\A
217
     */
218
    public function mailto(string $email, string $text = '')
219
    {
220
        return $this->a('mailto:'.$email, $text);
221
    }
222
223
    /**
224
     * @param string $text
225
     * @param string $value
226
     * @param bool $selected
227
     *
228
     * @return \Spatie\Html\Elements\Option
229
     */
230
    public function option(?string $text = '', ?string $value = '', $selected = false)
231
    {
232
        return Option::create()
233
            ->text($text)
234
            ->value($value)
235
            ->selectedIf($selected);
236
    }
237
238
    /**
239
     * @param string $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
240
     *
241
     * @return \Spatie\Html\Elements\Input
242
     */
243
    public function password(string $name = '')
244
    {
245
        return $this->input('password', $name)->value('');
246
    }
247
248
    /**
249
     * @param string $value
250
     *
251
     * @return \Spatie\Html\Elements\Input
252
     */
253
    public function radio(string $name = '', ?bool $checked = false, ?string $value = '')
254
    {
255
        return $this->input('radio', $name, $value)
256
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
0 ignored issues
show
Documentation introduced by
$checked is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
257
    }
258
259
    /**
260
     * @param string $name
261
     * @param iterable $options
262
     * @param string $value
263
     *
264
     * @return \Spatie\Html\Elements\Select
265
     */
266
    public function select(string $name = '', iterable $options = [], ?string $value = '')
267
    {
268
        return Select::create()
269
            ->attributeIf($name, 'name', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
270
            ->attributeIf($name, 'id', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
271
            ->options($options)
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 266 can also be of type array; however, Spatie\Html\Elements\Select::options() does only seem to accept object<Spatie\Html\Elements\iterable>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
272
            ->value($name ? $this->old($name, $value) : '');
0 ignored issues
show
Bug introduced by
It seems like $name ? $this->old($name, $value) : '' can also be of type array; however, Spatie\Html\Elements\Select::value() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
273
    }
274
275
    /**
276
     * @param string $name
277
     * @param iterable $options
278
     * @param iterable $values
279
     * @return \Spatie\Html\Elements\Select
280
     */
281
    public function multiselect(string $name = '', iterable $options = [], iterable $values = [])
282
    {
283
        $values = $name ? $this->old($name, $values) : [];
0 ignored issues
show
Bug introduced by
It seems like $values defined by $name ? $this->old($name, $values) : array() on line 283 can also be of type array; however, Spatie\Html\Html::old() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
284
285
        return Select::create()
286
            ->attribute('multiple')
287
            ->attributeIf($name, 'name', $this->fieldName($name) . '[]')
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
288
            ->attributeIf($name, 'id', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
289
            ->addChildren($options, function ($text, $value) use ($values) {
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 281 can also be of type array; however, Spatie\Html\BaseElement::addChildren() does only seem to accept object<Spatie\Html\HtmlE...tie\Html\iterable>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
290
                return Option::create()
291
                    ->value($value)
292
                    ->text($text)
293
                    ->selectedIf(in_array($value, $values));
294
            });
295
    }
296
297
    /**
298
     * @param \Spatie\Html\HtmlElement|string $contents
299
     *
300
     * @return \Spatie\Html\Elements\Span
301
     */
302
    public function span($contents = null)
303
    {
304
        return Span::create()->children($contents);
305
    }
306
307
    /**
308
     * @param string $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
309
     *
310
     * @return \Spatie\Html\Elements\Button
311
     */
312
    public function submit(?string $text = '')
313
    {
314
        return $this->button($text, 'submit');
315
    }
316
317
    /**
318
     * @param string $number
319
     * @param string $text
320
     *
321
     * @return \Spatie\Html\Elements\A
322
     */
323
    public function tel(?string $number, ?string $text = '')
324
    {
325
        return $this->a('tel:'.$number, $text);
326
    }
327
328
    /**
329
     * @param string $name
330
     * @param string $value
331
     *
332
     * @return \Spatie\Html\Elements\Input
333
     */
334
    public function text(string $name = '', ?string $value = '')
335
    {
336
        return $this->input('text', $name, $value);
337
    }
338
339
    /**
340
     * @param string $name
341
     * @param string $value
342
     *
343
     * @return \Spatie\Html\Elements\Textarea
344
     */
345
    public function textarea(string $name = '', ?string $value = '')
346
    {
347
        return Textarea::create()
348
            ->attributeIf($name, 'name', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
349
            ->attributeIf($name, 'id', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
350
            ->value($this->old($name, $value));
0 ignored issues
show
Bug introduced by
It seems like $this->old($name, $value) targeting Spatie\Html\Html::old() can also be of type array or null; however, Spatie\Html\Elements\Textarea::value() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
351
    }
352
353
    /**
354
     * @return \Spatie\Html\Elements\Input
355
     */
356
    public function token()
357
    {
358
        return $this->hidden('_token')->value($this->request->session()->token());
0 ignored issues
show
Bug introduced by
The method token() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
359
    }
360
361
    /**
362
     * @param \ArrayAccess|array $model
363
     *
364
     * @return $this
365
     */
366
    public function model($model)
367
    {
368
        $this->model = $model;
369
370
        return $this;
371
    }
372
373
    /**
374
     * @param \ArrayAccess|array $model
375
     * @param string $method
376
     * @param string $action
377
     *
378
     * @return \Spatie\Html\Elements\Form
379
     */
380
    public function modelForm($model, string $method = 'POST', string $action = ''): Form
381
    {
382
        $this->model($model);
383
384
        return $this->form($method, $action);
385
    }
386
387
    /**
388
     * @return $this
389
     */
390
    public function endModel()
391
    {
392
        $this->model = null;
393
394
        return $this;
395
    }
396
397
    /**
398
     * @return \Illuminate\Contracts\Support\Htmlable
399
     */
400
    public function closeModelForm(): Htmlable
401
    {
402
        $this->endModel();
403
404
        return $this->form()->close();
405
    }
406
407
    /**
408
     * @param string $name
409
     *
410
     * @return mixed
411
     */
412
    protected function old(string $name, $value = '')
413
    {
414
        if (empty($name)) {
415
            return;
416
        }
417
418
        // If there's no default value provided, and the html builder currently
419
        // has a model assigned, try to retrieve a value from the model.
420
        if (empty($value) && $this->model) {
421
            $value = $this->model[$name] ?? '';
422
        }
423
424
        return $this->request->old($name, $value);
425
    }
426
427
    protected function fieldName(string $name): string
428
    {
429
        return $name;
430
    }
431
432
    protected function ensureModelIsAvailable()
433
    {
434
        if (empty($this->model)) {
435
            throw new Exception('Method requires a model to be set on the html builder');
436
        }
437
    }
438
}
439