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

FieldFactory::inputText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use Yiisoft\Form\Field\InputText;
8
use Yiisoft\Form\Helper\HtmlForm;
9
use Yiisoft\Html\Tag\Label;
10
11
final class FieldFactory
12
{
13
    private ?string $template;
14
15
    private ?bool $setInputIdAttribute;
16
17
    private ?Label $labelTag;
18
    private ?bool $setLabelForAttribute;
19
20
    private array $inputTextConfig;
21
22
    public function __construct(FieldFactoryConfig $config)
23
    {
24
        $this->template = $config->getTemplate();
25
26
        $this->setInputIdAttribute = $config->getSetInputIdAttribute();
27
28
        $this->labelTag = $config->getLabelTag();
29
        $this->setLabelForAttribute = $config->getSetLabelForAttribute();
30
31
        $this->inputTextConfig = $config->getInputTextConfig();
32
    }
33
34
    public function label(FormModelInterface $formModel, string $attribute): Label
35
    {
36
        $tag = $this->labelTag ?? Label::tag();
37
        $tag = $tag->content(HtmlForm::getAttributeLabel($formModel, $attribute));
38
39
        if (
40
            ($this->setLabelForAttribute ?? true)
41
            && $tag->getAttribute('for') === null
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on Yiisoft\Html\Tag\Label. ( Ignorable by Annotation )

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

41
            && $tag->/** @scrutinizer ignore-call */ getAttribute('for') === null

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...
42
        ) {
43
            $id = $this->getInputId($formModel, $attribute);
44
            if ($id !== null) {
45
                $tag = $tag->forId($id);
46
            }
47
        }
48
49
        return $tag;
50
    }
51
52
    public function inputText(FormModelInterface $formModel, string $attribute): InputText
53
    {
54
        $config = array_merge(
55
            $this->makeWidgetConfig([
56
                'template()',
57
                'setInputIdAttribute()',
58
                'labelTag()',
59
                'setLabelForAttribute()',
60
            ]),
61
            $this->inputTextConfig
62
        );
63
64
        return InputText::widget($config)->attribute($formModel, $attribute);
0 ignored issues
show
Bug introduced by
The method attribute() does not exist on Yiisoft\Widget\Widget. It seems like you code against a sub-type of Yiisoft\Widget\Widget such as Yiisoft\Form\Field\Base\AbstractField. ( Ignorable by Annotation )

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

64
        return InputText::widget($config)->/** @scrutinizer ignore-call */ attribute($formModel, $attribute);
Loading history...
65
    }
66
67
    private function makeWidgetConfig(array $keys): array
68
    {
69
        $config = [];
70
        foreach ($keys as $key) {
71
            switch ($key) {
72
                case 'template()':
73
                    if ($this->template !== null) {
74
                        $config[$key] = [$this->template];
75
                    }
76
                    break;
77
78
                case 'setInputIdAttribute()':
79
                    if ($this->setInputIdAttribute !== null) {
80
                        $config[$key] = [$this->setInputIdAttribute];
81
                    }
82
                    break;
83
84
                case 'labelTag()':
85
                    if ($this->labelTag !== null) {
86
                        $config[$key] = [$this->labelTag];
87
                    }
88
                    break;
89
90
                case 'setLabelForAttribute()':
91
                    if ($this->setLabelForAttribute !== null) {
92
                        $config[$key] = [$this->setLabelForAttribute];
93
                    }
94
                    break;
95
            }
96
        }
97
        return $config;
98
    }
99
100
    private function getInputId(FormModelInterface $formModel, string $attribute): ?string
101
    {
102
        if (!($this->setInputIdAttribute ?? true)) {
103
            return null;
104
        }
105
106
        return $this->inputId ?? HtmlForm::getInputId($formModel, $attribute);
0 ignored issues
show
Bug Best Practice introduced by
The property inputId does not exist on Yiisoft\Form\FieldFactory. Did you maybe forget to declare it?
Loading history...
107
    }
108
}
109