DictionaryType   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 11
c 5
b 0
f 0
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A configureOptions() 0 15 1
A getParent() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Knp\DictionaryBundle\Form\Type;
6
7
use Knp\DictionaryBundle\Dictionary\Collection;
8
use Symfony\Component\Form\AbstractType;
9
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
10
use Symfony\Component\OptionsResolver\Options;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
13
/**
14
 * @extends AbstractType<mixed>
15
 */
16
final class DictionaryType extends AbstractType
17
{
18
    public function __construct(private readonly Collection $collection) {}
19
20 3
    public function configureOptions(OptionsResolver $optionsResolver): void
21
    {
22 3
        $dictionaries = $this->collection;
23 3
24
        $choices = static function (Options $options) use ($dictionaries): array {
25 1
            $name    = $options['name'];
26
            $choices = $dictionaries[$name]->getValues();
27 1
28
            return array_flip($choices);
29 1
        };
30 1
31 1
        $optionsResolver
32
            ->setDefault('choices', $choices)
33 1
            ->setRequired(['name'])
34 1
            ->setAllowedValues('name', array_keys(iterator_to_array($this->collection)))
35
        ;
36
    }
37 1
38 1
    public function getParent(): string
39 1
    {
40
        return ChoiceType::class;
41 1
    }
42
}
43