| Conditions | 6 |
| Paths | 18 |
| Total Lines | 58 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| Bugs | 1 | Features | 3 |
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 |
||
| 41 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
|
|
|||
| 42 | { |
||
| 43 | // logger |
||
| 44 | $logger = $this->getApplication()->getLogger(); |
||
| 45 | if (!isset($logger)) { |
||
| 46 | $verbosityLevelMap = [ |
||
| 47 | LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL, |
||
| 48 | LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE, |
||
| 49 | LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG, |
||
| 50 | ]; |
||
| 51 | $logger = new ConsoleLogger($output, $verbosityLevelMap); |
||
| 52 | $this->getApplication()->setLogger($logger); |
||
| 53 | } |
||
| 54 | |||
| 55 | // config |
||
| 56 | $config = $this->getApplication()->getConfig(); |
||
| 57 | if (!isset($config)) { |
||
| 58 | $config = new Config($input->getOption('config')); |
||
| 59 | try { |
||
| 60 | $config->loadConfigFile(); |
||
| 61 | } catch (\RuntimeException $e) { |
||
| 62 | $logger->critical($e->getMessage()); |
||
| 63 | $this->setCode(function () { |
||
| 64 | return 1; |
||
| 65 | }); |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | $this->getApplication()->setConfig($config); |
||
| 69 | } |
||
| 70 | |||
| 71 | // compatibility: first client available at old config names |
||
| 72 | // this overrides config values to options from command line arguments |
||
| 73 | $firstTransmission = $config->get('transmission')[0]; |
||
| 74 | $vars = ['host', 'port', 'user', 'password']; |
||
| 75 | foreach ($vars as $var) { |
||
| 76 | $configName = 'transmission-' . $var; |
||
| 77 | $config->set($configName, $this->getInputOption($input, $configName, $firstTransmission[$var])); |
||
| 78 | } |
||
| 79 | |||
| 80 | // client |
||
| 81 | $client = $this->getApplication()->getClient(); |
||
| 82 | if (!isset($client)) { |
||
| 83 | $client = $this->createTransmissionClient( |
||
| 84 | $config->get('transmission-host'), |
||
| 85 | $config->get('transmission-port'), |
||
| 86 | $config->get('transmission-user'), |
||
| 87 | $config->get('transmission-password') |
||
| 88 | ); |
||
| 89 | $this->getApplication()->setClient($client); |
||
| 90 | } |
||
| 91 | |||
| 92 | $logger->info('[{date}] command: {args}', [ |
||
| 93 | 'date' => date('Y-m-d H:i:s'), |
||
| 94 | 'args' => implode(' ', array_slice($_SERVER['argv'], 1)), |
||
| 95 | ]); |
||
| 96 | |||
| 97 | parent::initialize($input, $output); |
||
| 98 | } |
||
| 99 | |||
| 156 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: