Completed
Push — master ( 1f8dca...9227ef )
by Sebastian
03:04
created

Html::radio()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
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\Form;
9
use Spatie\Html\Elements\Span;
10
use Spatie\Html\Elements\Input;
11
use Spatie\Html\Elements\Label;
12
use Spatie\Html\Elements\Button;
13
use Spatie\Html\Elements\Legend;
14
use Spatie\Html\Elements\Option;
15
use Spatie\Html\Elements\Select;
16
use Spatie\Html\Elements\Element;
17
use Spatie\Html\Elements\Fieldset;
18
use Spatie\Html\Elements\Textarea;
19
use Illuminate\Contracts\Support\Htmlable;
20
21
class Html
22
{
23
    /** @var \Illuminate\Http\Request */
24
    protected $request;
25
26
    /** @var \ArrayAccess|array */
27
    protected $model;
28
29
    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...
30
    {
31
        $this->request = $request;
32
    }
33
34
    /**
35
     * @param string $href
36
     * @param string $text
37
     *
38
     * @return \Spatie\Html\Elements\A
39
     */
40
    public function a(?string $href = '', ?string $text = '')
41
    {
42
        return A::create()
43
            ->attributeIf($href, 'href', $href)
0 ignored issues
show
Documentation introduced by
$href is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
            ->text($text);
45
    }
46
47
    /**
48
     * @param string $type
49
     * @param string $text
50
     *
51
     * @return \Spatie\Html\Elements\Button
52
     */
53
    public function button(?string $text = '', ?string $type = 'button')
54
    {
55
        return Button::create()
56
            ->type($type)
57
            ->text($text);
58
    }
59
60
    /**
61
     * @param string $value
62
     *
63
     * @return \Spatie\Html\Elements\Input
64
     */
65
    public function checkbox(string $name = '', ?bool $checked = false, ?string $value = '1')
66
    {
67
        return $this->input('checkbox', $name, $value)
68
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
0 ignored issues
show
Documentation introduced by
$checked is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
    }
70
71
    /**
72
     * @param \Spatie\Html\HtmlElement|string $contents
73
     *
74
     * @return \Spatie\Html\Elements\Div
75
     */
76
    public function div($contents = null)
77
    {
78
        return Div::create()->children($contents);
79
    }
80
81
    /**
82
     * @param string $value
83
     *
84
     * @return \Spatie\Html\Elements\Input
85
     */
86
    public function email(string $name = '', ?string $value = '')
87
    {
88
        return $this->input('email', $name, $value);
89
    }
90
91
    /**
92
     * @param string $tag
93
     *
94
     * @return \Spatie\Html\Elements\Element
95
     */
96
    public function element(string $tag)
97
    {
98
        return Element::withTag($tag);
99
    }
100
101
    /**
102
     * @param string $type
103
     * @param string $name
104
     * @param string $value
105
     *
106
     * @return \Spatie\Html\Elements\Input
107
     */
108
    public function input(?string $type = '', string $name = '', ?string $value = '')
109
    {
110
        return Input::create()
111
            ->attributeIf($type, 'type', $type)
0 ignored issues
show
Documentation introduced by
$type is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112
            ->attributeIf($name, 'name', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113
            ->attributeIf($name, 'id', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
            ->attributeIf($name && $this->old($name, $value), 'value', $this->old($name, $value));
0 ignored issues
show
Bug introduced by
It seems like $this->old($name, $value) targeting Spatie\Html\Html::old() can also be of type array or null; however, Spatie\Html\BaseElement::attributeIf() does only seem to accept string, maybe add an additional type check?

This check looks at variables that 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...
115
    }
116
117
    /**
118
     * @param \Spatie\Html\HtmlElement|string $legend
119
     *
120
     * @return \Spatie\Html\Elements\Fieldset
121
     */
122
    public function fieldset($legend = null)
123
    {
124
        return $legend ?
125
            Fieldset::create()->legend($legend) :
126
            Fieldset::create();
127
    }
128
129
    /**
130
     * @param string $method
131
     * @param string $action
132
     * @param array $parameters
0 ignored issues
show
Bug introduced by
There is no parameter named $parameters. 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...
133
     *
134
     * @return \Spatie\Html\Elements\Form
135
     */
136
    public function form(string $method = 'POST', string $action = '')
137
    {
138
        $method = strtoupper($method);
139
        $form = Form::create();
140
141
        // If Laravel needs to spoof the form's method, we'll append a hidden
142
        // field containing the actual method
143
        if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) {
144
            $form = $form->addChild($this->hidden('_method')->value($method));
145
        }
146
147
        // On any other method that get, the form needs a CSRF token
148
        if ($method !== 'GET') {
149
            $form = $form->addChild($this->token());
150
        }
151
152
        return $form
153
            ->method($method === 'GET' ? 'GET' : 'POST')
154
            ->attributeIf($action, 'action', $action);
0 ignored issues
show
Documentation introduced by
$action is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
155
    }
156
157
    /**
158
     * @param string $name
159
     * @param string $value
160
     *
161
     * @return \Spatie\Html\Elements\Input
162
     */
163
    public function hidden(string $name = '', ?string $value = '')
164
    {
165
        return $this->input('hidden', $name, $value);
166
    }
167
168
    /**
169
     * @param \Spatie\Html\HtmlElement|iterable|string|null $contents
170
     * @param string $for
171
     *
172
     * @return \Spatie\Html\Elements\Label
173
     */
174
    public function label($contents = null, string $for = '')
175
    {
176
        return Label::create()
177
            ->attributeIf($for, 'for', $this->fieldName($for))
0 ignored issues
show
Documentation introduced by
$for is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
178
            ->children($contents);
179
    }
180
181
    /**
182
     * @param \Spatie\Html\HtmlElement|string $contents
183
     *
184
     * @return \Spatie\Html\Elements\Legend
185
     */
186
    public function legend($contents = null)
187
    {
188
        return Legend::create()->html($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 186 can also be of type null or object<Spatie\Html\HtmlElement>; however, Spatie\Html\BaseElement::html() does only seem to accept string, 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...
189
    }
190
191
    /**
192
     * @param string $email
193
     * @param string $text
194
     *
195
     * @return \Spatie\Html\Elements\A
196
     */
197
    public function mailto(string $email, string $text = '')
198
    {
199
        return $this->a('mailto:'.$email, $text);
200
    }
201
202
    /**
203
     * @param string $text
204
     * @param string $value
205
     * @param bool $selected
206
     *
207
     * @return \Spatie\Html\Elements\Option
208
     */
209
    public function option(?string $text = '', ?string $value = '', $selected = false)
210
    {
211
        return Option::create()
212
            ->text($text)
213
            ->value($value)
214
            ->selectedIf($selected);
215
    }
216
217
    /**
218
     * @param string $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...
219
     *
220
     * @return \Spatie\Html\Elements\Input
221
     */
222
    public function password(string $name = '')
223
    {
224
        return $this->input('password', $name)->value('');
225
    }
226
227
    /**
228
     * @param string $value
229
     *
230
     * @return \Spatie\Html\Elements\Input
231
     */
232
    public function radio(string $name = '', ?bool $checked = false, ?string $value = '')
233
    {
234
        return $this->input('radio', $name, $value)
235
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
0 ignored issues
show
Documentation introduced by
$checked is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
236
    }
237
238
    /**
239
     * @param string $name
240
     * @param iterable $options
241
     * @param string $value
242
     *
243
     * @return \Spatie\Html\Elements\Select
244
     */
245
    public function select(string $name = '', iterable $options = [], ?string $value = '')
246
    {
247
        return Select::create()
248
            ->attributeIf($name, 'name', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
249
            ->attributeIf($name, 'id', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
250
            ->options($options)
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 245 can also be of type array; however, Spatie\Html\Elements\Select::options() does only seem to accept object<Spatie\Html\Elements\iterable>, 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...
251
            ->value($name ? $this->old($name, $value) : '');
0 ignored issues
show
Bug introduced by
It seems like $name ? $this->old($name, $value) : '' can also be of type array; however, Spatie\Html\Elements\Select::value() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
252
    }
253
254
    /**
255
     * @param \Spatie\Html\HtmlElement|string $contents
256
     *
257
     * @return \Spatie\Html\Elements\Span
258
     */
259
    public function span($contents = null)
260
    {
261
        return Span::create()->children($contents);
262
    }
263
264
    /**
265
     * @param string $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...
266
     *
267
     * @return \Spatie\Html\Elements\Button
268
     */
269
    public function submit(?string $text = '')
270
    {
271
        return $this->button($text, 'submit');
272
    }
273
274
    /**
275
     * @param string $number
276
     * @param string $text
277
     *
278
     * @return \Spatie\Html\Elements\A
279
     */
280
    public function tel(?string $number, ?string $text = '')
281
    {
282
        return $this->a('tel:'.$number, $text);
283
    }
284
285
    /**
286
     * @param string $name
287
     * @param string $value
288
     *
289
     * @return \Spatie\Html\Elements\Input
290
     */
291
    public function text(string $name = '', ?string $value = '')
292
    {
293
        return $this->input('text', $name, $value);
294
    }
295
296
    /**
297
     * @param string $name
298
     * @param string $value
299
     *
300
     * @return \Spatie\Html\Elements\Textarea
301
     */
302
    public function textarea(string $name = '', ?string $value = '')
303
    {
304
        return Textarea::create()
305
            ->attributeIf($name, 'name', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
306
            ->attributeIf($name, 'id', $this->fieldName($name))
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
307
            ->value($this->old($name, $value));
0 ignored issues
show
Bug introduced by
It seems like $this->old($name, $value) targeting Spatie\Html\Html::old() can also be of type array or null; however, Spatie\Html\Elements\Textarea::value() does only seem to accept string, maybe add an additional type check?

This check looks at variables that 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...
308
    }
309
310
    /**
311
     * @return \Spatie\Html\Elements\Input
312
     */
313
    public function token()
314
    {
315
        return $this->hidden('_token')->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...
316
    }
317
318
    /**
319
     * @param \ArrayAccess|array $model
320
     *
321
     * @return $this
322
     */
323
    public function model($model)
324
    {
325
        $this->model = $model;
326
327
        return $this;
328
    }
329
330
    /**
331
     * @param \ArrayAccess|array $model
332
     * @param string $method
333
     * @param string $action
334
     *
335
     * @return \Spatie\Html\Elements\Form
336
     */
337
    public function modelForm($model, string $method = 'POST', string $action = ''): Form
338
    {
339
        $this->model($model);
340
341
        return $this->form($method, $action);
342
    }
343
344
    /**
345
     * @return $this
346
     */
347
    public function endModel()
348
    {
349
        $this->model = null;
350
351
        return $this;
352
    }
353
354
    /**
355
     * @return \Illuminate\Contracts\Support\Htmlable
356
     */
357
    public function closeModelForm(): Htmlable
358
    {
359
        $this->endModel();
360
361
        return $this->form()->close();
362
    }
363
364
    /**
365
     * @param string $name
366
     *
367
     * @return mixed
368
     */
369
    protected function old(string $name, ?string $value = '')
370
    {
371
        if (empty($name)) {
372
            return;
373
        }
374
375
        // If there's no default value provided, and the html builder currently
376
        // has a model assigned, try to retrieve a value from the model.
377
        if (empty($value) && $this->model) {
378
            $value = $this->model[$name] ?? '';
379
        }
380
381
        return $this->request->old($name, $value);
382
    }
383
384
    protected function fieldName(string $name): string
385
    {
386
        return $name;
387
    }
388
389
    protected function ensureModelIsAvailable()
390
    {
391
        if (empty($this->model)) {
392
            throw new Exception('Method requires a model to be set on the html builder');
393
        }
394
    }
395
}
396