Completed
Pull Request — master (#143)
by
unknown
01:21
created

Html::button()   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 3
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 Illuminate\Support\Str;
7
use Spatie\Html\Elements\A;
8
use Spatie\Html\Elements\I;
9
use Illuminate\Http\Request;
10
use Spatie\Html\Elements\Div;
11
use Spatie\Html\Elements\Img;
12
use Spatie\Html\Elements\File;
13
use Spatie\Html\Elements\Form;
14
use Spatie\Html\Elements\Span;
15
use Spatie\Html\Elements\Input;
16
use Spatie\Html\Elements\Label;
17
use Spatie\Html\Elements\Button;
18
use Spatie\Html\Elements\Legend;
19
use Spatie\Html\Elements\Option;
20
use Spatie\Html\Elements\Select;
21
use Spatie\Html\Elements\Element;
22
use Illuminate\Support\Collection;
23
use Illuminate\Support\HtmlString;
24
use Spatie\Html\Elements\Fieldset;
25
use Spatie\Html\Elements\Textarea;
26
use Illuminate\Support\Traits\Macroable;
27
use Illuminate\Contracts\Support\Htmlable;
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 string|null $min
162
     * @param string|null $max
163
     * @param string|null $step
164
     *
165
     * @return \Spatie\Html\Elements\Input
166
     */
167
    public function range($name = '', $value = '', $min = null, $max = null, $step = null)
168
    {
169
        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...
170
            ->attributeIfNotNull($min, 'min', $min)
171
            ->attributeIfNotNull($max, 'max', $max)
172
            ->attributeIfNotNull($step, 'step', $step);
173
    }
174
175
    /**
176
     * @param string|null $name
177
     * @param string|null $value
178
     * @param bool $format
179
     *
180
     * @return \Spatie\Html\Elements\Input
181
     */
182 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...
183
    {
184
        $element = $this->input('time', $name, $value);
185
186
        if (!$format || empty($element->getAttribute('value'))) {
187
            return $element;
188
        }
189
190
        return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_TIME_FORMAT));
191
    }
192
193
    /**
194
     * @param string $tag
195
     *
196
     * @return \Spatie\Html\Elements\Element
197
     */
198
    public function element($tag)
199
    {
200
        return Element::withTag($tag);
201
    }
202
203
    /**
204
     * @param string|null $type
205
     * @param string|null $name
206
     * @param string|null $value
207
     *
208
     * @return \Spatie\Html\Elements\Input
209
     */
210
    public function input($type = null, $name = null, $value = null)
211
    {
212
        $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...
213
214
        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...
215
            ->attributeIf($type, 'type', $type)
216
            ->attributeIf($name, 'name', $this->fieldName($name))
217
            ->attributeIf($name, 'id', $this->fieldName($name))
218
            ->attributeIf($hasValue, 'value', $this->old($name, $value));
219
    }
220
221
    /**
222
     * @param \Spatie\Html\HtmlElement|string|null $legend
223
     *
224
     * @return \Spatie\Html\Elements\Fieldset
225
     */
226
    public function fieldset($legend = null)
227
    {
228
        return $legend ?
229
            Fieldset::create()->legend($legend) : 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 $name
336
     * @param string|null $value
337
     * @param string|null $step
338
     *
339
     * @return \Spatie\Html\Elements\Input
340
     */
341
    public function number($name = null, $value = null, $step = null)
342
    {
343
        return $this->input('number', $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...
344
                ->attributeIf($step, 'step', $step);
345
    }
346
347
    /**
348
     * @param string|null $text
349
     * @param string|null $value
350
     * @param bool $selected
351
     *
352
     * @return \Spatie\Html\Elements\Option
353
     */
354
    public function option($text = null, $value = null, $selected = false)
355
    {
356
        return Option::create()
357
            ->text($text)
358
            ->value($value)
359
            ->selectedIf($selected);
360
    }
361
362
    /**
363
     * @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...
364
     *
365
     * @return \Spatie\Html\Elements\Input
366
     */
367
    public function password($name = null)
368
    {
369
        return $this->input('password', $name);
370
    }
371
372
    /**
373
     * @param string|null $name
374
     * @param bool $checked
375
     * @param string|null $value
376
     *
377
     * @return \Spatie\Html\Elements\Input
378
     */
379
    public function radio($name = null, $checked = null, $value = null)
380
    {
381
        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...
382
            ->attributeIf($name, 'id', $value === null ? $name : ($name . '_' . Str::slug($value)))
383
            ->attributeIf(!is_null($value), 'value', $value)
384
            ->attributeIf((!is_null($value) && $this->old($name) == $value) || $checked, 'checked');
385
    }
386
387
    /**
388
     * @param string|null $name
389
     * @param iterable $options
390
     * @param string|iterable|null $value
391
     *
392
     * @return \Spatie\Html\Elements\Select
393
     */
394 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...
395
    {
396
        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...
397
            ->attributeIf($name, 'name', $this->fieldName($name))
398
            ->attributeIf($name, 'id', $this->fieldName($name))
399
            ->options($options)
400
            ->value($name ? $this->old($name, $value) : $value);
401
    }
402
403
    /**
404
     * @param \Spatie\Html\HtmlElement|string|null $contents
405
     *
406
     * @return \Spatie\Html\Elements\Span
407
     */
408
    public function span($contents = null)
409
    {
410
        return Span::create()->children($contents);
411
    }
412
413
    /**
414
     * @param string|null $text
415
     *
416
     * @return \Spatie\Html\Elements\Button
417
     */
418
    public function submit($text = null)
419
    {
420
        return $this->button($text, 'submit');
421
    }
422
423
    /**
424
     * @param string|null $text
425
     *
426
     * @return \Spatie\Html\Elements\Button
427
     */
428
    public function reset($text = null)
429
    {
430
        return $this->button($text, 'reset');
431
    }
432
433
    /**
434
     * @param string $number
435
     * @param string|null $text
436
     *
437
     * @return \Spatie\Html\Elements\A
438
     */
439
    public function tel($number, $text = null)
440
    {
441
        return $this->a('tel:' . $number, $text ?: $number);
442
    }
443
444
    /**
445
     * @param string|null $name
446
     * @param string|null $value
447
     *
448
     * @return \Spatie\Html\Elements\Input
449
     */
450
    public function text($name = null, $value = null)
451
    {
452
        return $this->input('text', $name, $value);
453
    }
454
455
    /**
456
     * @param string|null $name
457
     *
458
     * @return \Spatie\Html\Elements\File
459
     */
460
    public function file($name = null)
461
    {
462
        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...
463
            ->attributeIf($name, 'name', $this->fieldName($name))
464
            ->attributeIf($name, 'id', $this->fieldName($name));
465
    }
466
467
    /**
468
     * @param string|null $name
469
     * @param string|null $value
470
     *
471
     * @return \Spatie\Html\Elements\Textarea
472
     */
473
    public function textarea($name = null, $value = null)
474
    {
475
        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...
476
            ->attributeIf($name, 'name', $this->fieldName($name))
477
            ->attributeIf($name, 'id', $this->fieldName($name))
478
            ->value($this->old($name, $value));
479
    }
480
481
    /**
482
     * @return \Spatie\Html\Elements\Input
483
     */
484
    public function token()
485
    {
486
        return $this
487
            ->hidden()
488
            ->name('_token')
489
            ->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...
490
    }
491
492
    /**
493
     * @param \ArrayAccess|array $model
494
     *
495
     * @return $this
496
     */
497
    public function model($model)
498
    {
499
        $this->model = $model;
500
501
        return $this;
502
    }
503
504
    /**
505
     * @param \ArrayAccess|array $model
506
     * @param string|null $method
507
     * @param string|null $action
508
     *
509
     * @return \Spatie\Html\Elements\Form
510
     */
511
    public function modelForm($model, $method = 'POST', $action = null): Form
512
    {
513
        $this->model($model);
514
515
        return $this->form($method, $action);
516
    }
517
518
    /**
519
     * @return $this
520
     */
521
    public function endModel()
522
    {
523
        $this->model = null;
524
525
        return $this;
526
    }
527
528
    /**
529
     * @return \Illuminate\Contracts\Support\Htmlable
530
     */
531
    public function closeModelForm(): Htmlable
532
    {
533
        $this->endModel();
534
535
        return $this->form()->close();
536
    }
537
538
    /**
539
     * @param string $name
540
     * @param mixed $value
541
     *
542
     * @return mixed
543
     */
544
    protected function old($name, $value = null)
545
    {
546
        if (empty($name)) {
547
            return;
548
        }
549
550
        // Convert array format (sth[1]) to dot notation (sth.1)
551
        $name = preg_replace('/\[(.+)\]/U', '.$1', $name);
552
553
        // If there's no default value provided, the html builder currently
554
        // has a model assigned and there aren't old input items,
555
        // try to retrieve a value from the model.
556
        if (is_null($value) && $this->model && empty($this->request->old())) {
557
            $value = data_get($this->model, $name) ?? '';
558
        }
559
560
        return $this->request->old($name, $value);
561
    }
562
563
    /**
564
     * Retrieve the value from the current session or assigned model. This is
565
     * a public alias for `old`.
566
     *
567
     * @param string $name
568
     * @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...
569
     *
570
     * @return mixed
571
     */
572
    public function value($name, $default = null)
573
    {
574
        return $this->old($name, $default);
575
    }
576
577
    /**
578
     * @param string $name
579
     *
580
     * @return string
581
     */
582
    protected function fieldName($name)
583
    {
584
        return $name;
585
    }
586
587
    protected function ensureModelIsAvailable()
588
    {
589
        if (empty($this->model)) {
590
            throw new Exception('Method requires a model to be set on the html builder');
591
        }
592
    }
593
594
    /**
595
     * @param string $value
596
     * @param string $format DateTime formatting string supported by date_format()
597
     * @return string
598
     */
599
    protected function formatDateTime($value, $format)
600
    {
601
        if (empty($value)) {
602
            return $value;
603
        }
604
605
        try {
606
            $date = new DateTimeImmutable($value);
607
608
            return $date->format($format);
609
        } catch (\Exception $e) {
610
            return $value;
611
        }
612
    }
613
}
614