Conditions | 12 |
Paths | 104 |
Total Lines | 47 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
48 | protected function selectHosts(Input $input, Output $output): array |
||
49 | { |
||
50 | $output->getFormatter()->setStyle('success', new OutputFormatterStyle('green')); |
||
51 | if (!$output->isDecorated() && !defined('NO_ANSI')) { |
||
52 | define('NO_ANSI', 'true'); |
||
53 | } |
||
54 | $selector = $input->getArgument('selector'); |
||
55 | $selector = empty($selector) ? Deployer::get()->config->get('default_selector', '') : $selector; |
||
56 | $selectExpression = is_array($selector) ? implode(',', $selector) : $selector; |
||
57 | |||
58 | if (empty($selectExpression)) { |
||
59 | if (count($this->deployer->hosts) === 0) { |
||
60 | throw new ConfigurationException("No host configured.\nSpecify at least one host: `localhost();`."); |
||
61 | } elseif (count($this->deployer->hosts) === 1) { |
||
62 | $hosts = $this->deployer->hosts->all(); |
||
63 | } elseif ($input->isInteractive()) { |
||
64 | $hostsAliases = []; |
||
65 | foreach ($this->deployer->hosts as $host) { |
||
66 | $hostsAliases[] = $host->getAlias(); |
||
67 | } |
||
68 | /** @var QuestionHelper $helper */ |
||
69 | $helper = $this->getHelper('question'); |
||
70 | $question = new ChoiceQuestion( |
||
71 | '<question>Select hosts:</question> (comma separated)', |
||
72 | $hostsAliases, |
||
73 | ); |
||
74 | $question->setMultiselect(true); |
||
75 | $question->setErrorMessage('There is no "%s" host.'); |
||
76 | $answer = $helper->ask($input, $output, $question); |
||
77 | $answer = array_unique($answer); |
||
78 | $hosts = $this->deployer->hosts->select(function (Host $host) use ($answer) { |
||
79 | return in_array($host->getAlias(), $answer, true); |
||
80 | }); |
||
81 | } |
||
82 | } else { |
||
83 | $hosts = $this->deployer->selector->select($selectExpression); |
||
84 | } |
||
85 | |||
86 | if (empty($hosts)) { |
||
87 | $message = 'No host selected.'; |
||
88 | if (!empty($selectExpression)) { |
||
89 | $message .= " Please, check your selector:\n\n $selectExpression"; |
||
90 | } |
||
91 | throw new Exception($message); |
||
92 | } |
||
93 | |||
94 | return $hosts; |
||
95 | } |
||
124 |