Passed
Pull Request — master (#278)
by Asmir
02:33
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 112
Code Lines 93

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 89
CRAP Score 5.0008

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 93
c 2
b 0
f 0
dl 0
loc 112
ccs 89
cts 92
cp 0.9674
rs 7.8416
cc 5
nc 2
nop 0
crap 5.0008

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