Test Failed
Pull Request — master (#159)
by Sergei
02:19
created

InputText::generateInput()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 16
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Field\Base\AbstractField;
9
use Yiisoft\Form\Field\Base\PlaceholderTrait;
10
use Yiisoft\Html\Html;
11
use Yiisoft\Html\Tag\Input;
12
13
use function is_string;
14
15
final class InputText extends AbstractField
16
{
17
    use PlaceholderTrait;
18
19
    private ?Input $tag = null;
20
21
    public function inputTag(?Input $tag): self
22
    {
23
        if ($tag !== null && $tag->getAttribute('type') !== 'text') {
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on Yiisoft\Html\Tag\Input. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        if ($tag !== null && $tag->/** @scrutinizer ignore-call */ getAttribute('type') !== 'text') {

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...
24
            throw new InvalidArgumentException('Input tag should be with type "text".');
25
        }
26
27
        $new = clone $this;
28
        $new->tag = $tag;
29
        return $new;
30
    }
31
32
    protected function generateInput(): string
33
    {
34
        $value = $this->getAttributeValue();
35
36
        if (!is_string($value) && $value !== null) {
37
            throw new InvalidArgumentException('Text widget must be a string or null value.');
38
        }
39
40
        $tag = $this->tag === null
41
            ? Html::textInput($this->getInputName(), $value)
42
            : $this->tag->name($this->getInputName())->value($value);
43
44
        $tag = $this->prepareIdInInputTag($tag);
45
        $tag = $this->preparePlaceholderInInputTag($tag);
46
47
        return $tag->render();
48
    }
49
}
50