Completed
Push — master ( 3c0a55...3afa22 )
by Maxime
02:42 queued 01:12
created

Configuration   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 61
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getConfigTreeBuilder() 0 58 7
1
<?php
2
3
/*
4
 * This file is part of the "elao/enum" package.
5
 *
6
 * Copyright (C) Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\Enum\Bridge\Symfony\Bundle\DependencyInjection;
12
13
use Elao\Enum\EnumInterface;
14
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
15
use Symfony\Component\Config\Definition\ConfigurationInterface;
16
use Symfony\Component\Serializer\SerializerInterface;
17
18
class Configuration implements ConfigurationInterface
19
{
20
    public function getConfigTreeBuilder()
21
    {
22
        $treeBuilder = new TreeBuilder('elao_enum');
23
        $rootNode = method_exists(TreeBuilder::class, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('elao_enum');
0 ignored issues
show
Bug introduced by
The method root() does not seem to exist on object<Symfony\Component...on\Builder\TreeBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
25
        $rootNode->children()
26
            ->arrayNode('argument_value_resolver')->canBeDisabled()->end()
27
            ->arrayNode('doctrine')
28
                ->fixXmlConfig('type')
29
                ->children()
30
                    ->arrayNode('types')
31
                        ->validate()
32
                            ->ifTrue(static function (array $v): bool {
33
                                $classes = array_keys($v);
34
                                foreach ($classes as $class) {
35
                                    if (!is_a($class, EnumInterface::class, true)) {
36
                                        return true;
37
                                    }
38
                                }
39
40
                                return false;
41
                            })
42
                            ->then(static function (array $v) {
43
                                $classes = array_keys($v);
44
                                $invalids = [];
45
                                foreach ($classes as $class) {
46
                                    if (!is_a($class, EnumInterface::class, true)) {
47
                                        $invalids[] = $class;
48
                                    }
49
                                }
50
51
                                throw new \InvalidArgumentException(sprintf(
52
                                    'Invalid classes %s. Expected instances of "%s"',
53
                                    json_encode($invalids),
54
                                    EnumInterface::class)
55
                                );
56
                            })
57
                        ->end()
58
                            ->useAttributeAsKey('class')
59
                            ->arrayPrototype()
60
                            ->beforeNormalization()
61
                                ->ifString()->then(static function (string $v): array { return ['name' => $v]; })
62
                            ->end()
63
                            ->children()
64
                                ->scalarNode('name')->cannotBeEmpty()->end()
65
                                ->enumNode('type')->values(['string', 'int'])->cannotBeEmpty()->defaultValue('string')->end()
66
                            ->end()
67
                        ->end()
68
                    ->end()
69
                ->end()
70
            ->end()
71
            ->arrayNode('serializer')
72
                ->{interface_exists(SerializerInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
73
            ->end()
74
        ->end();
75
76
        return $treeBuilder;
77
    }
78
}
79