| Conditions | 13 |
| Paths | 136 |
| Total Lines | 116 |
| Code Lines | 80 |
| 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 |
||
| 26 | public function execute() |
||
| 27 | { |
||
| 28 | $this->notEmptyCallback = function ($input) { |
||
| 29 | if (empty($input)) { |
||
| 30 | throw new \InvalidArgumentException('Please enter a value'); |
||
| 31 | } |
||
| 32 | return $input; |
||
| 33 | }; |
||
| 34 | |||
| 35 | $dbOptions = array('--dbHost', '--dbUser', '--dbPass', '--dbName'); |
||
| 36 | $dbOptionsFound = 0; |
||
| 37 | foreach ($dbOptions as $dbOption) { |
||
| 38 | foreach ($this->getCliArguments() as $definedCliOption) { |
||
| 39 | if (BinaryString::startsWith($definedCliOption, $dbOption)) { |
||
| 40 | $dbOptionsFound++; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | $hasAllOptions = $dbOptionsFound == 4; |
||
| 46 | |||
| 47 | // if all database options were passed in at cmd line |
||
| 48 | if ($hasAllOptions) { |
||
| 49 | $this->config->setString('db_host', $this->input->getOption('dbHost')); |
||
| 50 | $this->config->setString('db_user', $this->input->getOption('dbUser')); |
||
| 51 | $this->config->setString('db_pass', $this->input->getOption('dbPass')); |
||
| 52 | $this->config->setString('db_name', $this->input->getOption('dbName')); |
||
| 53 | $this->config->setInt('db_port', intval($this->input->getOption('dbPort'))); |
||
|
|
|||
| 54 | $db = $this->validateDatabaseSettings($this->input, $this->output); |
||
| 55 | |||
| 56 | if ($db === false) { |
||
| 57 | throw new \InvalidArgumentException("Database configuration is invalid", null); |
||
| 58 | } |
||
| 59 | } else { |
||
| 60 | /** @var DialogHelper $dialog */ |
||
| 61 | $dialog = $this->getCommand()->getHelperSet()->get('dialog'); |
||
| 62 | do { |
||
| 63 | // Host |
||
| 64 | $dbHostDefault = $this->input->getOption('dbHost') ? |
||
| 65 | $this->input->getOption('dbHost') : $this->commandConfig['installation']['db']['host']; |
||
| 66 | $this->config->setString( |
||
| 67 | 'db_host', |
||
| 68 | $dialog->askAndValidate( |
||
| 69 | $this->output, |
||
| 70 | '<question>Please enter the database host</question> <comment>[' . |
||
| 71 | $dbHostDefault . ']</comment>: ', |
||
| 72 | $this->notEmptyCallback, |
||
| 73 | false, |
||
| 74 | $dbHostDefault |
||
| 75 | ) |
||
| 76 | ); |
||
| 77 | |||
| 78 | // Port |
||
| 79 | $dbPortDefault = $this->input->getOption('dbPort') ? |
||
| 80 | $this->input->getOption('dbPort') : $this->commandConfig['installation']['db']['port']; |
||
| 81 | $this->config->setInt( |
||
| 82 | 'db_port', |
||
| 83 | intval($dialog->askAndValidate( |
||
| 84 | $this->output, |
||
| 85 | '<question>Please enter the database port </question> <comment>[' . |
||
| 86 | $dbPortDefault . ']</comment>: ', |
||
| 87 | $this->notEmptyCallback, |
||
| 88 | false, |
||
| 89 | $dbPortDefault |
||
| 90 | )) |
||
| 91 | ); |
||
| 92 | |||
| 93 | // User |
||
| 94 | $dbUserDefault = $this->input->getOption('dbUser') ? |
||
| 95 | $this->input->getOption('dbUser') : $this->commandConfig['installation']['db']['user']; |
||
| 96 | $this->config->setString( |
||
| 97 | 'db_user', |
||
| 98 | $dialog->askAndValidate( |
||
| 99 | $this->output, |
||
| 100 | '<question>Please enter the database username</question> <comment>[' . |
||
| 101 | $dbUserDefault . ']</comment>: ', |
||
| 102 | $this->notEmptyCallback, |
||
| 103 | false, |
||
| 104 | $dbUserDefault |
||
| 105 | ) |
||
| 106 | ); |
||
| 107 | |||
| 108 | // Password |
||
| 109 | $dbPassDefault = $this->input->getOption('dbPass') ? |
||
| 110 | $this->input->getOption('dbPass') : $this->commandConfig['installation']['db']['pass']; |
||
| 111 | $this->config->setString( |
||
| 112 | 'db_pass', |
||
| 113 | $dialog->ask( |
||
| 114 | $this->output, |
||
| 115 | '<question>Please enter the database password</question> <comment>[' . |
||
| 116 | $dbPassDefault . ']</comment>: ', |
||
| 117 | $dbPassDefault |
||
| 118 | ) |
||
| 119 | ); |
||
| 120 | |||
| 121 | // DB-Name |
||
| 122 | $dbNameDefault = $this->input->getOption('dbName') ? |
||
| 123 | $this->input->getOption('dbName') : $this->commandConfig['installation']['db']['name']; |
||
| 124 | $this->config->setString( |
||
| 125 | 'db_name', |
||
| 126 | $dialog->askAndValidate( |
||
| 127 | $this->output, |
||
| 128 | '<question>Please enter the database name</question> <comment>[' . |
||
| 129 | $dbNameDefault . ']</comment>: ', |
||
| 130 | $this->notEmptyCallback, |
||
| 131 | false, |
||
| 132 | $dbNameDefault |
||
| 133 | ) |
||
| 134 | ); |
||
| 135 | |||
| 136 | $db = $this->validateDatabaseSettings($this->input, $this->output); |
||
| 137 | } while ($db === false); |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->config->setObject('db', $db); |
||
| 141 | } |
||
| 142 | |||
| 201 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: