Passed
Pull Request — master (#90)
by
unknown
13:09
created

Widget::counter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RedCatGirl\YiiBootstrap386;
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
     * $param string|null $suffix
18
     *
19
     * @return string|null Id of the widget.
20
     */
21 167
    public function getId(?string $suffix = null): ?string
22
    {
23 167
        if ($this->autoGenerate && $this->id === null) {
24 166
            $this->id = $this->autoIdPrefix . static::$counter++ . $suffix;
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...
25
        }
26
27 167
        return $this->id;
28
    }
29
30
    /**
31
     * Set the Id of the widget.
32
     *
33
     * @param string $value
34
     *
35
     * @return self
36
     */
37 1
    public function id(string $value): self
38
    {
39 1
        $new = clone $this;
40 1
        $new->id = $value;
41
42 1
        return $new;
43
    }
44
45
    /**
46
     * Counter used to generate {@see id} for widgets.
47
     *
48
     * @param int $value
49
     */
50 156
    public static function counter(int $value): void
51
    {
52 156
        self::$counter = $value;
53
    }
54
55
    /**
56
     * The prefix to the automatically generated widget IDs.
57
     *
58
     * @param string $value
59
     *
60
     * @return self
61
     *
62
     * {@see getId()}
63
     */
64 1
    public function autoIdPrefix(string $value): self
65
    {
66 1
        $new = clone $this;
67 1
        $new->autoIdPrefix = $value;
68
69 1
        return $new;
70
    }
71
}
72