Passed
Pull Request — master (#35)
by Wilmer
02:20
created

Widget::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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;
10
    private bool $autoGenerate = true;
11
    private static int $counter = 0;
12
    private static string $autoIdPrefix = 'w';
13
14
    /**
15
     * Returns the Id of the widget.
16
     *
17
     * @return string Id of the widget.
18
     */
19 132
    public function getId(): string
20
    {
21 132
        if ($this->autoGenerate) {
22 132
            $this->id = self::$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 132
        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 setId(string $value): self
36
    {
37 1
        $this->id = $value;
38
39 1
        return $this;
40
    }
41
42
    /**
43
     * Counter used to generate {@see id} for widgets.
44
     *
45
     * @param int $value
46
     */
47 132
    public static function counter(int $value): void
48
    {
49 132
        self::$counter = $value;
50 132
    }
51
52
    /**
53
     * The prefix to the automatically generated widget IDs.
54
     *
55
     * @param string $value
56
     *
57
     * {@see getId()}
58
     */
59 1
    public static function autoIdPrefix(string $value): void
60
    {
61 1
        self::$autoIdPrefix = $value;
62 1
    }
63
}
64