Passed
Pull Request — master (#244)
by Sergei
02:56
created

ThemeContainer::getTheme()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 15
ccs 8
cts 9
cp 0.8889
crap 4.0218
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use function array_key_exists;
8
9
final class ThemeContainer
10
{
11
    /**
12
     * @psalm-var array<string,array>
13
     */
14
    private static array $configs = [];
15
16
    private static ?string $defaultConfig = null;
17
18
    /**
19
     * @psalm-var array<string,Theme|null>
20
     */
21
    private static array $themes = [];
22
23
    /**
24
     * @param array<string,array> $configs Array of configurations with {@see Theme::__construct()}
25
     * arguments indexed by name. For example:
26
     * ```php
27
     * [
28
     *     'default' => [
29
     *         'containerClass' => 'formField',
30
     *     ],
31
     *     'bulma' => [
32
     *         'containerClass' => 'field',
33
     *         'inputClass' => 'input',
34
     *         'invalidClass' => 'has-background-danger',
35
     *         'validClass' => 'has-background-success',
36
     *         'template' => "{label}<div class=\"control\">\n{input}</div>\n{hint}\n{error}",
37
     *         'labelClass' => 'label',
38
     *         'errorClass' => 'has-text-danger is-italic',
39
     *         'hintClass' => 'help',
40
     *     ],
41
     *     'bootstrap5' => [
42
     *         'containerClass' => 'mb-3',
43
     *         'invalidClass' => 'is-invalid',
44
     *         'errorClass' => 'text-danger fst-italic',
45
     *         'hintClass' => 'form-text',
46
     *         'inputClass' => 'form-control',
47
     *         'labelClass' => 'form-label',
48
     *         'validClass' => 'is-valid',
49
     *     ],
50
     * ]
51
     * ```
52
     * @param string|null $defaultConfig Configuration name that will be used for create fields by default.
53
     */
54 678
    public static function initialize(array $configs = [], ?string $defaultConfig = null): void
55
    {
56 678
        self::$configs = $configs;
57 678
        self::$defaultConfig = $defaultConfig;
58 678
        self::$themes = [];
59
    }
60
61 668
    public static function getTheme(?string $name = null): ?Theme
62
    {
63 668
        $name ??= self::$defaultConfig;
64 668
        if ($name === null) {
65 625
            return null;
66
        }
67
68 44
        if (!array_key_exists($name, self::$themes)) {
69
            /** @psalm-suppress MixedArgument */
70 44
            self::$themes[$name] = array_key_exists($name, self::$configs)
71 44
                ? new Theme(...self::$configs[$name])
0 ignored issues
show
Bug introduced by
self::configs[$name] is expanded, but the parameter $containerTag of Yiisoft\Form\Theme::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
                ? new Theme(/** @scrutinizer ignore-type */ ...self::$configs[$name])
Loading history...
72
                : null;
73
        }
74
75 44
        return self::$themes[$name];
76
    }
77
}
78