| Conditions | 2 |
| Paths | 1 |
| Total Lines | 158 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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); |
||
|
|
|||
| 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( |
||
| 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 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.