| Conditions | 2 |
| Paths | 1 |
| Total Lines | 147 |
| 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 |
||
| 49 | function get_container_settings() { |
||
| 50 | return [ |
||
| 51 | Yentu::class => [Yentu::class, 'singleton' => true], |
||
| 52 | Io::class => [Io::class, 'singleton' => true], |
||
| 53 | Config::class => [ |
||
| 54 | 'singleton' => true |
||
| 55 | ], |
||
| 56 | DriverFactory::class => [ |
||
| 57 | function($container) { |
||
| 58 | return new DriverFactory($container->resolve(Config::class)->get('db')); |
||
| 59 | }, |
||
| 60 | 'singleton' => true |
||
| 61 | ], |
||
| 62 | AbstractDatabaseManipulator::class => [ |
||
| 63 | function ($container) { |
||
| 64 | $config = $container->resolve(Config::class)->get('db'); |
||
| 65 | $class = "\\yentu\\manipulators\\" . ucfirst($config['driver']); |
||
| 66 | return $container->resolve($class, ['config' => $config]); |
||
| 67 | }, |
||
| 68 | 'singleton' => true |
||
| 69 | ], |
||
| 70 | Migrate::class => [ |
||
| 71 | Migrate::class, |
||
| 72 | 'calls' => ['setRollbackCommand'] |
||
| 73 | ], |
||
| 74 | ArgumentParser::class => [ |
||
| 75 | function($container) { |
||
| 76 | $io = $container->resolve(Io::class); |
||
|
|
|||
| 77 | $argumentParser = new ArgumentParser(); |
||
| 78 | |||
| 79 | // Setup commands |
||
| 80 | $argumentParser->addCommand(['name' => 'import', 'help' => 'import the schema of an existing database']); |
||
| 81 | $argumentParser->addCommand(['name' => 'migrate', 'help' => 'run new migrations on the target database']); |
||
| 82 | $argumentParser->addCommand(['name' => 'init', 'help' => 'initialize the yentu directory']); |
||
| 83 | $argumentParser->addCommand(['name' => 'create', 'help' => 'create a new migration']); |
||
| 84 | $argumentParser->addCommand(['name' => 'rollback', 'help' => 'rollback the previus migration which was run']); |
||
| 85 | $argumentParser->addCommand(['name' => 'status', 'help' => 'display the current status of the migrations']); |
||
| 86 | |||
| 87 | // Setup options |
||
| 88 | $argumentParser->addOption([ |
||
| 89 | 'command' => 'import', 'short_name' => 'd', 'name' => 'skip-defaults', |
||
| 90 | 'help' => 'do not import the default values of the columns' |
||
| 91 | ]); |
||
| 92 | $argumentParser->addOption([ |
||
| 93 | 'command' => 'init', 'short_name' => 'i', 'name' => 'interractive', |
||
| 94 | 'help' => 'initialize yentu interractively' |
||
| 95 | ]); |
||
| 96 | $argumentParser->addOption([ |
||
| 97 | 'command' => 'init', 'short_name' => 'd', 'name' => 'driver', |
||
| 98 | 'help' => 'database platform. Supports postgresql', 'type' => 'string' |
||
| 99 | ]); |
||
| 100 | $argumentParser->addOption([ |
||
| 101 | 'command' => 'init', 'short_name' => 'h', 'name' => 'host', |
||
| 102 | 'help' => 'the hostname of the target database', 'type' => 'string' |
||
| 103 | ]); |
||
| 104 | $argumentParser->addOption([ |
||
| 105 | 'command' => 'init', 'short_name' => 'P', 'name' => 'port', |
||
| 106 | 'help' => 'the port of the target database', 'type' => 'string' |
||
| 107 | ]); |
||
| 108 | $argumentParser->addOption([ |
||
| 109 | 'command' => 'init', 'short_name' => 'n', 'name' => 'dbname', |
||
| 110 | 'help' => 'the name of the target database', 'type' => 'string' |
||
| 111 | ]); |
||
| 112 | $argumentParser->addOption([ |
||
| 113 | 'command' => 'init', 'short_name' => 'u', 'name' => 'user', |
||
| 114 | 'help' => 'the user name on the target database', 'type' => 'string' |
||
| 115 | ]); |
||
| 116 | $argumentParser->addOption([ |
||
| 117 | 'command' => 'init', 'short_name' => 'p', 'name' => 'password', |
||
| 118 | 'help' => 'the password of the user on the target database', 'type' => 'string' |
||
| 119 | ]); |
||
| 120 | |||
| 121 | $argumentParser->addOption([ |
||
| 122 | 'command' => 'migrate', 'name' => 'no-foreign-keys', |
||
| 123 | 'help' => 'do not apply any database foriegn-key constraints' |
||
| 124 | ]); |
||
| 125 | $argumentParser->addOption([ |
||
| 126 | 'command' => 'migrate', 'name' => 'only-foreign-keys', |
||
| 127 | 'help' => 'apply only database foreign-key constraints (fails on errors)' |
||
| 128 | ]); |
||
| 129 | $argumentParser->addOption([ |
||
| 130 | 'command' => 'migrate', 'name' => 'force-foreign-keys', |
||
| 131 | 'help' => 'apply only database foreign-key constraints' |
||
| 132 | ]); |
||
| 133 | $argumentParser->addOption([ |
||
| 134 | 'command' => 'migrate', 'name' => 'dump-queries', |
||
| 135 | 'help' => 'just dump the query and perform no action' |
||
| 136 | ]); |
||
| 137 | $argumentParser->addOption([ |
||
| 138 | 'command' => 'migrate', 'name' => 'dry', |
||
| 139 | 'help' => 'perform a dry run. Do not alter the database in anyway' |
||
| 140 | ]); |
||
| 141 | $argumentParser->addOption([ |
||
| 142 | 'command' => 'migrate', 'name' => 'default-schema', |
||
| 143 | 'type' => 'string', 'help' => 'use this as the default schema for all migrations' |
||
| 144 | ]); |
||
| 145 | $argumentParser->addOption([ |
||
| 146 | 'command' => 'migrate', 'name' => 'default-ondelete', |
||
| 147 | 'help' => 'the default cascade action for foreign key deletes', 'type' => 'string' |
||
| 148 | ]); |
||
| 149 | $argumentParser->addOption([ |
||
| 150 | 'command' => 'migrate', 'name' => 'default-onupdate', |
||
| 151 | 'help' => 'the default cascade action for foreign key updates', 'type' => 'string' |
||
| 152 | |||
| 153 | ]); |
||
| 154 | |||
| 155 | $argumentParser->addOption([ |
||
| 156 | 'short_name' => 'y', 'name' => 'home', |
||
| 157 | 'help' => 'specifies where the yentu configurations and migrations are found', |
||
| 158 | 'type' => 'string' |
||
| 159 | ]); |
||
| 160 | $argumentParser->addOption([ |
||
| 161 | 'short_name' => 'v', 'name' => 'verbose', |
||
| 162 | 'help' => 'set level of verbosity. high, mid, low and none', |
||
| 163 | 'type' => 'string' |
||
| 164 | ]); |
||
| 165 | $argumentParser->addOption(['name' => 'details', 'help' => 'show details of all migrations.', 'command' => 'status']); |
||
| 166 | $argumentParser->enableHelp("Yentu database migration tool", "Report bugs on https://github.com/ntentan/yentu"); |
||
| 167 | |||
| 168 | return $argumentParser; |
||
| 169 | }, |
||
| 170 | 'singleton' => true |
||
| 171 | ], |
||
| 172 | Command::class => [ |
||
| 173 | function($container) { |
||
| 174 | $argumentParser = $container->resolve(ArgumentParser::class); |
||
| 175 | $arguments = $argumentParser->parse(); |
||
| 176 | if(isset($arguments['__command'])) { |
||
| 177 | $commandClass = "yentu\\commands\\" . ucfirst($arguments['__command']); |
||
| 178 | $defaultHome = $arguments['home'] ?? './yentu'; |
||
| 179 | $config = $container->resolve(Config::class); |
||
| 180 | $config->readPath($defaultHome . "/config/default.conf.php"); |
||
| 181 | $yentu = $container->resolve( |
||
| 182 | Yentu::class, [ |
||
| 183 | 'migrationVariables' => $config->get('variables', []), |
||
| 184 | 'otherMigrations' => $config->get('other_migrations', []) |
||
| 185 | ] |
||
| 186 | ); |
||
| 187 | $yentu->setDefaultHome($defaultHome); |
||
| 188 | return $container->resolve($commandClass); |
||
| 189 | } else { |
||
| 190 | return null; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | ] |
||
| 194 | ]; |
||
| 195 | } |
||
| 196 | |||
| 197 |
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.