Passed
Push — master ( 46e4e2...118217 )
by Rustam
02:47
created

Label::render()   B

Complexity

Conditions 10
Paths 22

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 22
nop 0
dl 0
loc 31
ccs 18
cts 18
cp 1
crap 10
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Part;
6
7
use Stringable;
8
use Yiisoft\Form\Field\Base\FormAttributeTrait;
9
use Yiisoft\Html\Html;
10
use Yiisoft\Widget\Widget;
11
12
/**
13
 * Represents label for a form field.
14
 */
15
final class Label extends Widget
16
{
17
    use FormAttributeTrait;
18
19
    private array $attributes = [];
20
21
    private bool $setFor = true;
22
23
    private ?string $forId = null;
24
    private bool $useInputId = true;
25
26
    private string|Stringable|null $content = null;
27
28
    private bool $encode = true;
29
30 3
    public function attributes(array $attributes): self
31
    {
32 3
        $new = clone $this;
33 3
        $new->attributes = $attributes;
34 3
        return $new;
35
    }
36
37 6
    public function addAttributes(array $attributes): self
38
    {
39 6
        $new = clone $this;
40 6
        $new->attributes = array_merge($this->attributes, $attributes);
41 6
        return $new;
42
    }
43
44
    /**
45
     * Set tag ID.
46
     *
47
     * @param string|null $id Tag ID.
48
     */
49 2
    public function id(?string $id): self
50
    {
51 2
        $new = clone $this;
52 2
        $new->attributes['id'] = $id;
53 2
        return $new;
54
    }
55
56
    /**
57
     * Replace tag CSS classes with a new set of classes.
58
     *
59
     * @param string|null ...$class One or many CSS classes.
60
     */
61 9
    public function class(?string ...$class): self
62
    {
63 9
        $new = clone $this;
64 9
        $new->attributes['class'] = array_filter($class, static fn ($c) => $c !== null);
65 9
        return $new;
66
    }
67
68
    /**
69
     * Add one or more CSS classes to the tag.
70
     *
71
     * @param string|null ...$class One or many CSS classes.
72
     */
73 10
    public function addClass(?string ...$class): self
74
    {
75 10
        $new = clone $this;
76 10
        Html::addCssClass(
77 10
            $new->attributes,
78 10
            array_filter($class, static fn ($c) => $c !== null),
79 10
        );
80 10
        return $new;
81
    }
82
83 5
    public function setFor(bool $value): self
84
    {
85 5
        $new = clone $this;
86 5
        $new->setFor = $value;
87 5
        return $new;
88
    }
89
90 6
    public function forId(?string $id): self
91
    {
92 6
        $new = clone $this;
93 6
        $new->forId = $id;
94 6
        return $new;
95
    }
96
97 21
    public function useInputId(bool $value): self
98
    {
99 21
        $new = clone $this;
100 21
        $new->useInputId = $value;
101 21
        return $new;
102
    }
103
104 14
    public function content(string|Stringable|null $content): self
105
    {
106 14
        $new = clone $this;
107 14
        $new->content = $content;
108 14
        return $new;
109
    }
110
111
    /**
112
     * Whether content should be HTML-encoded.
113
     */
114 2
    public function encode(bool $value): self
115
    {
116 2
        $new = clone $this;
117 2
        $new->encode = $value;
118 2
        return $new;
119
    }
120
121 283
    public function render(): string
122
    {
123 283
        $useModel = $this->hasFormModelAndAttribute();
124
125 283
        $content = $useModel
126 172
            ? $this->content ?? $this->getFormAttributeLabel()
127 113
            : (string) $this->content;
128
129 283
        if ($content === '') {
130 108
            return '';
131
        }
132
133 177
        $labelAttributes = $this->attributes;
134
135 177
        if ($this->setFor && !isset($labelAttributes['for'])) {
136 173
            $id = $this->forId;
137 173
            if ($useModel && $id === null && $this->useInputId) {
138 143
                $id = $this->getInputId();
139
            }
140 173
            if ($id !== null) {
141 148
                $labelAttributes['for'] = $id;
142
            }
143
        }
144
145 177
        $tag = Html::label($content)->addAttributes($labelAttributes);
146
147 177
        if (!$this->encode) {
148 1
            $tag = $tag->encode(false);
149
        }
150
151 177
        return $tag->render();
152
    }
153
}
154