Completed
Pull Request — master (#6)
by James Ekow Abaka
20:00 queued 18:35
created

main.php ➔ get_container_settings()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 158

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 158
rs 8
c 0
b 0
f 0

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
/*
4
 * The MIT License
5
 *
6
 * Copyright 2015 ekow.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
require __DIR__ . "/../../../../vendor/autoload.php";
28
require_once __DIR__ . "/../src/globals.php";
29
30
use clearice\argparser\ArgumentParser;
31
use clearice\io\Io;
32
use yentu\manipulators\AbstractDatabaseManipulator;
33
use ntentan\atiaa\DriverFactory;
34
use yentu\Migrations;
35
use yentu\Yentu;
36
use ntentan\config\Config;
37
use yentu\commands\Migrate;
38
use ntentan\panie\Container;
39
use yentu\Cli;
40
use yentu\commands\Command;
41
42
$container = new Container();
43
$container->setup(get_container_settings());
44
$ui = $container->resolve(Cli::class);
45
$ui->run();
46
47
/**
48
 * Return the container setup.
49
 */
50
function get_container_settings() {
51
    return [
52
        Migrations::class => [Migrations::class, 'singleton' => true],
53
        Io::class => [Io::class, 'singleton' => true],
54
        Config::class => [
55
            'singleton' => true
56
        ],
57
        DriverFactory::class => [ 
58
            function($container) {
59
                return new DriverFactory($container->resolve(Config::class)->get('db'));        
60
            }, 
61
            'singleton' => true
62
        ],
63
        AbstractDatabaseManipulator::class => [
64
            function ($container) {
65
                $config = $container->resolve(Config::class)->get('db');
66
                $class = "\\yentu\\manipulators\\" . ucfirst($config['driver']);
67
                return $container->resolve($class, ['config' => $config]);
68
            },
69
            'singleton' => true
70
        ],
71
        Migrate::class => [
72
            Migrate::class,
73
            'calls' => ['setRollbackCommand']
74
        ],
75
        ArgumentParser::class => [
76
            function($container) {
77
                $io = $container->resolve(Io::class);
0 ignored issues
show
Unused Code introduced by
$io is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
                $argumentParser = new ArgumentParser();
79
                
80
                // Setup commands
81
                $argumentParser->addCommand(['name' => 'import', 'help' => 'import the schema of an existing database']);
82
                $argumentParser->addCommand(['name' => 'migrate', 'help' => 'run new migrations on the target database']);
83
                $argumentParser->addCommand(['name' => 'init', 'help' => 'initialize the yentu directory']);
84
                $argumentParser->addCommand(['name' => 'create', 'help' => 'create a new migration']);
85
                $argumentParser->addCommand(['name' => 'rollback', 'help' => 'rollback the previus migration which was run']);
86
                $argumentParser->addCommand(['name' => 'status', 'help' => 'display the current status of the migrations']);
87
                
88
                // Setup options
89
                $argumentParser->addOption([
90
                    'command' => 'import', 'short_name' => 'd', 'name' => 'skip-defaults',
91
                    'help' => 'do not import the default values of the columns'
92
                ]);
93
                $argumentParser->addOption([
94
                    'command' => 'init', 'short_name' => 'i', 'name' => 'interractive',
95
                    'help' => 'initialize yentu interractively'
96
                ]);
97
                $argumentParser->addOption([
98
                    'command' => 'init', 'short_name' => 'd', 'name' => 'driver',
99
                    'help' => 'database platform. Supports postgresql', 'type' => 'string'
100
                ]);
101
                $argumentParser->addOption([
102
                    'command' => 'init', 'short_name' => 'h', 'name' => 'host',
103
                    'help' => 'the hostname of the target database', 'type' => 'string'
104
                ]);
105
                $argumentParser->addOption([
106
                    'command' => 'init', 'short_name' => 'P', 'name' => 'port',
107
                    'help' => 'the port of the target database', 'type' => 'string'
108
                ]);
109
                $argumentParser->addOption([
110
                    'command' => 'init', 'short_name' => 'n', 'name' => 'dbname',
111
                    'help' => 'the name of the target database', 'type' => 'string'
112
                ]);
113
                $argumentParser->addOption([
114
                    'command' => 'init', 'short_name' => 'u', 'name' => 'user',
115
                    'help' => 'the user name on the target database', 'type' => 'string'
116
                ]);
117
                $argumentParser->addOption([
118
                    'command' => 'init', 'short_name' => 'p', 'name' => 'password',
119
                    'help' => 'the password of the user on the target database', 'type' => 'string'
120
                ]);
121
                
122
                $argumentParser->addOption([
123
                    'command' => 'migrate', 'name' => 'no-foreign-keys',
124
                    'help' => 'do not apply any database foriegn-key constraints'
125
                ]);
126
                $argumentParser->addOption([
127
                    'command' => 'migrate', 'name' => 'only-foreign-keys',
128
                    'help' => 'apply only database foreign-key constraints (fails on errors)'
129
                ]);
130
                $argumentParser->addOption([
131
                    'command' => 'migrate', 'name' => 'force-foreign-keys',
132
                    'help' => 'apply only database foreign-key constraints'
133
                ]);
134
                $argumentParser->addOption([
135
                    'command' => 'migrate', 'name' => 'dump-queries',
136
                    'help' => 'just dump the query and perform no action'
137
                ]);
138
                $argumentParser->addOption([
139
                    'command' => 'migrate', 'name' => 'dry',
140
                    'help' => 'perform a dry run. Do not alter the database in anyway'
141
                ]);
142
                $argumentParser->addOption([
143
                    'command' => 'migrate', 'name' => 'default-schema',
144
                    'type' => 'string', 'help' => 'use this as the default schema for all migrations'
145
                ]);
146
                $argumentParser->addOption([
147
                    'command' => 'migrate', 'name' => 'default-ondelete',
148
                    'help' => 'the default cascade action for foreign key deletes', 'type' => 'string'
149
                ]);
150
                $argumentParser->addOption([
151
                    'command' => 'migrate', 'name' => 'default-onupdate',
152
                    'help' => 'the default cascade action for foreign key updates', 'type' => 'string'
153
                
154
                ]);
155
                
156
                $argumentParser->addOption([
157
                    'short_name' => 'y', 'name' => 'home',
158
                    'help' => 'specifies where the yentu configurations and migrations are found',
159
                    'type' => 'string'
160
                ]);
161
                $argumentParser->addOption([
162
                    'short_name' => 'v', 'name' => 'verbose',
163
                    'help' => 'set level of verbosity. high, mid, low and none',
164
                    'type' => 'string'
165
                ]);
166
                $argumentParser->addOption(['name' => 'details', 'help' => 'show details of all migrations.', 'command' => 'status']);
167
                $argumentParser->enableHelp("Yentu database migration tool", "Report bugs on https://github.com/ntentan/yentu");            
168
169
                return $argumentParser;
170
            },
171
            'singleton' => true
172
        ], 
173
        Command::class => [
174
            function($container)
175
            {
176
                /**
177
                 * @var Container $container
178
                 * @var Migrations $migrations
179
                 */
180
181
                $argumentParser = $container->resolve(ArgumentParser::class);
182
                $arguments = $argumentParser->parse();
183
                if(isset($arguments['__command'])) {
184
                    $commandClass = "yentu\\commands\\" . ucfirst($arguments['__command']);
185
                    $defaultHome = $arguments['home'] ?? './yentu';
186
                    $config = $container->resolve(Config::class);
187
                    $config->readPath($defaultHome . "/config/default.conf.php");
188
189
                    $migrations = $container->resolve(
0 ignored issues
show
Unused Code introduced by
$migrations is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
190
                       Migrations::class, [
191
                           'config' => [
192
                               'variables' => $config->get('variables', []),
193
                               'other_migrations' => $config->get('other_migrations', []),
194
                               'home' => $defaultHome
195
                           ]
196
                       ]
197
                    );
198
                    $command = $container->resolve($commandClass);
199
                    $command->setOptions($arguments);
200
                    return $command;
201
                } else {
202
                    return null;
203
                }
204
            }
205
        ]
206
    ];
207
}
208
209