Failed Conditions
Push — master ( 795476...31c911 )
by Asmir
48s queued 11s
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 5
Paths 2

Size

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

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