Test Failed
Pull Request — master (#159)
by Sergei
03:08
created

Label::content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
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\Tag\Label as LabelTag;
10
use Yiisoft\Widget\Widget;
11
12
final class Label extends Widget
13
{
14
    use FormAttributeTrait;
15
16
    private bool $setForAttribute = true;
17
18
    private ?string $forId = null;
19
    private bool $useInputIdAttribute = true;
20
21
    /**
22
     * @var string|Stringable|null
23
     */
24
    private $content = null;
25
26
    protected function run(): string
27
    {
28
        $tag = LabelTag::tag()->content(
29
            $this->content ?? $this->getAttributeLabel()
30
        );
31
32
        if (
33
            $this->setForAttribute
34
            && $this->useInputIdAttribute
35
            && $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

35
            && $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...
36
        ) {
37
            $tag = $tag->forId($this->forId ?? $this->getInputId());
38
        }
39
40
        return $tag->render();
41
    }
42
43
    public function setForAttribute(bool $value): self
44
    {
45
        $new = clone $this;
46
        $new->setForAttribute = $value;
47
        return $new;
48
    }
49
50
    /**
51
     * @return static
52
     */
53
    public function forId(?string $id): self
54
    {
55
        $new = clone $this;
56
        $new->forId = $id;
57
        return $new;
58
    }
59
60
    /**
61
     * @return static
62
     */
63
    public function useInputIdAttribute(bool $value): self
64
    {
65
        $new = clone $this;
66
        $new->useInputIdAttribute = $value;
67
        return $new;
68
    }
69
70
    /**
71
     * @param string|Stringable|null $content
72
     *
73
     * @return static
74
     */
75
    public function content($content): self
76
    {
77
        $new = clone $this;
78
        $new->content = $content;
79
        return $new;
80
    }
81
}
82