Completed
Pull Request — master (#265)
by Chris
05:34
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 0
Metric Value
cc 5
eloc 37
nc 2
nop 0
dl 0
loc 51
rs 9.0168
c 0
b 0
f 0
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 2
    public function getConfigTreeBuilder() : TreeBuilder
30
    {
31 2
        $treeBuilder = new TreeBuilder('doctrine_migrations');
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Component\Config...eBuilder::__construct() has too many arguments starting with 'doctrine_migrations'. ( Ignorable by Annotation )

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

31
        $treeBuilder = /** @scrutinizer ignore-call */ new TreeBuilder('doctrine_migrations');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

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