Completed
Push — master ( 821a00...b420eb )
by Sebastian
01:52 queued 47s
created

Html::range()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Html;
4
5
use DateTimeImmutable;
6
use Spatie\Html\Elements\A;
7
use Spatie\Html\Elements\I;
8
use Illuminate\Http\Request;
9
use Spatie\Html\Elements\Div;
10
use Spatie\Html\Elements\Img;
11
use Spatie\Html\Elements\File;
12
use Spatie\Html\Elements\Form;
13
use Spatie\Html\Elements\Span;
14
use Spatie\Html\Elements\Input;
15
use Spatie\Html\Elements\Label;
16
use Spatie\Html\Elements\Button;
17
use Spatie\Html\Elements\Legend;
18
use Spatie\Html\Elements\Option;
19
use Spatie\Html\Elements\Select;
20
use Spatie\Html\Elements\Element;
21
use Illuminate\Support\Collection;
22
use Illuminate\Support\HtmlString;
23
use Spatie\Html\Elements\Fieldset;
24
use Spatie\Html\Elements\Textarea;
25
use Illuminate\Support\Traits\Macroable;
26
use Illuminate\Contracts\Support\Htmlable;
27
28
class Html
29
{
30
    use Macroable;
31
32
    const HTML_DATE_FORMAT = 'Y-m-d';
33
    const HTML_TIME_FORMAT = 'H:i:s';
34
35
    /** @var \Illuminate\Http\Request */
36
    protected $request;
37
38
    /** @var \ArrayAccess|array */
39
    protected $model;
40
41
    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...
42
    {
43
        $this->request = $request;
44
    }
45
46
    /**
47
     * @param string|null $href
48
     * @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...
49
     *
50
     * @return \Spatie\Html\Elements\A
51
     */
52
    public function a($href = null, $contents = null)
53
    {
54
        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...
55
            ->attributeIf($href, 'href', $href)
56
            ->html($contents);
57
    }
58
59
    /**
60
     * @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...
61
     * @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...
62
     *
63
     * @return \Spatie\Html\Elements\I
64
     */
65
    public function i($contents = null)
66
    {
67
        return I::create()
68
            ->html($contents);
69
    }
70
71
    /**
72
     * @param string|null $type
73
     * @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...
74
     *
75
     * @return \Spatie\Html\Elements\Button
76
     */
77
    public function button($contents = null, $type = null, $name = null)
78
    {
79
        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...
80
            ->attributeIf($type, 'type', $type)
81
            ->attributeIf($name, 'name', $this->fieldName($name))
82
            ->html($contents);
83
    }
84
85
    /**
86
     * @param \Illuminate\Support\Collection|iterable|string $classes
87
     *
88
     * @return \Illuminate\Contracts\Support\Htmlable
89
     */
90
    public function class($classes): Htmlable
91
    {
92
        if ($classes instanceof Collection) {
93
            $classes = $classes->toArray();
94
        }
95
96
        $attributes = new Attributes();
97
        $attributes->addClass($classes);
98
99
        return new HtmlString(
100
            $attributes->render()
101
        );
102
    }
103
104
    /**
105
     * @param string|null $name
106
     * @param bool $checked
107
     * @param string|null $value
108
     *
109
     * @return \Spatie\Html\Elements\Input
110
     */
111
    public function checkbox($name = null, $checked = null, $value = '1')
112
    {
113
        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...
114
            ->attributeIf(! is_null($value), 'value', $value)
115
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
116
    }
117
118
    /**
119
     * @param \Spatie\Html\HtmlElement|string|null $contents
120
     *
121
     * @return \Spatie\Html\Elements\Div
122
     */
123
    public function div($contents = null)
124
    {
125
        return Div::create()->children($contents);
126
    }
127
128
    /**
129
     * @param string|null $name
130
     * @param string|null $value
131
     *
132
     * @return \Spatie\Html\Elements\Input
133
     */
134
    public function email($name = null, $value = null)
135
    {
136
        return $this->input('email', $name, $value);
137
    }
138
139
    /**
140
     * @param string|null $name
141
     * @param string|null $value
142
     * @param bool $format
143
     *
144
     * @return \Spatie\Html\Elements\Input
145
     */
146 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...
147
    {
148
        $element = $this->input('date', $name, $value);
149
150
        if (! $format || empty($element->getAttribute('value'))) {
151
            return $element;
152
        }
153
154
        return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_DATE_FORMAT));
155
    }
156
157
    /**
158
     * @param string|null $name
159
     * @param string|null $value
160
     * @param string|null $min
161
     * @param string|null $max
162
     * @param string|null $step
163
     *
164
     * @return \Spatie\Html\Elements\Input
165
     */
166
    public function range($name = '', $value = '', $min = null, $max = null, $step = null)
167
    {
168
        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...
169
            ->attributeIfNotNull($min, 'min', $min)
170
            ->attributeIfNotNull($max, 'max', $max)
171
            ->attributeIfNotNull($step, 'step', $step);
172
    }
173
174
    /**
175
     * @param string|null $name
176
     * @param string|null $value
177
     * @param bool $format
178
     *
179
     * @return \Spatie\Html\Elements\Input
180
     */
181 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...
182
    {
183
        $element = $this->input('time', $name, $value);
184
185
        if (! $format || empty($element->getAttribute('value'))) {
186
            return $element;
187
        }
188
189
        return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_TIME_FORMAT));
190
    }
191
192
    /**
193
     * @param string $tag
194
     *
195
     * @return \Spatie\Html\Elements\Element
196
     */
197
    public function element($tag)
198
    {
199
        return Element::withTag($tag);
200
    }
201
202
    /**
203
     * @param string|null $type
204
     * @param string|null $name
205
     * @param string|null $value
206
     *
207
     * @return \Spatie\Html\Elements\Input
208
     */
209
    public function input($type = null, $name = null, $value = null)
210
    {
211
        $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...
212
213
        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...
214
            ->attributeIf($type, 'type', $type)
215
            ->attributeIf($name, 'name', $this->fieldName($name))
216
            ->attributeIf($name, 'id', $this->fieldName($name))
217
            ->attributeIf($hasValue, 'value', $this->old($name, $value));
218
    }
219
220
    /**
221
     * @param \Spatie\Html\HtmlElement|string|null $legend
222
     *
223
     * @return \Spatie\Html\Elements\Fieldset
224
     */
225
    public function fieldset($legend = null)
226
    {
227
        return $legend ?
228
            Fieldset::create()->legend($legend) :
229
            Fieldset::create();
230
    }
231
232
    /**
233
     * @param string $method
234
     * @param string|null $action
235
     *
236
     * @return \Spatie\Html\Elements\Form
237
     */
238
    public function form($method = 'POST', $action = null)
239
    {
240
        $method = strtoupper($method);
241
        $form = Form::create();
242
243
        // If Laravel needs to spoof the form's method, we'll append a hidden
244
        // field containing the actual method
245
        if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) {
246
            $form = $form->addChild($this->hidden('_method')->value($method));
247
        }
248
249
        // On any other method that get, the form needs a CSRF token
250
        if ($method !== 'GET') {
251
            $form = $form->addChild($this->token());
252
        }
253
254
        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...
255
            ->method($method === 'GET' ? 'GET' : 'POST')
256
            ->attributeIf($action, 'action', $action);
257
    }
258
259
    /**
260
     * @param string|null $name
261
     * @param string|null $value
262
     *
263
     * @return \Spatie\Html\Elements\Input
264
     */
265
    public function hidden($name = null, $value = null)
266
    {
267
        return $this->input('hidden', $name, $value);
268
    }
269
270
    /**
271
     * @param string|null $src
272
     * @param string|null $alt
273
     *
274
     * @return \Spatie\Html\Elements\Img
275
     */
276
    public function img($src = null, $alt = null)
277
    {
278
        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...
279
            ->attributeIf($src, 'src', $src)
280
            ->attributeIf($alt, 'alt', $alt);
281
    }
282
283
    /**
284
     * @param \Spatie\Html\HtmlElement|iterable|string|null $contents
285
     * @param string|null $for
286
     *
287
     * @return \Spatie\Html\Elements\Label
288
     */
289
    public function label($contents = null, $for = null)
290
    {
291
        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...
292
            ->attributeIf($for, 'for', $this->fieldName($for))
293
            ->children($contents);
294
    }
295
296
    /**
297
     * @param \Spatie\Html\HtmlElement|string|null $contents
298
     *
299
     * @return \Spatie\Html\Elements\Legend
300
     */
301
    public function legend($contents = null)
302
    {
303
        return Legend::create()->html($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 301 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...
304
    }
305
306
    /**
307
     * @param string $email
308
     * @param string|null $text
309
     *
310
     * @return \Spatie\Html\Elements\A
311
     */
312
    public function mailto($email, $text = null)
313
    {
314
        return $this->a('mailto:'.$email, $text ?: $email);
315
    }
316
317
    /**
318
     * @param string|null $name
319
     * @param iterable $options
320
     * @param string|iterable|null $value
321
     *
322
     * @return \Spatie\Html\Elements\Select
323
     */
324 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...
325
    {
326
        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...
327
            ->attributeIf($name, 'name', $this->fieldName($name))
328
            ->attributeIf($name, 'id', $this->fieldName($name))
329
            ->options($options)
330
            ->value($name ? $this->old($name, $value) : $value)
331
            ->multiple();
332
    }
333
334
    /**
335
     * @param string|null $text
336
     * @param string|null $value
337
     * @param bool $selected
338
     *
339
     * @return \Spatie\Html\Elements\Option
340
     */
341
    public function option($text = null, $value = null, $selected = false)
342
    {
343
        return Option::create()
344
            ->text($text)
345
            ->value($value)
346
            ->selectedIf($selected);
347
    }
348
349
    /**
350
     * @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...
351
     *
352
     * @return \Spatie\Html\Elements\Input
353
     */
354
    public function password($name = null)
355
    {
356
        return $this->input('password', $name);
357
    }
358
359
    /**
360
     * @param string|null $name
361
     * @param bool $checked
362
     * @param string|null $value
363
     *
364
     * @return \Spatie\Html\Elements\Input
365
     */
366
    public function radio($name = null, $checked = null, $value = null)
367
    {
368
        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...
369
            ->attributeIf($name, 'id', $value === null ? $name : ($name.'_'.str_slug($value)))
0 ignored issues
show
Deprecated Code introduced by
The function str_slug() has been deprecated with message: Str::slug() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
370
            ->attributeIf(! is_null($value), 'value', $value)
371
            ->attributeIf((! is_null($value) && $this->old($name) == $value) || $checked, 'checked');
372
    }
373
374
    /**
375
     * @param string|null $name
376
     * @param iterable $options
377
     * @param string|iterable|null $value
378
     *
379
     * @return \Spatie\Html\Elements\Select
380
     */
381 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...
382
    {
383
        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...
384
            ->attributeIf($name, 'name', $this->fieldName($name))
385
            ->attributeIf($name, 'id', $this->fieldName($name))
386
            ->options($options)
387
            ->value($name ? $this->old($name, $value) : $value);
388
    }
389
390
    /**
391
     * @param \Spatie\Html\HtmlElement|string|null $contents
392
     *
393
     * @return \Spatie\Html\Elements\Span
394
     */
395
    public function span($contents = null)
396
    {
397
        return Span::create()->children($contents);
398
    }
399
400
    /**
401
     * @param string|null $text
402
     *
403
     * @return \Spatie\Html\Elements\Button
404
     */
405
    public function submit($text = null)
406
    {
407
        return $this->button($text, 'submit');
408
    }
409
410
    /**
411
     * @param string|null $text
412
     *
413
     * @return \Spatie\Html\Elements\Button
414
     */
415
    public function reset($text = null)
416
    {
417
        return $this->button($text, 'reset');
418
    }
419
420
    /**
421
     * @param string $number
422
     * @param string|null $text
423
     *
424
     * @return \Spatie\Html\Elements\A
425
     */
426
    public function tel($number, $text = null)
427
    {
428
        return $this->a('tel:'.$number, $text ?: $number);
429
    }
430
431
    /**
432
     * @param string|null $name
433
     * @param string|null $value
434
     *
435
     * @return \Spatie\Html\Elements\Input
436
     */
437
    public function text($name = null, $value = null)
438
    {
439
        return $this->input('text', $name, $value);
440
    }
441
442
    /**
443
     * @param string|null $name
444
     *
445
     * @return \Spatie\Html\Elements\File
446
     */
447
    public function file($name = null)
448
    {
449
        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...
450
            ->attributeIf($name, 'name', $this->fieldName($name))
451
            ->attributeIf($name, 'id', $this->fieldName($name));
452
    }
453
454
    /**
455
     * @param string|null $name
456
     * @param string|null $value
457
     *
458
     * @return \Spatie\Html\Elements\Textarea
459
     */
460
    public function textarea($name = null, $value = null)
461
    {
462
        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...
463
            ->attributeIf($name, 'name', $this->fieldName($name))
464
            ->attributeIf($name, 'id', $this->fieldName($name))
465
            ->value($this->old($name, $value));
466
    }
467
468
    /**
469
     * @return \Spatie\Html\Elements\Input
470
     */
471
    public function token()
472
    {
473
        return $this
474
            ->hidden()
475
            ->name('_token')
476
            ->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...
477
    }
478
479
    /**
480
     * @param \ArrayAccess|array $model
481
     *
482
     * @return $this
483
     */
484
    public function model($model)
485
    {
486
        $this->model = $model;
487
488
        return $this;
489
    }
490
491
    /**
492
     * @param \ArrayAccess|array $model
493
     * @param string|null $method
494
     * @param string|null $action
495
     *
496
     * @return \Spatie\Html\Elements\Form
497
     */
498
    public function modelForm($model, $method = 'POST', $action = null): Form
499
    {
500
        $this->model($model);
501
502
        return $this->form($method, $action);
503
    }
504
505
    /**
506
     * @return $this
507
     */
508
    public function endModel()
509
    {
510
        $this->model = null;
511
512
        return $this;
513
    }
514
515
    /**
516
     * @return \Illuminate\Contracts\Support\Htmlable
517
     */
518
    public function closeModelForm(): Htmlable
519
    {
520
        $this->endModel();
521
522
        return $this->form()->close();
523
    }
524
525
    /**
526
     * @param string $name
527
     * @param mixed $value
528
     *
529
     * @return mixed
530
     */
531
    protected function old($name, $value = null)
532
    {
533
        if (empty($name)) {
534
            return;
535
        }
536
537
        // Convert array format (sth[1]) to dot notation (sth.1)
538
        $name = preg_replace('/\[(.+)\]/U', '.$1', $name);
539
540
        // If there's no default value provided, the html builder currently
541
        // has a model assigned and there aren't old input items,
542
        // try to retrieve a value from the model.
543
        if (is_null($value) && $this->model && empty($this->request->old())) {
544
            $value = data_get($this->model, $name) ?? '';
545
        }
546
547
        return $this->request->old($name, $value);
548
    }
549
550
    /**
551
     * Retrieve the value from the current session or assigned model. This is
552
     * a public alias for `old`.
553
     *
554
     * @param string $name
555
     * @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...
556
     *
557
     * @return mixed
558
     */
559
    public function value($name, $default = null)
560
    {
561
        return $this->old($name, $default);
562
    }
563
564
    /**
565
     * @param string $name
566
     *
567
     * @return string
568
     */
569
    protected function fieldName($name)
570
    {
571
        return $name;
572
    }
573
574
    protected function ensureModelIsAvailable()
575
    {
576
        if (empty($this->model)) {
577
            throw new Exception('Method requires a model to be set on the html builder');
578
        }
579
    }
580
581
    /**
582
     * @param string $value
583
     * @param string $format DateTime formatting string supported by date_format()
584
     * @return string
585
     */
586
    protected function formatDateTime($value, $format)
587
    {
588
        if (empty($value)) {
589
            return $value;
590
        }
591
592
        try {
593
            $date = new DateTimeImmutable($value);
594
595
            return $date->format($format);
596
        } catch (\Exception $e) {
597
            return $value;
598
        }
599
    }
600
}
601