Completed
Pull Request — master (#294)
by Asmir
14:10 queued 10:15
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 111
Code Lines 89

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 84
CRAP Score 5.0023

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 89
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 111
ccs 84
cts 88
cp 0.9545
crap 5.0023
rs 7.9288

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 count;
12
use function in_array;
13
use function is_string;
14
use function method_exists;
15
use function strlen;
16
use function strpos;
17
use function strtoupper;
18
use function substr;
19
20
/**
21
 * DoctrineMigrationsExtension configuration structure.
22
 */
23
class Configuration implements ConfigurationInterface
24
{
25
    /**
26
     * Generates the configuration tree.
27
     *
28
     * @return TreeBuilder The config tree builder
29
     */
30 2
    public function getConfigTreeBuilder() : TreeBuilder
31
    {
32 2
        $treeBuilder = new TreeBuilder('doctrine_migrations');
33
34 2
        if (method_exists($treeBuilder, 'getRootNode')) {
35 2
            $rootNode = $treeBuilder->getRootNode();
36
        } else {
37
            // BC layer for symfony/config 4.1 and older
38
            $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

38
            $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...
39
        }
40
41 2
        $organizeMigrationModes = $this->getOrganizeMigrationsModes();
42
43
        $rootNode
44 2
            ->children()
45 2
                ->scalarNode('name')->defaultValue('Application Migrations')->end()
46
47
                // 3.x forward compatibility layer
48 2
                ->arrayNode('migrations_paths')
49 2
                    ->info('A list of pairs namespace/path where to look for migrations.')
50 2
                    ->useAttributeAsKey('name')
51 2
                    ->defaultValue([])
52 2
                    ->prototype('scalar')->end()
53 2
                    ->validate()
54
                        ->ifTrue(static function ($v) {
55
                            return count($v) === 0;
56 2
                        })
57 2
                        ->thenInvalid('At least one migrations path must be specified.')
58
59
                        ->ifTrue(static function ($v) {
60 1
                            return count($v) >  1;
61 2
                        })
62 2
                        ->thenInvalid('Maximum one migration path can be specified with the 2.x version.')
63 2
                    ->end()
64 2
                ->end()
65
66 2
                ->arrayNode('storage')
67 2
                    ->info('Storage to use for migration status metadata.')
68 2
                    ->children()
69 2
                        ->arrayNode('table_storage')
70 2
                            ->info('The default metadata storage, implemented as table in the database.')
71 2
                            ->children()
72 2
                                ->scalarNode('table_name')->defaultValue(null)->cannotBeEmpty()->end()
73 2
                                ->scalarNode('version_column_name')->defaultValue(null)->end()
74 2
                                ->scalarNode('version_column_length')
75 2
                                    ->defaultValue(null)
76 2
                                    ->validate()
77
                                        ->ifTrue(static function ($v) {
78 1
                                            return $v< 1024;
79 2
                                        })
80 2
                                        ->thenInvalid('The minimum length for the version column is 1024.')
81 2
                                    ->end()
82 2
                                ->end()
83 2
                                ->scalarNode('executed_at_column_name')->defaultValue(null)->end()
84 2
                            ->end()
85 2
                        ->end()
86 2
                    ->end()
87 2
                ->end()
88
89 2
                ->scalarNode('dir_name')
90 2
                    ->defaultValue('%kernel.root_dir%/DoctrineMigrations')->cannotBeEmpty()
91 2
                    ->setDeprecated('The "%node%" option is deprecated. Use "migrations_paths" instead.')
92 2
                ->end()
93 2
                ->scalarNode('namespace')
94 2
                    ->defaultValue('Application\Migrations')->cannotBeEmpty()
95 2
                    ->setDeprecated('The "%node%" option is deprecated. Use "migrations_paths" instead.')
96 2
                ->end()
97 2
                ->scalarNode('table_name')
98 2
                    ->defaultValue('migration_versions')->cannotBeEmpty()
99 2
                    ->setDeprecated('The "%node%" option is deprecated. Use "storage.table_storage.table_name" instead.')
100 2
                ->end()
101 2
                ->scalarNode('column_name')
102 2
                    ->defaultValue('version')
103 2
                    ->setDeprecated('The "%node%" option is deprecated. Use "storage.table_storage.version_column_name" instead.')
104 2
                ->end()
105 2
                ->scalarNode('column_length')
106 2
                    ->defaultValue(14)
107 2
                    ->setDeprecated('The "%node%" option is deprecated. Use "storage.table_storage.version_column_length" instead.')
108 2
                ->end()
109 2
                ->scalarNode('executed_at_column_name')
110 2
                    ->defaultValue('executed_at')
111 2
                    ->setDeprecated('The "%node%" option is deprecated. Use "storage.table_storage.executed_at_column_name" instead.')
112 2
                ->end()
113 2
                ->scalarNode('all_or_nothing')->defaultValue(false)->end()
114 2
                ->scalarNode('custom_template')->defaultValue(null)->end()
115 2
                ->scalarNode('organize_migrations')->defaultValue(false)
116 2
                    ->info('Organize migrations mode. Possible values are: "BY_YEAR", "BY_YEAR_AND_MONTH", false')
117 2
                    ->validate()
118
                        ->ifTrue(static function ($v) use ($organizeMigrationModes) {
119 1
                            if ($v === false) {
120
                                return false;
121
                            }
122
123 1
                            if (is_string($v) && in_array(strtoupper($v), $organizeMigrationModes)) {
124 1
                                return false;
125
                            }
126
127
                            return true;
128 2
                        })
129 2
                        ->thenInvalid('Invalid organize migrations mode value %s')
130 2
                    ->end()
131 2
                    ->validate()
132 2
                        ->ifString()
133
                            ->then(static function ($v) {
134 1
                                return constant('Doctrine\Migrations\Configuration\Configuration::VERSIONS_ORGANIZATION_' . strtoupper($v));
135 2
                            })
136 2
                    ->end()
137 2
                ->end()
138 2
            ->end();
139
140 2
        return $treeBuilder;
141
    }
142
143
144
    /**
145
     * Find organize migrations modes for their names
146
     *
147
     * @return string[]
148
     */
149 2
    private function getOrganizeMigrationsModes() : array
150
    {
151 2
        $constPrefix = 'VERSIONS_ORGANIZATION_';
152 2
        $prefixLen   = strlen($constPrefix);
153 2
        $refClass    = new ReflectionClass('Doctrine\Migrations\Configuration\Configuration');
154 2
        $constsArray = $refClass->getConstants();
155 2
        $namesArray  = [];
156
157 2
        foreach ($constsArray as $key => $value) {
158 2
            if (strpos($key, $constPrefix) !== 0) {
159 2
                continue;
160
            }
161
162 2
            $namesArray[] = substr($key, $prefixLen);
163
        }
164
165 2
        return $namesArray;
166
    }
167
}
168