Passed
Pull Request — master (#192)
by Sergei
02:16
created

Field::getFactory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 16
ccs 6
cts 8
cp 0.75
crap 3.1406
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use RuntimeException;
8
use Yiisoft\Form\Field\Hidden;
9
use Yiisoft\Form\Field\Part\Error;
10
use Yiisoft\Form\Field\Part\Hint;
11
use Yiisoft\Form\Field\Part\Label;
12
use Yiisoft\Form\Field\Text;
13
14
use function array_key_exists;
15
16
final class Field
17
{
18
    /**
19
     * @psalm-var array<string,array>
20
     */
21
    private static array $configs = [
22
        'default' => [],
23
    ];
24
25
    private static string $defaultConfigName = 'default';
26
27
    /**
28
     * @psalm-var array<string,FieldFactory>
29
     */
30
    private static array $factories = [];
31
32
    /**
33
     * @psalm-param array<string,array> $configs
34
     */
35
    public static function initialize(array $configs = [], string $defaultConfigName = 'default'): void
36
    {
37
        self::$configs = array_merge(self::$configs, $configs);
38
        self::$defaultConfigName = $defaultConfigName;
39
    }
40
41 1
    public static function hidden(FormModelInterface $formModel, string $attribute, array $config = []): Hidden
42
    {
43 1
        return self::getFactory()->hidden($formModel, $attribute, $config);
44
    }
45
46 1
    public static function text(FormModelInterface $formModel, string $attribute, array $config = []): Text
47
    {
48 1
        return self::getFactory()->text($formModel, $attribute, $config);
49
    }
50
51 1
    public static function label(FormModelInterface $formModel, string $attribute, array $config = []): Label
52
    {
53 1
        return self::getFactory()->label($formModel, $attribute, $config);
54
    }
55
56 1
    public static function hint(FormModelInterface $formModel, string $attribute, array $config = []): Hint
57
    {
58 1
        return self::getFactory()->hint($formModel, $attribute, $config);
59
    }
60
61 1
    public static function error(FormModelInterface $formModel, string $attribute, array $config = []): Error
62
    {
63 1
        return self::getFactory()->error($formModel, $attribute, $config);
64
    }
65
66
    /**
67
     * @psalm-template T
68
     * @psalm-param class-string<T> $class
69
     * @psalm-return T
70
     */
71
    public function field(string $class, FormModelInterface $formModel, string $attribute, array $config = []): object
72
    {
73
        return self::getFactory()->field($class, $formModel, $attribute, $config);
74
    }
75
76 5
    public static function getFactory(?string $name = null): FieldFactory
77
    {
78 5
        $name = $name ?? self::$defaultConfigName;
79
80 5
        if (!array_key_exists($name, self::$factories)) {
81 1
            if (!array_key_exists($name, self::$configs)) {
82
                throw new RuntimeException(
83
                    sprintf('Configuration with name "%s" not found.', $name)
84
                );
85
            }
86
87
            /** @psalm-suppress MixedArgument */
88 1
            self::$factories[$name] = new FieldFactory(...self::$configs[$name]);
0 ignored issues
show
Bug introduced by
self::configs[$name] is expanded, but the parameter $containerTag of Yiisoft\Form\FieldFactory::__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

88
            self::$factories[$name] = new FieldFactory(/** @scrutinizer ignore-type */ ...self::$configs[$name]);
Loading history...
89
        }
90
91 5
        return self::$factories[$name];
92
    }
93
}
94