Completed
Pull Request — master (#88)
by
unknown
01:34
created

Html::i()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Html;
4
5
use Spatie\Html\Elements\A;
6
use Illuminate\Http\Request;
7
use Spatie\Html\Elements\Div;
8
use Spatie\Html\Elements\File;
9
use Spatie\Html\Elements\Form;
10
use Spatie\Html\Elements\Span;
11
use Spatie\Html\Elements\Input;
12
use Spatie\Html\Elements\Label;
13
use Spatie\Html\Elements\Button;
14
use Spatie\Html\Elements\Legend;
15
use Spatie\Html\Elements\Option;
16
use Spatie\Html\Elements\Select;
17
use Spatie\Html\Elements\Element;
18
use Illuminate\Support\Collection;
19
use Illuminate\Support\HtmlString;
20
use Spatie\Html\Elements\Fieldset;
21
use Spatie\Html\Elements\Textarea;
22
use Illuminate\Support\Traits\Macroable;
23
use Illuminate\Contracts\Support\Htmlable;
24
use Spatie\Html\Elements\Img;
25
use Spatie\Html\Elements\I;
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)
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
            ->html($contents);
78
    }
79
80
    /**
81
     * @param \Illuminate\Support\Collection|iterable|string $classes
82
     *
83
     * @return \Illuminate\Contracts\Support\Htmlable
84
     */
85
    public function class($classes): Htmlable
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
86
    {
87
        if ($classes instanceof Collection) {
88
            $classes = $classes->toArray();
89
        }
90
91
        $attributes = new Attributes();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $attributes.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
92
        $attributes->addClass($classes);
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $attributes.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
93
94
        return new HtmlString(
95
            $attributes->render()
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $attributes.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
96
        );
97
    }
98
99
    /**
100
     * @param string|null $name
101
     * @param bool $checked
102
     * @param string|null $value
103
     *
104
     * @return \Spatie\Html\Elements\Input
105
     */
106
    public function checkbox($name = null, $checked = false, $value = '1')
107
    {
108
        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...
109
            ->attribute('type', 'checkbox')
110
            ->attributeIf($name, 'name', $this->fieldName($name))
111
            ->attributeIf($name, 'id', $this->fieldName($name))
112
            ->attributeIf(! is_null($value), 'value', $value)
113
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
114
    }
115
116
    /**
117
     * @param \Spatie\Html\HtmlElement|string|null $contents
118
     *
119
     * @return \Spatie\Html\Elements\Div
120
     */
121
    public function div($contents = null)
122
    {
123
        return Div::create()->children($contents);
124
    }
125
126
    /**
127
     * @param string|null $name
128
     * @param string|null $value
129
     *
130
     * @return \Spatie\Html\Elements\Input
131
     */
132
    public function email($name = '', $value = '')
133
    {
134
        return $this->input('email', $name, $value);
135
    }
136
137
    /**
138
     * @param string|null $name
139
     * @param string|null $value
140
     *
141
     * @return \Spatie\Html\Elements\Input
142
     */
143
    public function date($name = '', $value = '')
144
    {
145
        return $this->input('date', $name, $value);
146
    }
147
148
    /**
149
     * @param string|null $name
150
     * @param string|null $value
151
     *
152
     * @return \Spatie\Html\Elements\Input
153
     */
154
    public function time($name = '', $value = '')
155
    {
156
        return $this->input('time', $name, $value);
157
    }
158
159
    /**
160
     * @param string $tag
161
     *
162
     * @return \Spatie\Html\Elements\Element
163
     */
164
    public function element($tag)
165
    {
166
        return Element::withTag($tag);
167
    }
168
169
    /**
170
     * @param string|null $type
171
     * @param string|null $name
172
     * @param string|null $value
173
     *
174
     * @return \Spatie\Html\Elements\Input
175
     */
176
    public function input($type = null, $name = null, $value = null)
177
    {
178
        $hasValue = $name && (! is_null($this->old($name, $value)) || ! is_null($value));
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
179
180
        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...
181
            ->attributeIf($type, 'type', $type)
182
            ->attributeIf($name, 'name', $this->fieldName($name))
183
            ->attributeIf($name, 'id', $this->fieldName($name))
184
            ->attributeIf($hasValue, 'value', $this->old($name, $value));
185
    }
186
187
    /**
188
     * @param \Spatie\Html\HtmlElement|string|null $legend
189
     *
190
     * @return \Spatie\Html\Elements\Fieldset
191
     */
192
    public function fieldset($legend = null)
193
    {
194
        return $legend ?
195
            Fieldset::create()->legend($legend) :
196
            Fieldset::create();
197
    }
198
199
    /**
200
     * @param string $method
201
     * @param string|null $action
202
     *
203
     * @return \Spatie\Html\Elements\Form
204
     */
205
    public function form($method = 'POST', $action = null)
206
    {
207
        $method = strtoupper($method);
208
        $form = Form::create();
209
210
        // If Laravel needs to spoof the form's method, we'll append a hidden
211
        // field containing the actual method
212
        if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) {
213
            $form = $form->addChild($this->hidden('_method')->value($method));
214
        }
215
216
        // On any other method that get, the form needs a CSRF token
217
        if ($method !== 'GET') {
218
            $form = $form->addChild($this->token());
219
        }
220
221
        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...
222
            ->method($method === 'GET' ? 'GET' : 'POST')
223
            ->attributeIf($action, 'action', $action);
224
    }
225
226
    /**
227
     * @param string|null $name
228
     * @param string|null $value
229
     *
230
     * @return \Spatie\Html\Elements\Input
231
     */
232
    public function hidden($name = null, $value = null)
233
    {
234
        return $this->input('hidden', $name, $value);
235
    }
236
237
    /**
238
     * @param string|null $src
239
     * @param string|null $alt
240
     *
241
     * @return \Spatie\Html\Elements\Img
242
     */
243
    public function img($src = null, $alt = null)
244
    {
245
        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...
246
            ->attributeIf($src, 'src', $src)
247
            ->attributeIf($alt, 'alt', $alt);
248
    }
249
250
    /**
251
     * @param \Spatie\Html\HtmlElement|iterable|string|null $contents
252
     * @param string|null $for
253
     *
254
     * @return \Spatie\Html\Elements\Label
255
     */
256
    public function label($contents = null, $for = null)
257
    {
258
        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...
259
            ->attributeIf($for, 'for', $this->fieldName($for))
260
            ->children($contents);
261
    }
262
263
    /**
264
     * @param \Spatie\Html\HtmlElement|string|null $contents
265
     *
266
     * @return \Spatie\Html\Elements\Legend
267
     */
268
    public function legend($contents = null)
269
    {
270
        return Legend::create()->html($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 268 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...
271
    }
272
273
    /**
274
     * @param string $email
275
     * @param string|null $text
276
     *
277
     * @return \Spatie\Html\Elements\A
278
     */
279
    public function mailto($email, $text = null)
280
    {
281
        return $this->a('mailto:'.$email, $text);
282
    }
283
284
    /**
285
     * @param string|null $name
286
     * @param iterable $options
287
     * @param string|iterable|null $value
288
     *
289
     * @return \Spatie\Html\Elements\Select
290
     */
291 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...
292
    {
293
        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...
294
            ->attributeIf($name, 'name', $this->fieldName($name))
295
            ->attributeIf($name, 'id', $this->fieldName($name))
296
            ->options($options)
297
            ->value($name ? $this->old($name, $value) : $value)
298
            ->multiple();
299
    }
300
301
    /**
302
     * @param string|null $text
303
     * @param string|null $value
304
     * @param bool $selected
305
     *
306
     * @return \Spatie\Html\Elements\Option
307
     */
308
    public function option($text = null, $value = null, $selected = false)
309
    {
310
        return Option::create()
311
            ->text($text)
312
            ->value($value)
313
            ->selectedIf($selected);
314
    }
315
316
    /**
317
     * @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...
318
     *
319
     * @return \Spatie\Html\Elements\Input
320
     */
321
    public function password($name = null)
322
    {
323
        return $this->input('password', $name);
324
    }
325
326
    /**
327
     * @param string|null $name
328
     * @param bool $checked
329
     * @param string|null $value
330
     *
331
     * @return \Spatie\Html\Elements\Input
332
     */
333
    public function radio($name = null, $checked = false, $value = null)
334
    {
335
        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...
336
            ->attributeIf((bool) $this->old($name, $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);
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
        // If there's no default value provided, and the html builder currently
503
        // has a model assigned, try to retrieve a value from the model.
504
        if (empty($value) && $this->model) {
505
            $value = $this->model[$name] ?? '';
506
        }
507
508
        // Convert array format (sth[1]) to dot notation (sth.1)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
509
        $name = preg_replace('/\[(.+)\]/U', '.$1', $name);
510
511
        return $this->request->old($name, $value);
512
    }
513
514
    /**
515
     * Retrieve the value from the current session or assigned model. This is
516
     * a public alias for `old`.
517
     *
518
     * @param string $name
519
     * @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...
520
     *
521
     * @return mixed
522
     */
523
    public function value($name, $default = null)
524
    {
525
        return $this->old($name, $default);
526
    }
527
528
    /**
529
     * @param string $name
530
     *
531
     * @return string
532
     */
533
    protected function fieldName($name)
534
    {
535
        return $name;
536
    }
537
538
    protected function ensureModelIsAvailable()
539
    {
540
        if (empty($this->model)) {
541
            throw new Exception('Method requires a model to be set on the html builder');
542
        }
543
    }
544
}
545