Html::div()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Html;
4
5
use DateTimeImmutable;
6
use Illuminate\Contracts\Support\Htmlable;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\HtmlString;
10
use Illuminate\Support\Str;
11
use Illuminate\Support\Traits\Macroable;
12
use Spatie\Html\Elements\A;
13
use Spatie\Html\Elements\Button;
14
use Spatie\Html\Elements\Div;
15
use Spatie\Html\Elements\Element;
16
use Spatie\Html\Elements\Fieldset;
17
use Spatie\Html\Elements\File;
18
use Spatie\Html\Elements\Form;
19
use Spatie\Html\Elements\I;
20
use Spatie\Html\Elements\Img;
21
use Spatie\Html\Elements\Input;
22
use Spatie\Html\Elements\Label;
23
use Spatie\Html\Elements\Legend;
24
use Spatie\Html\Elements\Option;
25
use Spatie\Html\Elements\Select;
26
use Spatie\Html\Elements\Span;
27
use Spatie\Html\Elements\Textarea;
28
29
class Html
30
{
31
    use Macroable;
32
33
    const HTML_DATE_FORMAT = 'Y-m-d';
34
    const HTML_TIME_FORMAT = 'H:i:s';
35
36
    /** @var \Illuminate\Http\Request */
37
    protected $request;
38
39
    /** @var \ArrayAccess|array */
40
    protected $model;
41
42
    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...
43
    {
44
        $this->request = $request;
45
    }
46
47
    /**
48
     * @param string|null $href
49
     * @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...
50
     *
51
     * @return \Spatie\Html\Elements\A
52
     */
53
    public function a($href = null, $contents = null)
54
    {
55
        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...
56
            ->attributeIf($href, 'href', $href)
57
            ->html($contents);
58
    }
59
60
    /**
61
     * @param string|null $href
0 ignored issues
show
Bug introduced by
There is no parameter named $href. 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...
62
     * @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...
63
     *
64
     * @return \Spatie\Html\Elements\I
65
     */
66
    public function i($contents = null)
67
    {
68
        return I::create()
69
            ->html($contents);
70
    }
71
72
    /**
73
     * @param string|null $type
74
     * @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...
75
     *
76
     * @return \Spatie\Html\Elements\Button
77
     */
78
    public function button($contents = null, $type = null, $name = null)
79
    {
80
        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...
81
            ->attributeIf($type, 'type', $type)
82
            ->attributeIf($name, 'name', $this->fieldName($name))
83
            ->html($contents);
84
    }
85
86
    /**
87
     * @param \Illuminate\Support\Collection|iterable|string $classes
88
     *
89
     * @return \Illuminate\Contracts\Support\Htmlable
90
     */
91
    public function class($classes): Htmlable
92
    {
93
        if ($classes instanceof Collection) {
94
            $classes = $classes->toArray();
95
        }
96
97
        $attributes = new Attributes();
98
        $attributes->addClass($classes);
99
100
        return new HtmlString(
101
            $attributes->render()
102
        );
103
    }
104
105
    /**
106
     * @param string|null $name
107
     * @param bool $checked
108
     * @param string|null $value
109
     *
110
     * @return \Spatie\Html\Elements\Input
111
     */
112
    public function checkbox($name = null, $checked = null, $value = '1')
113
    {
114
        return $this->input('checkbox', $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...
115
            ->attributeIf(! is_null($value), 'value', $value)
116
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
117
    }
118
119
    /**
120
     * @param \Spatie\Html\HtmlElement|string|null $contents
121
     *
122
     * @return \Spatie\Html\Elements\Div
123
     */
124
    public function div($contents = null)
125
    {
126
        return Div::create()->children($contents);
127
    }
128
129
    /**
130
     * @param string|null $name
131
     * @param string|null $value
132
     *
133
     * @return \Spatie\Html\Elements\Input
134
     */
135
    public function email($name = null, $value = null)
136
    {
137
        return $this->input('email', $name, $value);
138
    }
139
140
    /**
141
     * @param string|null $name
142
     * @param string|null $value
143
     * @param bool $format
144
     *
145
     * @return \Spatie\Html\Elements\Input
146
     */
147 View Code Duplication
    public function date($name = '', $value = null, $format = true)
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...
148
    {
149
        $element = $this->input('date', $name, $value);
150
151
        if (! $format || empty($element->getAttribute('value'))) {
152
            return $element;
153
        }
154
155
        return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_DATE_FORMAT));
156
    }
157
158
    /**
159
     * @param string|null $name
160
     * @param string|null $value
161
     * @param bool $format
162
     *
163
     * @return \Spatie\Html\Elements\Input
164
     */
165 View Code Duplication
    public function datetime($name = '', $value = null, $format = true)
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...
166
    {
167
        $element = $this->input('datetime-local', $name, $value);
168
169
        if (! $format || empty($element->getAttribute('value'))) {
170
            return $element;
171
        }
172
173
        return $element->value($this->formatDateTime($element->getAttribute('value'),
174
            self::HTML_DATE_FORMAT.'\T'.self::HTML_TIME_FORMAT));
175
    }
176
177
    /**
178
     * @param string|null $name
179
     * @param string|null $value
180
     * @param string|null $min
181
     * @param string|null $max
182
     * @param string|null $step
183
     *
184
     * @return \Spatie\Html\Elements\Input
185
     */
186
    public function range($name = '', $value = '', $min = null, $max = null, $step = null)
187
    {
188
        return $this->input('range', $name, $value)
0 ignored issues
show
Documentation Bug introduced by
The method attributeIfNotNull 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...
189
            ->attributeIfNotNull($min, 'min', $min)
190
            ->attributeIfNotNull($max, 'max', $max)
191
            ->attributeIfNotNull($step, 'step', $step);
192
    }
193
194
    /**
195
     * @param string|null $name
196
     * @param string|null $value
197
     * @param bool $format
198
     *
199
     * @return \Spatie\Html\Elements\Input
200
     */
201 View Code Duplication
    public function time($name = '', $value = null, $format = true)
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...
202
    {
203
        $element = $this->input('time', $name, $value);
204
205
        if (! $format || empty($element->getAttribute('value'))) {
206
            return $element;
207
        }
208
209
        return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_TIME_FORMAT));
210
    }
211
212
    /**
213
     * @param string $tag
214
     *
215
     * @return \Spatie\Html\Elements\Element
216
     */
217
    public function element($tag)
218
    {
219
        return Element::withTag($tag);
220
    }
221
222
    /**
223
     * @param string|null $type
224
     * @param string|null $name
225
     * @param string|null $value
226
     *
227
     * @return \Spatie\Html\Elements\Input
228
     */
229
    public function input($type = null, $name = null, $value = null)
230
    {
231
        $hasValue = $name && ($type !== 'password' && ! 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...
232
233
        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...
234
            ->attributeIf($type, 'type', $type)
235
            ->attributeIf($name, 'name', $this->fieldName($name))
236
            ->attributeIf($name, 'id', $this->fieldName($name))
237
            ->attributeIf($hasValue, 'value', $this->old($name, $value));
238
    }
239
240
    /**
241
     * @param \Spatie\Html\HtmlElement|string|null $legend
242
     *
243
     * @return \Spatie\Html\Elements\Fieldset
244
     */
245
    public function fieldset($legend = null)
246
    {
247
        return $legend ?
248
            Fieldset::create()->legend($legend) : Fieldset::create();
249
    }
250
251
    /**
252
     * @param string $method
253
     * @param string|null $action
254
     *
255
     * @return \Spatie\Html\Elements\Form
256
     */
257
    public function form($method = 'POST', $action = null)
258
    {
259
        $method = strtoupper($method);
260
        $form = Form::create();
261
262
        // If Laravel needs to spoof the form's method, we'll append a hidden
263
        // field containing the actual method
264
        if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) {
265
            $form = $form->addChild($this->hidden('_method')->value($method));
266
        }
267
268
        // On any other method that get, the form needs a CSRF token
269
        if ($method !== 'GET') {
270
            $form = $form->addChild($this->token());
271
        }
272
273
        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...
274
            ->method($method === 'GET' ? 'GET' : 'POST')
275
            ->attributeIf($action, 'action', $action);
276
    }
277
278
    /**
279
     * @param string|null $name
280
     * @param string|null $value
281
     *
282
     * @return \Spatie\Html\Elements\Input
283
     */
284
    public function hidden($name = null, $value = null)
285
    {
286
        return $this->input('hidden', $name, $value);
287
    }
288
289
    /**
290
     * @param string|null $src
291
     * @param string|null $alt
292
     *
293
     * @return \Spatie\Html\Elements\Img
294
     */
295
    public function img($src = null, $alt = null)
296
    {
297
        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...
298
            ->attributeIf($src, 'src', $src)
299
            ->attributeIf($alt, 'alt', $alt);
300
    }
301
302
    /**
303
     * @param \Spatie\Html\HtmlElement|iterable|string|null $contents
304
     * @param string|null $for
305
     *
306
     * @return \Spatie\Html\Elements\Label
307
     */
308
    public function label($contents = null, $for = null)
309
    {
310
        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...
311
            ->attributeIf($for, 'for', $this->fieldName($for))
312
            ->children($contents);
313
    }
314
315
    /**
316
     * @param \Spatie\Html\HtmlElement|string|null $contents
317
     *
318
     * @return \Spatie\Html\Elements\Legend
319
     */
320
    public function legend($contents = null)
321
    {
322
        return Legend::create()->html($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 320 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...
323
    }
324
325
    /**
326
     * @param string $email
327
     * @param string|null $text
328
     *
329
     * @return \Spatie\Html\Elements\A
330
     */
331
    public function mailto($email, $text = null)
332
    {
333
        return $this->a('mailto:'.$email, $text ?: $email);
334
    }
335
336
    /**
337
     * @param string|null $name
338
     * @param iterable $options
339
     * @param string|iterable|null $value
340
     *
341
     * @return \Spatie\Html\Elements\Select
342
     */
343 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...
344
    {
345
        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...
346
            ->attributeIf($name, 'name', $this->fieldName($name))
347
            ->attributeIf($name, 'id', $this->fieldName($name))
348
            ->options($options)
349
            ->value($name ? $this->old($name, $value) : $value)
350
            ->multiple();
351
    }
352
353
    /**
354
     * @param string|null $name
355
     * @param string|null $value
356
     * @param string|null $min
357
     * @param string|null $max
358
     * @param string|null $step
359
     *
360
     * @return \Spatie\Html\Elements\Input
361
     */
362
    public function number($name = null, $value = null, $min = null, $max = null, $step = null)
363
    {
364
        return $this->input('number', $name, $value)
0 ignored issues
show
Documentation Bug introduced by
The method attributeIfNotNull 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...
365
                ->attributeIfNotNull($min, 'min', $min)
366
                ->attributeIfNotNull($max, 'max', $max)
367
                ->attributeIfNotNull($step, 'step', $step);
368
    }
369
370
    /**
371
     * @param string|null $text
372
     * @param string|null $value
373
     * @param bool $selected
374
     *
375
     * @return \Spatie\Html\Elements\Option
376
     */
377
    public function option($text = null, $value = null, $selected = false)
378
    {
379
        return Option::create()
380
            ->text($text)
381
            ->value($value)
382
            ->selectedIf($selected);
383
    }
384
385
    /**
386
     * @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...
387
     *
388
     * @return \Spatie\Html\Elements\Input
389
     */
390
    public function password($name = null)
391
    {
392
        return $this->input('password', $name);
393
    }
394
395
    /**
396
     * @param string|null $name
397
     * @param bool $checked
398
     * @param string|null $value
399
     *
400
     * @return \Spatie\Html\Elements\Input
401
     */
402
    public function radio($name = null, $checked = null, $value = null)
403
    {
404
        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...
405
            ->attributeIf($name, 'id', $value === null ? $name : ($name.'_'.Str::slug($value)))
406
            ->attributeIf(! is_null($value), 'value', $value)
407
            ->attributeIf((! is_null($value) && $this->old($name) == $value) || $checked, 'checked');
408
    }
409
410
    /**
411
     * @param string|null $name
412
     * @param iterable $options
413
     * @param string|iterable|null $value
414
     *
415
     * @return \Spatie\Html\Elements\Select
416
     */
417 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...
418
    {
419
        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...
420
            ->attributeIf($name, 'name', $this->fieldName($name))
421
            ->attributeIf($name, 'id', $this->fieldName($name))
422
            ->options($options)
423
            ->value($name ? $this->old($name, $value) : $value);
424
    }
425
426
    /**
427
     * @param \Spatie\Html\HtmlElement|string|null $contents
428
     *
429
     * @return \Spatie\Html\Elements\Span
430
     */
431
    public function span($contents = null)
432
    {
433
        return Span::create()->children($contents);
434
    }
435
436
    /**
437
     * @param string|null $text
438
     *
439
     * @return \Spatie\Html\Elements\Button
440
     */
441
    public function submit($text = null)
442
    {
443
        return $this->button($text, 'submit');
444
    }
445
446
    /**
447
     * @param string|null $text
448
     *
449
     * @return \Spatie\Html\Elements\Button
450
     */
451
    public function reset($text = null)
452
    {
453
        return $this->button($text, 'reset');
454
    }
455
456
    /**
457
     * @param string $number
458
     * @param string|null $text
459
     *
460
     * @return \Spatie\Html\Elements\A
461
     */
462
    public function tel($number, $text = null)
463
    {
464
        return $this->a('tel:'.$number, $text ?: $number);
465
    }
466
467
    /**
468
     * @param string|null $name
469
     * @param string|null $value
470
     *
471
     * @return \Spatie\Html\Elements\Input
472
     */
473
    public function text($name = null, $value = null)
474
    {
475
        return $this->input('text', $name, $value);
476
    }
477
478
    /**
479
     * @param string|null $name
480
     *
481
     * @return \Spatie\Html\Elements\File
482
     */
483
    public function file($name = null)
484
    {
485
        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...
486
            ->attributeIf($name, 'name', $this->fieldName($name))
487
            ->attributeIf($name, 'id', $this->fieldName($name));
488
    }
489
490
    /**
491
     * @param string|null $name
492
     * @param string|null $value
493
     *
494
     * @return \Spatie\Html\Elements\Textarea
495
     */
496
    public function textarea($name = null, $value = null)
497
    {
498
        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...
499
            ->attributeIf($name, 'name', $this->fieldName($name))
500
            ->attributeIf($name, 'id', $this->fieldName($name))
501
            ->value($this->old($name, $value));
502
    }
503
504
    /**
505
     * @return \Spatie\Html\Elements\Input
506
     */
507
    public function token()
508
    {
509
        return $this
510
            ->hidden()
511
            ->name('_token')
512
            ->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...
513
    }
514
515
    /**
516
     * @param \ArrayAccess|array $model
517
     *
518
     * @return $this
519
     */
520
    public function model($model)
521
    {
522
        $this->model = $model;
523
524
        return $this;
525
    }
526
527
    /**
528
     * @param \ArrayAccess|array $model
529
     * @param string|null $method
530
     * @param string|null $action
531
     *
532
     * @return \Spatie\Html\Elements\Form
533
     */
534
    public function modelForm($model, $method = 'POST', $action = null): Form
535
    {
536
        $this->model($model);
537
538
        return $this->form($method, $action);
539
    }
540
541
    /**
542
     * @return $this
543
     */
544
    public function endModel()
545
    {
546
        $this->model = null;
547
548
        return $this;
549
    }
550
551
    /**
552
     * @return \Illuminate\Contracts\Support\Htmlable
553
     */
554
    public function closeModelForm(): Htmlable
555
    {
556
        $this->endModel();
557
558
        return $this->form()->close();
559
    }
560
561
    /**
562
     * @param string $name
563
     * @param mixed $value
564
     *
565
     * @return mixed
566
     */
567
    protected function old($name, $value = null)
568
    {
569
        if (empty($name)) {
570
            return;
571
        }
572
573
        // Convert array format (sth[1]) to dot notation (sth.1)
574
        $name = preg_replace('/\[(.+)\]/U', '.$1', $name);
575
576
        // If there's no default value provided, the html builder currently
577
        // has a model assigned and there aren't old input items,
578
        // try to retrieve a value from the model.
579
        if (is_null($value) && $this->model && empty($this->request->old())) {
580
            $value = data_get($this->model, $name) ?? '';
581
        }
582
583
        return $this->request->old($name, $value);
584
    }
585
586
    /**
587
     * Retrieve the value from the current session or assigned model. This is
588
     * a public alias for `old`.
589
     *
590
     * @param string $name
591
     * @param mixed $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...
592
     *
593
     * @return mixed
594
     */
595
    public function value($name, $default = null)
596
    {
597
        return $this->old($name, $default);
598
    }
599
600
    /**
601
     * @param string $name
602
     *
603
     * @return string
604
     */
605
    protected function fieldName($name)
606
    {
607
        return $name;
608
    }
609
610
    protected function ensureModelIsAvailable()
611
    {
612
        if (empty($this->model)) {
613
            throw new Exception('Method requires a model to be set on the html builder');
614
        }
615
    }
616
617
    /**
618
     * @param string $value
619
     * @param string $format DateTime formatting string supported by date_format()
620
     * @return string
621
     */
622
    protected function formatDateTime($value, $format)
623
    {
624
        if (empty($value)) {
625
            return $value;
626
        }
627
628
        try {
629
            $date = new DateTimeImmutable($value);
630
631
            return $date->format($format);
632
        } catch (\Exception $e) {
633
            return $value;
634
        }
635
    }
636
}
637