Completed
Pull Request — master (#278)
by Asmir
06:15
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 109
Code Lines 91

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 87
CRAP Score 5.0009

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 91
c 2
b 0
f 0
dl 0
loc 109
ccs 87
cts 90
cp 0.9667
rs 7.8852
cc 5
nc 2
nop 0
crap 5.0009

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 6
    public function getConfigTreeBuilder() : TreeBuilder
31
    {
32 6
        $treeBuilder = new TreeBuilder('doctrine_migrations');
33
34 6
        if (method_exists($treeBuilder, 'getRootNode')) {
35 6
            $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
Bug introduced by
The method root() does not exist on Symfony\Component\Config...ion\Builder\TreeBuilder. ( Ignorable by Annotation )

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

38
            /** @scrutinizer ignore-call */ 
39
            $rootNode = $treeBuilder->root('doctrine_migrations', 'array');

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...
39
        }
40
41 6
        $organizeMigrationModes = $this->getOrganizeMigrationsModes();
42
43
        $rootNode
44 6
            ->children()
45 6
                ->scalarNode('name')->defaultValue('Application Migrations')->end()
46 6
                ->arrayNode('migrations_paths')
47
48 6
                    ->info('A list of pairs namespace/path where to look for migrations.')
49 6
                    ->requiresAtLeastOneElement()
50 6
                    ->useAttributeAsKey('name')
51 6
                    ->defaultValue(['%kernel.root_dir%/DoctrineMigrations' => 'Application\Migrations'])
52 6
                    ->prototype('scalar')->end()
53 6
                    ->validate()
54
                        ->ifTrue(static function ($v) {
55 1
                            return count($v) === 0;
56 6
                        })
57 6
                        ->thenInvalid('At least one migrations path must be specified.')
58 6
                    ->end()
59 6
                 ->end()
60
61 6
                ->arrayNode('storage')
62 6
                    ->addDefaultsIfNotSet()
63 6
                    ->info('Storage to use for migration status metadata.')
64 6
                    ->children()
65 6
                        ->scalarNode('id')
66 6
                            ->info('Custom metadata storage service ID.')
67 6
                            ->defaultValue(null)
68 6
                        ->end()
69 6
                        ->arrayNode('table_storage')
70 6
                            ->addDefaultsIfNotSet()
71 6
                            ->info('The default metadata storage, implemented as table in the database.')
72 6
                            ->children()
73 6
                                ->scalarNode('table_name')->defaultValue(null)->cannotBeEmpty()->end()
74 6
                                ->scalarNode('version_column_name')->defaultValue(null)->end()
75 6
                                ->scalarNode('version_column_length')->defaultValue(null)->end()
76 6
                                ->scalarNode('executed_at_column_name')->defaultValue(null)->end()
77 6
                                ->scalarNode('execution_time_column_name')->defaultValue(null)->end()
78 6
                            ->end()
79 6
                        ->end()
80 6
                    ->end()
81
82 6
                ->end()
83 6
                ->arrayNode('migrations')
84 6
                    ->info('A list of migrations to load in addition the the one discovered via "migrations_paths".')
85 6
                    ->prototype('scalar')->end()
86 6
                    ->defaultValue([])
87 6
                ->end()
88 6
                ->scalarNode('sorter')
89 6
                    ->info('Alternative migrations sorting algorithm')
90 6
                    ->defaultValue(null)
91 6
                ->end()
92 6
                ->scalarNode('connection')
93 6
                    ->info('Connection name to use for the migrations database.')
94 6
                    ->defaultValue(null)
95 6
                ->end()
96 6
                ->scalarNode('em')
97 6
                    ->info('Entity manager name to use for the migrations database (available when doctrine/orm is installed).')
98 6
                    ->defaultValue(null)
99 6
                ->end()
100 6
                ->scalarNode('all_or_nothing')
101 6
                    ->info('Run all migrations in a transaction.')
102 6
                    ->defaultValue(false)
103 6
                ->end()
104 6
                ->scalarNode('check_database_platform')
105 6
                    ->info('Adds an extra check in the generated migrations to ensure that is executed on the same database type.')
106 6
                    ->defaultValue(true)
107 6
                ->end()
108 6
                ->scalarNode('custom_template')
109 6
                    ->info('Custom template path for generated migration classes.')
110 6
                    ->defaultValue(null)
111 6
                ->end()
112 6
                ->scalarNode('organize_migrations')
113 6
                    ->defaultValue(false)
114 6
                    ->info('Organize migrations mode. Possible values are: "BY_YEAR", "BY_YEAR_AND_MONTH", false')
115 6
                    ->validate()
116
                        ->ifTrue(static function ($v) use ($organizeMigrationModes) {
117 1
                            if ($v === false) {
118
                                return false;
119
                            }
120
121 1
                            if (is_string($v) && in_array(strtoupper($v), $organizeMigrationModes, true)) {
122 1
                                return false;
123
                            }
124
125
                            return true;
126 6
                        })
127 6
                        ->thenInvalid('Invalid organize migrations mode value %s')
128 6
                    ->end()
129 6
                    ->validate()
130 6
                        ->ifString()
131
                            ->then(static function ($v) {
132 1
                                return constant('Doctrine\Migrations\Configuration\Configuration::VERSIONS_ORGANIZATION_' . strtoupper($v));
133 6
                            })
134 6
                    ->end()
135 6
                ->end()
136 6
            ->end();
137
138 6
        return $treeBuilder;
139
    }
140
141
142
    /**
143
     * Find organize migrations modes for their names
144
     *
145
     * @return string[]
146
     */
147 6
    private function getOrganizeMigrationsModes() : array
148
    {
149 6
        $constPrefix = 'VERSIONS_ORGANIZATION_';
150 6
        $prefixLen   = strlen($constPrefix);
151 6
        $refClass    = new ReflectionClass('Doctrine\Migrations\Configuration\Configuration');
152 6
        $constsArray = $refClass->getConstants();
153 6
        $namesArray  = [];
154
155 6
        foreach ($constsArray as $key => $value) {
156 6
            if (strpos($key, $constPrefix) !== 0) {
157
                continue;
158
            }
159
160 6
            $namesArray[] = substr($key, $prefixLen);
161
        }
162
163 6
        return $namesArray;
164
    }
165
}
166