Failed Conditions
Push — master ( 521d07...1946f9 )
by Asmir
05:03 queued 04:33
created

Configuration   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 100
c 2
b 0
f 0
dl 0
loc 141
ccs 96
cts 100
cp 0.96
rs 10

2 Methods

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