Completed
Pull Request — master (#141)
by
unknown
01:53
created

Html   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 519
Duplicated Lines 3.28 %

Coupling/Cohesion

Components 1
Dependencies 21

Importance

Changes 0
Metric Value
dl 17
loc 519
rs 3.6
c 0
b 0
f 0
wmc 60
lcom 1
cbo 21

40 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A a() 0 6 1
A i() 0 5 1
A value() 0 4 1
A fieldName() 0 4 1
A ensureModelIsAvailable() 0 6 2
A button() 0 7 1
A class() 0 13 2
A checkbox() 0 6 1
A div() 0 4 1
A email() 0 4 1
A date() 0 4 1
A time() 0 4 1
A element() 0 4 1
A fieldset() 0 6 2
A form() 0 20 4
A hidden() 0 4 1
A img() 0 6 1
A label() 0 6 1
A legend() 0 4 1
A mailto() 0 4 2
A multiselect() 9 9 2
A option() 0 7 1
A password() 0 4 1
A radio() 0 7 4
A select() 8 8 2
A span() 0 4 1
A submit() 0 4 1
A reset() 0 4 1
A tel() 0 4 2
A text() 0 4 1
A file() 0 6 1
A textarea() 0 7 1
A token() 0 7 1
A model() 0 6 1
A modelForm() 0 6 1
A endModel() 0 6 1
A closeModelForm() 0 6 1
A old() 0 18 5
A input() 0 10 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Html often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Html, and based on these observations, apply Extract Interface, too.

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