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

Widget   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 63
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 6 1
A counter() 0 3 1
A getId() 0 7 3
A autoIdPrefix() 0 6 1
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