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

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 7.983
c 0
b 0
f 0
cc 7
nc 4
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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