Passed
Push — master ( 1a0e8b...4e85a8 )
by Asmir
04:29 queued 11s
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 51
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 5.0144

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 37
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 51
rs 9.0168
ccs 33
cts 36
cp 0.9167
crap 5.0144

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

37
            $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...
38
        }
39
40 1
        $organizeMigrationModes = $this->getOrganizeMigrationsModes();
41
42
        $rootNode
43 1
            ->children()
44 1
                ->scalarNode('dir_name')->defaultValue('%kernel.root_dir%/DoctrineMigrations')->cannotBeEmpty()->end()
45 1
                ->scalarNode('namespace')->defaultValue('Application\Migrations')->cannotBeEmpty()->end()
46 1
                ->scalarNode('table_name')->defaultValue('migration_versions')->cannotBeEmpty()->end()
47 1
                ->scalarNode('column_name')->defaultValue('version')->end()
48 1
                ->scalarNode('column_length')->defaultValue(14)->end()
49 1
                ->scalarNode('executed_at_column_name')->defaultValue('executed_at')->end()
50 1
                ->scalarNode('all_or_nothing')->defaultValue(false)->end()
51 1
                ->scalarNode('name')->defaultValue('Application Migrations')->end()
52 1
                ->scalarNode('custom_template')->defaultValue(null)->end()
53 1
                ->scalarNode('check_database_platform')->defaultValue(true)->end()
54 1
                ->scalarNode('organize_migrations')->defaultValue(false)
55 1
                    ->info('Organize migrations mode. Possible values are: "BY_YEAR", "BY_YEAR_AND_MONTH", false')
56 1
                    ->validate()
57
                        ->ifTrue(static function ($v) use ($organizeMigrationModes) {
58 1
                            if ($v === false) {
59
                                return false;
60
                            }
61
62 1
                            if (is_string($v) && in_array(strtoupper($v), $organizeMigrationModes)) {
63 1
                                return false;
64
                            }
65
66
                            return true;
67 1
                        })
68 1
                        ->thenInvalid('Invalid organize migrations mode value %s')
69 1
                    ->end()
70 1
                    ->validate()
71 1
                        ->ifString()
72
                            ->then(static function ($v) {
73 1
                                return constant('Doctrine\Migrations\Configuration\Configuration::VERSIONS_ORGANIZATION_' . strtoupper($v));
74 1
                            })
75 1
                    ->end()
76 1
                ->end()
77 1
            ->end();
78
79 1
        return $treeBuilder;
80
    }
81
82
83
    /**
84
     * Find organize migrations modes for their names
85
     *
86
     * @return string[]
87
     */
88 1
    private function getOrganizeMigrationsModes() : array
89
    {
90 1
        $constPrefix = 'VERSIONS_ORGANIZATION_';
91 1
        $prefixLen   = strlen($constPrefix);
92 1
        $refClass    = new ReflectionClass('Doctrine\Migrations\Configuration\Configuration');
93 1
        $constsArray = $refClass->getConstants();
94 1
        $namesArray  = [];
95
96 1
        foreach ($constsArray as $key => $value) {
97 1
            if (strpos($key, $constPrefix) !== 0) {
98 1
                continue;
99
            }
100
101 1
            $namesArray[] = substr($key, $prefixLen);
102
        }
103
104 1
        return $namesArray;
105
    }
106
}
107