Passed
Pull Request — master (#123)
by
unknown
02:39
created

Widget   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 66
ccs 18
cts 18
cp 1
rs 10
c 2
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A counter() 0 3 1
A id() 0 6 1
A getId() 0 7 3
A autoIdPrefix() 0 6 1
A withDarkTheme() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap5;
6
7
use Yiisoft\Widget\Widget as YiiWidget;
8
9
abstract class Widget extends YiiWidget
10
{
11
    private ?string $id = null;
12
    private bool $autoGenerate = true;
13
    private string $autoIdPrefix = 'w';
14
    private static int $counter = 0;
15
    protected bool $darkTheme = false;
16
17
18
    /**
19
     * Returns the ID of the widget.
20
     *
21
     * $param string|null $suffix
22
     *
23
     * @return string|null ID of the widget.
24
     */
25 173
    public function getId(?string $suffix = null): ?string
26
    {
27 173
        if ($this->autoGenerate && $this->id === null) {
28 172
            $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...
29
        }
30
31 173
        return $this->id;
32
    }
33
34
    /**
35
     * Set the ID of the widget.
36
     *
37
     * @return static
38
     */
39 1
    public function id(string $value): static
40
    {
41 1
        $new = clone $this;
42 1
        $new->id = $value;
43
44 1
        return $new;
45
    }
46
47
    /**
48
     * Counter used to generate {@see id} for widgets.
49
     */
50 162
    public static function counter(int $value): void
51
    {
52 162
        self::$counter = $value;
53
    }
54
55
    /**
56
     * The prefix to the automatically generated widget IDs.
57
     *
58
     * @return static
59
     * {@see getId()}
60
     */
61 1
    public function autoIdPrefix(string $value): static
62
    {
63 1
        $new = clone $this;
64 1
        $new->autoIdPrefix = $value;
65
66 1
        return $new;
67
    }
68
69 1
    public function withDarkTheme(bool $darkTheme = true): static
70
    {
71 1
        $new = clone $this;
72 1
        $new->darkTheme = $darkTheme;
73
74 1
        return $new;
75
    }
76
}
77