| Conditions | 8 |
| Paths | 96 |
| Total Lines | 68 |
| Code Lines | 46 |
| Lines | 32 |
| Ratio | 47.06 % |
| 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 |
||
| 45 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 46 | { |
||
| 47 | $this->detectDbSettings($output); |
||
| 48 | |||
| 49 | $settings = array(); |
||
| 50 | foreach ($this->dbSettings as $key => $value) { |
||
| 51 | $settings[$key] = (string) $value; |
||
| 52 | } |
||
| 53 | |||
| 54 | $isSocketConnect = $this->isSocketConnect; |
||
| 55 | |||
| 56 | // note: there is no need to specify the default port neither for PDO, nor JDBC nor CLI. |
||
| 57 | $portOrDefault = isset($this->dbSettings['port']) ? $this->dbSettings['port'] : 3306; |
||
| 58 | |||
| 59 | $pdoConnectionString = ''; |
||
| 60 | View Code Duplication | if ($isSocketConnect) { |
|
| 61 | $pdoConnectionString = sprintf( |
||
| 62 | 'mysql:unix_socket=%s;dbname=%s', |
||
| 63 | $this->dbSettings['unix_socket'], |
||
| 64 | $this->dbSettings['dbname'] |
||
| 65 | ); |
||
| 66 | } else { |
||
| 67 | $pdoConnectionString = sprintf( |
||
| 68 | 'mysql:host=%s;port=%s;dbname=%s', |
||
| 69 | $this->dbSettings['host'], |
||
| 70 | $portOrDefault, |
||
| 71 | $this->dbSettings['dbname'] |
||
| 72 | ); |
||
| 73 | } |
||
| 74 | $settings['PDO-Connection-String'] = $pdoConnectionString; |
||
| 75 | |||
| 76 | $jdbcConnectionString = ''; |
||
| 77 | View Code Duplication | if ($isSocketConnect) { |
|
| 78 | // isn't supported according to this post: http://stackoverflow.com/a/18493673/145829 |
||
| 79 | $jdbcConnectionString = 'Connecting using JDBC through a unix socket isn\'t supported!'; |
||
| 80 | } else { |
||
| 81 | $jdbcConnectionString = sprintf( |
||
| 82 | 'jdbc:mysql://%s:%s/%s?username=%s&password=%s', |
||
| 83 | $this->dbSettings['host'], |
||
| 84 | $portOrDefault, |
||
| 85 | $this->dbSettings['dbname'], |
||
| 86 | $this->dbSettings['username'], |
||
| 87 | $this->dbSettings['password'] |
||
| 88 | ); |
||
| 89 | } |
||
| 90 | $settings['JDBC-Connection-String'] = $jdbcConnectionString; |
||
| 91 | |||
| 92 | /* @var $database DatabaseHelper */ |
||
| 93 | $database = $this->getHelper('database'); |
||
| 94 | $mysqlCliString = 'mysql ' . $database->getMysqlClientToolConnectionString(); |
||
| 95 | $settings['MySQL-Cli-String'] = $mysqlCliString; |
||
| 96 | |||
| 97 | $rows = array(); |
||
| 98 | foreach ($settings as $settingName => $settingValue) { |
||
| 99 | $rows[] = array($settingName, $settingValue); |
||
| 100 | } |
||
| 101 | |||
| 102 | if (($settingArgument = $input->getArgument('setting')) !== null) { |
||
| 103 | if (!isset($settings[$settingArgument])) { |
||
| 104 | throw new InvalidArgumentException('Unknown setting: ' . $settingArgument); |
||
| 105 | } |
||
| 106 | $output->writeln((string) $settings[$settingArgument]); |
||
| 107 | View Code Duplication | } else { |
|
| 108 | $this->getHelper('table') |
||
| 109 | ->setHeaders(array('Name', 'Value')) |
||
| 110 | ->renderByFormat($output, $rows, $input->getOption('format')); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.