Completed
Pull Request — master (#84)
by Paul
01:31
created

Html::time()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 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\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
25
class Html
26
{
27
    use Macroable;
28
29
    /** @var \Illuminate\Http\Request */
30
    protected $request;
31
32
    /** @var \ArrayAccess|array */
33
    protected $model;
34
35
    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...
36
    {
37
        $this->request = $request;
38
    }
39
40
    /**
41
     * @param string|null $href
42
     * @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...
43
     *
44
     * @return \Spatie\Html\Elements\A
45
     */
46
    public function a($href = null, $contents = null)
47
    {
48
        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...
49
            ->attributeIf($href, 'href', $href)
50
            ->html($contents);
51
    }
52
53
    /**
54
     * @param string|null $type
55
     * @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...
56
     *
57
     * @return \Spatie\Html\Elements\Button
58
     */
59
    public function button($contents = null, $type = null)
60
    {
61
        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...
62
            ->attributeIf($type, 'type', $type)
63
            ->html($contents);
64
    }
65
66
    /**
67
     * @param \Illuminate\Support\Collection|iterable|string $classes
68
     *
69
     * @return \Illuminate\Contracts\Support\Htmlable
70
     */
71
    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...
72
    {
73
        if ($classes instanceof Collection) {
74
            $classes = $classes->toArray();
75
        }
76
77
        $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...
78
        $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...
79
80
        return new HtmlString(
81
            $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...
82
        );
83
    }
84
85
    /**
86
     * @param string|null $name
87
     * @param bool $checked
88
     * @param string|null $value
89
     *
90
     * @return \Spatie\Html\Elements\Input
91
     */
92
    public function checkbox($name = null, $checked = false, $value = '1')
93
    {
94
        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...
95
            ->attribute('type', 'checkbox')
96
            ->attributeIf($name, 'name', $this->fieldName($name))
97
            ->attributeIf($name, 'id', $this->fieldName($name))
98
            ->attributeIf(! is_null($value), 'value', $value)
99
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
100
    }
101
102
    /**
103
     * @param \Spatie\Html\HtmlElement|string|null $contents
104
     *
105
     * @return \Spatie\Html\Elements\Div
106
     */
107
    public function div($contents = null)
108
    {
109
        return Div::create()->children($contents);
110
    }
111
112
    /**
113
     * @param string|null $name
114
     * @param string|null $value
115
     *
116
     * @return \Spatie\Html\Elements\Input
117
     */
118
    public function email($name = '', $value = '')
119
    {
120
        return $this->input('email', $name, $value);
121
    }
122
123
    /**
124
     * @param string|null $name
125
     * @param string|null $value
126
     *
127
     * @return \Spatie\Html\Elements\Input
128
     */
129
    public function date($name = '', $value = '')
130
    {
131
        return $this->input('date', $name, $value);
132
    }
133
134
    /**
135
     * @param string|null $name
136
     * @param string|null $value
137
     *
138
     * @return \Spatie\Html\Elements\Input
139
     */
140
    public function time($name = '', $value = '')
141
    {
142
        return $this->input('time', $name, $value);
143
    }
144
145
    /**
146
     * @param string $tag
147
     *
148
     * @return \Spatie\Html\Elements\Element
149
     */
150
    public function element($tag)
151
    {
152
        return Element::withTag($tag);
153
    }
154
155
    /**
156
     * @param string|null $type
157
     * @param string|null $name
158
     * @param string|null $value
159
     *
160
     * @return \Spatie\Html\Elements\Input
161
     */
162
    public function input($type = null, $name = null, $value = null)
163
    {
164
        $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...
165
166
        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...
167
            ->attributeIf($type, 'type', $type)
168
            ->attributeIf($name, 'name', $this->fieldName($name))
169
            ->attributeIf($name, 'id', $this->fieldName($name))
170
            ->attributeIf($hasValue, 'value', $this->old($name, $value));
171
    }
172
173
    /**
174
     * @param \Spatie\Html\HtmlElement|string|null $legend
175
     *
176
     * @return \Spatie\Html\Elements\Fieldset
177
     */
178
    public function fieldset($legend = null)
179
    {
180
        return $legend ?
181
            Fieldset::create()->legend($legend) :
182
            Fieldset::create();
183
    }
184
185
    /**
186
     * @param string $method
187
     * @param string|null $action
188
     *
189
     * @return \Spatie\Html\Elements\Form
190
     */
191
    public function form($method = 'POST', $action = null)
192
    {
193
        $method = strtoupper($method);
194
        $form = Form::create();
195
196
        // If Laravel needs to spoof the form's method, we'll append a hidden
197
        // field containing the actual method
198
        if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) {
199
            $form = $form->addChild($this->hidden('_method')->value($method));
200
        }
201
202
        // On any other method that get, the form needs a CSRF token
203
        if ($method !== 'GET') {
204
            $form = $form->addChild($this->token());
205
        }
206
207
        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...
208
            ->method($method === 'GET' ? 'GET' : 'POST')
209
            ->attributeIf($action, 'action', $action);
210
    }
211
212
    /**
213
     * @param string|null $name
214
     * @param string|null $value
215
     *
216
     * @return \Spatie\Html\Elements\Input
217
     */
218
    public function hidden($name = null, $value = null)
219
    {
220
        return $this->input('hidden', $name, $value);
221
    }
222
223
    /**
224
     * @param \Spatie\Html\HtmlElement|iterable|string|null $contents
225
     * @param string|null $for
226
     *
227
     * @return \Spatie\Html\Elements\Label
228
     */
229
    public function label($contents = null, $for = null)
230
    {
231
        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...
232
            ->attributeIf($for, 'for', $this->fieldName($for))
233
            ->children($contents);
234
    }
235
236
    /**
237
     * @param \Spatie\Html\HtmlElement|string|null $contents
238
     *
239
     * @return \Spatie\Html\Elements\Legend
240
     */
241
    public function legend($contents = null)
242
    {
243
        return Legend::create()->html($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 241 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...
244
    }
245
246
    /**
247
     * @param string $email
248
     * @param string|null $text
249
     *
250
     * @return \Spatie\Html\Elements\A
251
     */
252
    public function mailto($email, $text = null)
253
    {
254
        return $this->a('mailto:'.$email, $text);
255
    }
256
257
    /**
258
     * @param string|null $name
259
     * @param iterable $options
260
     * @param string|iterable|null $value
261
     *
262
     * @return \Spatie\Html\Elements\Select
263
     */
264 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...
265
    {
266
        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...
267
            ->attributeIf($name, 'name', $this->fieldName($name))
268
            ->attributeIf($name, 'id', $this->fieldName($name))
269
            ->options($options)
270
            ->value($name ? $this->old($name, $value) : $value)
271
            ->multiple();
272
    }
273
274
    /**
275
     * @param string|null $text
276
     * @param string|null $value
277
     * @param bool $selected
278
     *
279
     * @return \Spatie\Html\Elements\Option
280
     */
281
    public function option($text = null, $value = null, $selected = false)
282
    {
283
        return Option::create()
284
            ->text($text)
285
            ->value($value)
286
            ->selectedIf($selected);
287
    }
288
289
    /**
290
     * @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...
291
     *
292
     * @return \Spatie\Html\Elements\Input
293
     */
294
    public function password($name = null)
295
    {
296
        return $this->input('password', $name);
297
    }
298
299
    /**
300
     * @param string|null $name
301
     * @param bool $checked
302
     * @param string|null $value
303
     *
304
     * @return \Spatie\Html\Elements\Input
305
     */
306
    public function radio($name = null, $checked = false, $value = null)
307
    {
308
        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...
309
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
310
    }
311
312
    /**
313
     * @param string|null $name
314
     * @param iterable $options
315
     * @param string|iterable|null $value
316
     *
317
     * @return \Spatie\Html\Elements\Select
318
     */
319 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...
320
    {
321
        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...
322
            ->attributeIf($name, 'name', $this->fieldName($name))
323
            ->attributeIf($name, 'id', $this->fieldName($name))
324
            ->options($options)
325
            ->value($name ? $this->old($name, $value) : $value);
326
    }
327
328
    /**
329
     * @param \Spatie\Html\HtmlElement|string|null $contents
330
     *
331
     * @return \Spatie\Html\Elements\Span
332
     */
333
    public function span($contents = null)
334
    {
335
        return Span::create()->children($contents);
336
    }
337
338
    /**
339
     * @param string|null $text
340
     *
341
     * @return \Spatie\Html\Elements\Button
342
     */
343
    public function submit($text = null)
344
    {
345
        return $this->button($text, 'submit');
346
    }
347
348
    /**
349
     * @param string|null $text
350
     *
351
     * @return \Spatie\Html\Elements\Button
352
     */
353
    public function reset($text = null)
354
    {
355
        return $this->button($text, 'reset');
356
    }
357
358
    /**
359
     * @param string $number
360
     * @param string|null $text
361
     *
362
     * @return \Spatie\Html\Elements\A
363
     */
364
    public function tel($number, $text = null)
365
    {
366
        return $this->a('tel:'.$number, $text);
367
    }
368
369
    /**
370
     * @param string|null $name
371
     * @param string|null $value
372
     *
373
     * @return \Spatie\Html\Elements\Input
374
     */
375
    public function text($name = null, $value = null)
376
    {
377
        return $this->input('text', $name, $value);
378
    }
379
380
    /**
381
     * @param string|null $name
382
     *
383
     * @return \Spatie\Html\Elements\File
384
     */
385
    public function file($name = null)
386
    {
387
        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...
388
            ->attributeIf($name, 'name', $this->fieldName($name))
389
            ->attributeIf($name, 'id', $this->fieldName($name));
390
    }
391
392
    /**
393
     * @param string|null $name
394
     * @param string|null $value
395
     *
396
     * @return \Spatie\Html\Elements\Textarea
397
     */
398
    public function textarea($name = null, $value = null)
399
    {
400
        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...
401
            ->attributeIf($name, 'name', $this->fieldName($name))
402
            ->attributeIf($name, 'id', $this->fieldName($name))
403
            ->value($this->old($name, $value));
404
    }
405
406
    /**
407
     * @return \Spatie\Html\Elements\Input
408
     */
409
    public function token()
410
    {
411
        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...
412
    }
413
414
    /**
415
     * @param \ArrayAccess|array $model
416
     *
417
     * @return $this
418
     */
419
    public function model($model)
420
    {
421
        $this->model = $model;
422
423
        return $this;
424
    }
425
426
    /**
427
     * @param \ArrayAccess|array $model
428
     * @param string|null $method
429
     * @param string|null $action
430
     *
431
     * @return \Spatie\Html\Elements\Form
432
     */
433
    public function modelForm($model, $method = 'POST', $action = null): Form
434
    {
435
        $this->model($model);
436
437
        return $this->form($method, $action);
438
    }
439
440
    /**
441
     * @return $this
442
     */
443
    public function endModel()
444
    {
445
        $this->model = null;
446
447
        return $this;
448
    }
449
450
    /**
451
     * @return \Illuminate\Contracts\Support\Htmlable
452
     */
453
    public function closeModelForm(): Htmlable
454
    {
455
        $this->endModel();
456
457
        return $this->form()->close();
458
    }
459
460
    /**
461
     * @param string $name
462
     * @param mixed $value
463
     *
464
     * @return mixed
465
     */
466
    protected function old($name, $value = null)
467
    {
468
        if (empty($name)) {
469
            return;
470
        }
471
472
        // If there's no default value provided, and the html builder currently
473
        // has a model assigned, try to retrieve a value from the model.
474
        if (empty($value) && $this->model) {
475
            $value = $this->model[$name] ?? '';
476
        }
477
478
        // 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...
479
        $name = preg_replace('/\[(.+)\]/U', '.$1', $name);
480
481
        return $this->request->old($name, $value);
482
    }
483
484
    /**
485
     * @param string $name
486
     *
487
     * @return string
488
     */
489
    protected function fieldName($name)
490
    {
491
        return $name;
492
    }
493
494
    protected function ensureModelIsAvailable()
495
    {
496
        if (empty($this->model)) {
497
            throw new Exception('Method requires a model to be set on the html builder');
498
        }
499
    }
500
}
501