Passed
Push — master ( cef086...22be2e )
by Emmanuel
01:44
created

ConfigDefinition::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 67
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 0
cts 66
cp 0
rs 9.2815
c 0
b 0
f 0
cc 1
eloc 62
nc 1
nop 0
crap 2

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
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.1
6
 *
7
 * @author Emmanuel Dyan
8
 * @author Rémi Sauvat
9
 * @copyright 2018 Emmanuel Dyan
10
 *
11
 * @package edyan/neuralyzer
12
 *
13
 * @license GNU General Public License v2.0
14
 *
15
 * @link https://github.com/edyan/neuralyzer
16
 */
17
18
namespace Edyan\Neuralyzer\Configuration;
19
20
use Edyan\Neuralyzer\Guesser;
21
use Symfony\Component\Config\Definition\ConfigurationInterface;
22
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
23
24
/**
25
 * Configuration Validation
26
 */
27
class ConfigDefinition implements ConfigurationInterface
28
{
29
    /**
30
     * Validate the configuration
31
     *
32
     * The config structure is something like :
33
     * ## Root
34
     * entities:
35
     *    ## Can be repeated : the name of the table, is an array
36
     *    accounts:
37
     *        cols:
38
     *            ## Can be repeated : the name of the field, is an array
39
     *            name:
40
     *                method: words # Required: name of the method
41
     *                params: [8] # Optional: parameters (an array)
42
     *
43
     * @return TreeBuilder
44
     */
45
    public function getConfigTreeBuilder()
46
    {
47
        $treeBuilder = new TreeBuilder();
48
        $rootNode = $treeBuilder->root('config');
49
        $rootNode
50
            ->children()
51
                ->scalarNode('guesser')
52
                    ->info('Set the guesser class')
53
                    ->defaultValue(Guesser::class)
54
                    ->cannotBeEmpty()
55
                ->end()
56
                ->scalarNode('guesser_version')
57
                    ->info('Set the version of the guesser the conf has been written with')
58
                    ->defaultValue((new Guesser)->getVersion())
59
                    ->cannotBeEmpty()
60
                ->end()
61
                ->scalarNode('language')
62
                    ->info("Faker's language, make sure all your methods have a translation")
63
                    ->defaultValue('en_US')
64
                ->end()
65
                ->arrayNode('entities')
66
                    ->info("List all entities, theirs cols and actions")
67
                    ->example('people')
68
                    ->isRequired()
69
                    ->requiresAtLeastOneElement()
70
                    ->prototype('array')
71
                        ->children()
72
                            ->scalarNode('action')
73
                                ->info('Either "update" or "insert" data')
74
                                ->defaultValue('update')
75
                                ->validate()
76
                                    ->ifNotInArray(['update', 'insert'])
77
                                    ->thenInvalid('Action is either "update" or "insert"')
78
                                ->end()
79
                            ->end()
80
                            ->scalarNode('delete')
81
                                ->info('Should we delete data with what is defined in "delete_where" ?')
82
                                ->defaultValue(false)
83
                            ->end()
84
                            ->scalarNode('delete_where')
85
                                ->cannotBeEmpty()
86
                                ->info('Condition applied in a WHERE if delete is set to "true"')
87
                                ->example("'1 = 1'")
88
                            ->end()
89
                            ->arrayNode('cols')
90
                                ->example([
91
                                    'first_name' => ['method' => 'firstName'],
92
                                    'last_name' => ['method' => 'lastName']
93
                                ])
94
                                ->requiresAtLeastOneElement()
95
                                ->prototype('array')
96
                                    ->children()
97
                                        ->scalarNode('method')->isRequired()->end()
98
                                        ->arrayNode('params')
99
                                            ->defaultValue([])
100
                                            ->prototype('variable')->end()
101
                                        ->end()
102
                                    ->end()
103
                                ->end()
104
                            ->end()
105
                        ->end()
106
                    ->end()
107
                ->end()
108
            ->end()
109
        ;
110
111
        return $treeBuilder;
112
    }
113
}
114