Completed
Pull Request — master (#278)
by Asmir
22:58 queued 15:13
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 66
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 51
c 2
b 0
f 0
dl 0
loc 66
ccs 0
cts 50
cp 0
rs 8.7579
cc 5
nc 2
nop 0
crap 30

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
declare(strict_types=1);
4
5
namespace Doctrine\Bundle\MigrationsBundle\DependencyInjection;
6
7
use ReflectionClass;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
use function constant;
11
use function in_array;
12
use function is_string;
13
use function method_exists;
14
use function strlen;
15
use function strpos;
16
use function strtoupper;
17
use function substr;
18
19
/**
20
 * DoctrineMigrationsExtension configuration structure.
21
 */
22
class Configuration implements ConfigurationInterface
23
{
24
    /**
25
     * Generates the configuration tree.
26
     *
27
     * @return TreeBuilder The config tree builder
28
     */
29
    public function getConfigTreeBuilder() : TreeBuilder
30
    {
31
        $treeBuilder = new TreeBuilder('doctrine_migrations');
32
33
        if (method_exists($treeBuilder, 'getRootNode')) {
34
            $rootNode = $treeBuilder->getRootNode();
35
        } else {
36
            // BC layer for symfony/config 4.1 and older
37
            $rootNode = $treeBuilder->root('doctrine_migrations', 'array');
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

37
            $rootNode = /** @scrutinizer ignore-deprecated */ $treeBuilder->root('doctrine_migrations', 'array');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
38
        }
39
40
        $organizeMigrationModes = $this->getOrganizeMigrationsModes();
41
42
        $rootNode
43
            ->children()
44
                ->scalarNode('name')->defaultValue('Application Migrations')->end()
45
                ->arrayNode('migrations_paths')
46
                    ->requiresAtLeastOneElement()
47
                    ->defaultValue(['%kernel.root_dir%/DoctrineMigrations' => 'Application\Migrations'])
48
                    ->prototype('scalar')->end()
49
                 ->end()
50
51
                ->arrayNode('storage')
52
                    ->children()
53
                        ->arrayNode('table_storage')
54
                            ->children()
55
                                ->scalarNode('table_name')->defaultValue('migration_versions')->cannotBeEmpty()->end()
56
                                ->scalarNode('version_column_name')->defaultValue('version')->end()
57
                                ->scalarNode('version_column_length')->defaultValue(2048)->end()
58
                                ->scalarNode('executed_at_column_name')->defaultValue('executed_at')->end()
59
                                ->scalarNode('execution_time_column_name')->defaultValue('execution_time')->end()
60
                            ->end()
61
                        ->end()
62
                    ->end()
63
                ->end()
64
                ->scalarNode('sorter')->defaultValue(null)->end()
65
                ->scalarNode('connection')->defaultValue('default')->end()
66
                ->scalarNode('em')->defaultValue(null)->end()
67
                ->scalarNode('all_or_nothing')->defaultValue(false)->end()
68
                ->scalarNode('custom_template')->defaultValue(null)->end()
69
                ->scalarNode('organize_migrations')->defaultValue(false)
70
                    ->info('Organize migrations mode. Possible values are: "BY_YEAR", "BY_YEAR_AND_MONTH", false')
71
                    ->validate()
72
                        ->ifTrue(static function ($v) use ($organizeMigrationModes) {
73
                            if ($v === false) {
74
                                return false;
75
                            }
76
77
                            if (is_string($v) && in_array(strtoupper($v), $organizeMigrationModes)) {
78
                                return false;
79
                            }
80
81
                            return true;
82
                        })
83
                        ->thenInvalid('Invalid organize migrations mode value %s')
84
                    ->end()
85
                    ->validate()
86
                        ->ifString()
87
                            ->then(static function ($v) {
88
                                return constant('Doctrine\Migrations\Configuration\Configuration::VERSIONS_ORGANIZATION_' . strtoupper($v));
89
                            })
90
                    ->end()
91
                ->end()
92
            ->end();
93
94
        return $treeBuilder;
95
    }
96
97
98
    /**
99
     * Find organize migrations modes for their names
100
     *
101
     * @return string[]
102
     */
103
    private function getOrganizeMigrationsModes() : array
104
    {
105
        $constPrefix = 'VERSIONS_ORGANIZATION_';
106
        $prefixLen   = strlen($constPrefix);
107
        $refClass    = new ReflectionClass('Doctrine\Migrations\Configuration\Configuration');
108
        $constsArray = $refClass->getConstants();
109
        $namesArray  = [];
110
111
        foreach ($constsArray as $key => $value) {
112
            if (strpos($key, $constPrefix) !== 0) {
113
                continue;
114
            }
115
116
            $namesArray[] = substr($key, $prefixLen);
117
        }
118
119
        return $namesArray;
120
    }
121
}
122