Completed
Push — master ( 81dcbd...e7dfbe )
by Sebastian
03:27
created

Html::radio()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
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\File;
9
use Spatie\Html\Elements\Form;
10
use Spatie\Html\Elements\Span;
11
use Spatie\Html\Elements\Input;
12
use Spatie\Html\Elements\Label;
13
use Spatie\Html\Elements\Button;
14
use Spatie\Html\Elements\Legend;
15
use Spatie\Html\Elements\Option;
16
use Spatie\Html\Elements\Select;
17
use Spatie\Html\Elements\Element;
18
use Illuminate\Support\Collection;
19
use Illuminate\Support\HtmlString;
20
use Spatie\Html\Elements\Fieldset;
21
use Spatie\Html\Elements\Textarea;
22
use Illuminate\Support\Traits\Macroable;
23
use Illuminate\Contracts\Support\Htmlable;
24
use Spatie\Html\Elements\Img;
25
26
class Html
27
{
28
    use Macroable;
29
30
    /** @var \Illuminate\Http\Request */
31
    protected $request;
32
33
    /** @var \ArrayAccess|array */
34
    protected $model;
35
36
    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...
37
    {
38
        $this->request = $request;
39
    }
40
41
    /**
42
     * @param string|null $href
43
     * @param string|null $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...
44
     *
45
     * @return \Spatie\Html\Elements\A
46
     */
47
    public function a($href = null, $contents = null)
48
    {
49
        return A::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\A>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
50
            ->attributeIf($href, 'href', $href)
51
            ->html($contents);
52
    }
53
54
    /**
55
     * @param string|null $type
56
     * @param string|null $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...
57
     *
58
     * @return \Spatie\Html\Elements\Button
59
     */
60
    public function button($contents = null, $type = null)
61
    {
62
        return Button::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Button>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63
            ->attributeIf($type, 'type', $type)
64
            ->html($contents);
65
    }
66
67
    /**
68
     * @param \Illuminate\Support\Collection|iterable|string $classes
69
     *
70
     * @return \Illuminate\Contracts\Support\Htmlable
71
     */
72
    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...
73
    {
74
        if ($classes instanceof Collection) {
75
            $classes = $classes->toArray();
76
        }
77
78
        $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...
79
        $attributes->addClass($classes);
0 ignored issues
show
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...
80
81
        return new HtmlString(
82
            $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...
83
        );
84
    }
85
86
    /**
87
     * @param string|null $name
88
     * @param bool $checked
89
     * @param string|null $value
90
     *
91
     * @return \Spatie\Html\Elements\Input
92
     */
93
    public function checkbox($name = null, $checked = false, $value = '1')
94
    {
95
        return Input::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Input>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
96
            ->attribute('type', 'checkbox')
97
            ->attributeIf($name, 'name', $this->fieldName($name))
98
            ->attributeIf($name, 'id', $this->fieldName($name))
99
            ->attributeIf(! is_null($value), 'value', $value)
100
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
101
    }
102
103
    /**
104
     * @param \Spatie\Html\HtmlElement|string|null $contents
105
     *
106
     * @return \Spatie\Html\Elements\Div
107
     */
108
    public function div($contents = null)
109
    {
110
        return Div::create()->children($contents);
111
    }
112
113
    /**
114
     * @param string|null $name
115
     * @param string|null $value
116
     *
117
     * @return \Spatie\Html\Elements\Input
118
     */
119
    public function email($name = '', $value = '')
120
    {
121
        return $this->input('email', $name, $value);
122
    }
123
124
    /**
125
     * @param string|null $name
126
     * @param string|null $value
127
     *
128
     * @return \Spatie\Html\Elements\Input
129
     */
130
    public function date($name = '', $value = '')
131
    {
132
        return $this->input('date', $name, $value);
133
    }
134
135
    /**
136
     * @param string|null $name
137
     * @param string|null $value
138
     *
139
     * @return \Spatie\Html\Elements\Input
140
     */
141
    public function time($name = '', $value = '')
142
    {
143
        return $this->input('time', $name, $value);
144
    }
145
146
    /**
147
     * @param string $tag
148
     *
149
     * @return \Spatie\Html\Elements\Element
150
     */
151
    public function element($tag)
152
    {
153
        return Element::withTag($tag);
154
    }
155
156
    /**
157
     * @param string|null $type
158
     * @param string|null $name
159
     * @param string|null $value
160
     *
161
     * @return \Spatie\Html\Elements\Input
162
     */
163
    public function input($type = null, $name = null, $value = null)
164
    {
165
        $hasValue = $name && (! is_null($this->old($name, $value)) || ! is_null($value));
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
166
167
        return Input::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Input>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
168
            ->attributeIf($type, 'type', $type)
169
            ->attributeIf($name, 'name', $this->fieldName($name))
170
            ->attributeIf($name, 'id', $this->fieldName($name))
171
            ->attributeIf($hasValue, 'value', $this->old($name, $value));
172
    }
173
174
    /**
175
     * @param \Spatie\Html\HtmlElement|string|null $legend
176
     *
177
     * @return \Spatie\Html\Elements\Fieldset
178
     */
179
    public function fieldset($legend = null)
180
    {
181
        return $legend ?
182
            Fieldset::create()->legend($legend) :
183
            Fieldset::create();
184
    }
185
186
    /**
187
     * @param string $method
188
     * @param string|null $action
189
     *
190
     * @return \Spatie\Html\Elements\Form
191
     */
192
    public function form($method = 'POST', $action = null)
193
    {
194
        $method = strtoupper($method);
195
        $form = Form::create();
196
197
        // If Laravel needs to spoof the form's method, we'll append a hidden
198
        // field containing the actual method
199
        if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) {
200
            $form = $form->addChild($this->hidden('_method')->value($method));
201
        }
202
203
        // On any other method that get, the form needs a CSRF token
204
        if ($method !== 'GET') {
205
            $form = $form->addChild($this->token());
206
        }
207
208
        return $form
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Form>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
209
            ->method($method === 'GET' ? 'GET' : 'POST')
210
            ->attributeIf($action, 'action', $action);
211
    }
212
213
    /**
214
     * @param string|null $name
215
     * @param string|null $value
216
     *
217
     * @return \Spatie\Html\Elements\Input
218
     */
219
    public function hidden($name = null, $value = null)
220
    {
221
        return $this->input('hidden', $name, $value);
222
    }
223
224
    /**
225
     * @param string|null $src
226
     * @param string|null $alt
227
     *
228
     * @return \Spatie\Html\Elements\Img
229
     */
230
    public function img($src = null, $alt = null)
231
    {
232
        return Img::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Img>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
233
            ->attributeIf($src, 'src', $src)
234
            ->attributeIf($alt, 'alt', $alt);
235
    }
236
237
    /**
238
     * @param \Spatie\Html\HtmlElement|iterable|string|null $contents
239
     * @param string|null $for
240
     *
241
     * @return \Spatie\Html\Elements\Label
242
     */
243
    public function label($contents = null, $for = null)
244
    {
245
        return Label::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Label>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
246
            ->attributeIf($for, 'for', $this->fieldName($for))
247
            ->children($contents);
248
    }
249
250
    /**
251
     * @param \Spatie\Html\HtmlElement|string|null $contents
252
     *
253
     * @return \Spatie\Html\Elements\Legend
254
     */
255
    public function legend($contents = null)
256
    {
257
        return Legend::create()->html($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 255 can also be of type object<Spatie\Html\HtmlElement>; however, Spatie\Html\BaseElement::html() does only seem to accept string|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...
258
    }
259
260
    /**
261
     * @param string $email
262
     * @param string|null $text
263
     *
264
     * @return \Spatie\Html\Elements\A
265
     */
266
    public function mailto($email, $text = null)
267
    {
268
        return $this->a('mailto:'.$email, $text);
269
    }
270
271
    /**
272
     * @param string|null $name
273
     * @param iterable $options
274
     * @param string|iterable|null $value
275
     *
276
     * @return \Spatie\Html\Elements\Select
277
     */
278 View Code Duplication
    public function multiselect($name = null, $options = [], $value = null)
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...
279
    {
280
        return Select::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Select>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
281
            ->attributeIf($name, 'name', $this->fieldName($name))
282
            ->attributeIf($name, 'id', $this->fieldName($name))
283
            ->options($options)
284
            ->value($name ? $this->old($name, $value) : $value)
285
            ->multiple();
286
    }
287
288
    /**
289
     * @param string|null $text
290
     * @param string|null $value
291
     * @param bool $selected
292
     *
293
     * @return \Spatie\Html\Elements\Option
294
     */
295
    public function option($text = null, $value = null, $selected = false)
296
    {
297
        return Option::create()
298
            ->text($text)
299
            ->value($value)
300
            ->selectedIf($selected);
301
    }
302
303
    /**
304
     * @param string|null $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...
305
     *
306
     * @return \Spatie\Html\Elements\Input
307
     */
308
    public function password($name = null)
309
    {
310
        return $this->input('password', $name);
311
    }
312
313
    /**
314
     * @param string|null $name
315
     * @param bool $checked
316
     * @param string|null $value
317
     *
318
     * @return \Spatie\Html\Elements\Input
319
     */
320
    public function radio($name = null, $checked = false, $value = null)
321
    {
322
        return $this->input('radio', $name, $value)
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Input>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
323
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
324
    }
325
326
    /**
327
     * @param string|null $name
328
     * @param iterable $options
329
     * @param string|iterable|null $value
330
     *
331
     * @return \Spatie\Html\Elements\Select
332
     */
333 View Code Duplication
    public function select($name = null, $options = [], $value = null)
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...
334
    {
335
        return Select::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Select>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
336
            ->attributeIf($name, 'name', $this->fieldName($name))
337
            ->attributeIf($name, 'id', $this->fieldName($name))
338
            ->options($options)
339
            ->value($name ? $this->old($name, $value) : $value);
340
    }
341
342
    /**
343
     * @param \Spatie\Html\HtmlElement|string|null $contents
344
     *
345
     * @return \Spatie\Html\Elements\Span
346
     */
347
    public function span($contents = null)
348
    {
349
        return Span::create()->children($contents);
350
    }
351
352
    /**
353
     * @param string|null $text
354
     *
355
     * @return \Spatie\Html\Elements\Button
356
     */
357
    public function submit($text = null)
358
    {
359
        return $this->button($text, 'submit');
360
    }
361
362
    /**
363
     * @param string|null $text
364
     *
365
     * @return \Spatie\Html\Elements\Button
366
     */
367
    public function reset($text = null)
368
    {
369
        return $this->button($text, 'reset');
370
    }
371
372
    /**
373
     * @param string $number
374
     * @param string|null $text
375
     *
376
     * @return \Spatie\Html\Elements\A
377
     */
378
    public function tel($number, $text = null)
379
    {
380
        return $this->a('tel:'.$number, $text);
381
    }
382
383
    /**
384
     * @param string|null $name
385
     * @param string|null $value
386
     *
387
     * @return \Spatie\Html\Elements\Input
388
     */
389
    public function text($name = null, $value = null)
390
    {
391
        return $this->input('text', $name, $value);
392
    }
393
394
    /**
395
     * @param string|null $name
396
     *
397
     * @return \Spatie\Html\Elements\File
398
     */
399
    public function file($name = null)
400
    {
401
        return File::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\File>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
402
            ->attributeIf($name, 'name', $this->fieldName($name))
403
            ->attributeIf($name, 'id', $this->fieldName($name));
404
    }
405
406
    /**
407
     * @param string|null $name
408
     * @param string|null $value
409
     *
410
     * @return \Spatie\Html\Elements\Textarea
411
     */
412
    public function textarea($name = null, $value = null)
413
    {
414
        return Textarea::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Textarea>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
415
            ->attributeIf($name, 'name', $this->fieldName($name))
416
            ->attributeIf($name, 'id', $this->fieldName($name))
417
            ->value($this->old($name, $value));
418
    }
419
420
    /**
421
     * @return \Spatie\Html\Elements\Input
422
     */
423
    public function token()
424
    {
425
        return $this
426
            ->hidden()
427
            ->name('_token')
428
            ->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...
429
    }
430
431
    /**
432
     * @param \ArrayAccess|array $model
433
     *
434
     * @return $this
435
     */
436
    public function model($model)
437
    {
438
        $this->model = $model;
439
440
        return $this;
441
    }
442
443
    /**
444
     * @param \ArrayAccess|array $model
445
     * @param string|null $method
446
     * @param string|null $action
447
     *
448
     * @return \Spatie\Html\Elements\Form
449
     */
450
    public function modelForm($model, $method = 'POST', $action = null): Form
451
    {
452
        $this->model($model);
453
454
        return $this->form($method, $action);
455
    }
456
457
    /**
458
     * @return $this
459
     */
460
    public function endModel()
461
    {
462
        $this->model = null;
463
464
        return $this;
465
    }
466
467
    /**
468
     * @return \Illuminate\Contracts\Support\Htmlable
469
     */
470
    public function closeModelForm(): Htmlable
471
    {
472
        $this->endModel();
473
474
        return $this->form()->close();
475
    }
476
477
    /**
478
     * @param string $name
479
     * @param mixed $value
480
     *
481
     * @return mixed
482
     */
483
    protected function old($name, $value = null)
484
    {
485
        if (empty($name)) {
486
            return;
487
        }
488
489
        // If there's no default value provided, and the html builder currently
490
        // has a model assigned, try to retrieve a value from the model.
491
        if (empty($value) && $this->model) {
492
            $value = $this->model[$name] ?? '';
493
        }
494
495
        // Convert array format (sth[1]) to dot notation (sth.1)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
496
        $name = preg_replace('/\[(.+)\]/U', '.$1', $name);
497
498
        return $this->request->old($name, $value);
499
    }
500
501
    /**
502
     * @param string $name
503
     *
504
     * @return string
505
     */
506
    protected function fieldName($name)
507
    {
508
        return $name;
509
    }
510
511
    protected function ensureModelIsAvailable()
512
    {
513
        if (empty($this->model)) {
514
            throw new Exception('Method requires a model to be set on the html builder');
515
        }
516
    }
517
}
518