Passed
Push — master ( 012829...2a72be )
by Jonathan
02:28
created

Configuration   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 93.48%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 82
ccs 43
cts 46
cp 0.9348
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigTreeBuilder() 0 50 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 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');
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 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');
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('organize_migrations')->defaultValue(false)
54 1
                    ->info('Organize migrations mode. Possible values are: "BY_YEAR", "BY_YEAR_AND_MONTH", false')
55 1
                    ->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 1
                        })
67 1
                        ->thenInvalid('Invalid organize migrations mode value %s')
68 1
                    ->end()
69 1
                    ->validate()
70 1
                        ->ifString()
71
                            ->then(static function ($v) {
72 1
                                return constant('Doctrine\Migrations\Configuration\Configuration::VERSIONS_ORGANIZATION_' . strtoupper($v));
73 1
                            })
74 1
                    ->end()
75 1
                ->end()
76 1
            ->end();
77
78 1
        return $treeBuilder;
79
    }
80
81
82
    /**
83
     * Find organize migrations modes for their names
84
     *
85
     * @return string[]
86
     */
87 1
    private function getOrganizeMigrationsModes() : array
88
    {
89 1
        $constPrefix = 'VERSIONS_ORGANIZATION_';
90 1
        $prefixLen   = strlen($constPrefix);
91 1
        $refClass    = new ReflectionClass('Doctrine\Migrations\Configuration\Configuration');
92 1
        $constsArray = $refClass->getConstants();
93 1
        $namesArray  = [];
94
95 1
        foreach ($constsArray as $key => $value) {
96 1
            if (strpos($key, $constPrefix) !== 0) {
97 1
                continue;
98
            }
99
100 1
            $namesArray[] = substr($key, $prefixLen);
101
        }
102
103 1
        return $namesArray;
104
    }
105
}
106