Passed
Push — master ( 566566...d0f53d )
by Alexander
02:18
created

Widget   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 61
ccs 15
cts 15
cp 1
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 7 3
A counter() 0 3 1
A id() 0 6 1
A autoIdPrefix() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap5;
6
7
abstract class Widget extends \Yiisoft\Widget\Widget
8
{
9
    private ?string $id = null;
10
    private bool $autoGenerate = true;
11
    private string $autoIdPrefix = 'w';
12
    private static int $counter = 0;
13
14
    /**
15
     * Returns the Id of the widget.
16
     *
17
     * @return string|null Id of the widget.
18
     */
19 134
    protected function getId(): ?string
20
    {
21 134
        if ($this->autoGenerate && $this->id === null) {
22 133
            $this->id = $this->autoIdPrefix . static::$counter++;
0 ignored issues
show
Bug introduced by
Since $counter is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $counter to at least protected.
Loading history...
23
        }
24
25 134
        return $this->id;
26
    }
27
28
    /**
29
     * Set the Id of the widget.
30
     *
31
     * @param string $value
32
     *
33
     * @return $this
34
     */
35 1
    public function id(string $value): self
36
    {
37 1
        $new = clone $this;
38 1
        $new->id = $value;
39
40 1
        return $new;
41
    }
42
43
    /**
44
     * Counter used to generate {@see id} for widgets.
45
     *
46
     * @param int $value
47
     */
48 124
    public static function counter(int $value): void
49
    {
50 124
        self::$counter = $value;
51 124
    }
52
53
    /**
54
     * The prefix to the automatically generated widget IDs.
55
     *
56
     * @param string $value
57
     *
58
     * @return $this
59
     *
60
     * {@see getId()}
61
     */
62 1
    public function autoIdPrefix(string $value): self
63
    {
64 1
        $new = clone $this;
65 1
        $new->autoIdPrefix = $value;
66
67 1
        return $new;
68
    }
69
}
70