Complex classes like QuestionHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QuestionHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class QuestionHelper extends Helper |
||
35 | { |
||
36 | private $inputStream; |
||
37 | private static $shell; |
||
38 | private static $stty; |
||
39 | |||
40 | /** |
||
41 | * Asks a question to the user. |
||
42 | * |
||
43 | * @param InputInterface $input An InputInterface instance |
||
44 | * @param OutputInterface $output An OutputInterface instance |
||
45 | * @param Question $question The question to ask |
||
46 | * |
||
47 | * @return string The user answer |
||
48 | * |
||
49 | * @throws RuntimeException If there is no data to read in the input stream |
||
50 | */ |
||
51 | public function ask(InputInterface $input, OutputInterface $output, Question $question) |
||
73 | |||
74 | /** |
||
75 | * Sets the input stream to read from when interacting with the user. |
||
76 | * |
||
77 | * This is mainly useful for testing purpose. |
||
78 | * |
||
79 | * @param resource $stream The input stream |
||
80 | * |
||
81 | * @throws InvalidArgumentException In case the stream is not a resource |
||
82 | */ |
||
83 | public function setInputStream($stream) |
||
91 | |||
92 | /** |
||
93 | * Returns the helper's input stream. |
||
94 | * |
||
95 | * @return resource |
||
96 | */ |
||
97 | public function getInputStream() |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | public function getName() |
||
109 | |||
110 | /** |
||
111 | * Asks the question to the user. |
||
112 | * |
||
113 | * This method is public for PHP 5.3 compatibility, it should be private. |
||
114 | * |
||
115 | * @param OutputInterface $output |
||
116 | * @param Question $question |
||
117 | * |
||
118 | * @return bool|mixed|null|string |
||
119 | * |
||
120 | * @throws \Exception |
||
121 | * @throws \RuntimeException |
||
122 | */ |
||
123 | public function doAsk(OutputInterface $output, Question $question) |
||
157 | |||
158 | /** |
||
159 | * Outputs the question prompt. |
||
160 | * |
||
161 | * @param OutputInterface $output |
||
162 | * @param Question $question |
||
163 | */ |
||
164 | protected function writePrompt(OutputInterface $output, Question $question) |
||
183 | |||
184 | /** |
||
185 | * Outputs an error message. |
||
186 | * |
||
187 | * @param OutputInterface $output |
||
188 | * @param \Exception $error |
||
189 | */ |
||
190 | protected function writeError(OutputInterface $output, \Exception $error) |
||
200 | |||
201 | /** |
||
202 | * Autocompletes a question. |
||
203 | * |
||
204 | * @param OutputInterface $output |
||
205 | * @param Question $question |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | private function autocomplete(OutputInterface $output, Question $question, $inputStream) |
||
318 | |||
319 | /** |
||
320 | * Gets a hidden response from user. |
||
321 | * |
||
322 | * @param OutputInterface $output An Output instance |
||
323 | * |
||
324 | * @return string The answer |
||
325 | * |
||
326 | * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden |
||
327 | */ |
||
328 | private function getHiddenResponse(OutputInterface $output, $inputStream) |
||
378 | |||
379 | /** |
||
380 | * Validates an attempt. |
||
381 | * |
||
382 | * @param callable $interviewer A callable that will ask for a question and return the result |
||
383 | * @param OutputInterface $output An Output instance |
||
384 | * @param Question $question A Question instance |
||
385 | * |
||
386 | * @return string The validated response |
||
387 | * |
||
388 | * @throws \Exception In case the max number of attempts has been reached and no valid response has been given |
||
389 | */ |
||
390 | private function validateAttempts($interviewer, OutputInterface $output, Question $question) |
||
407 | |||
408 | /** |
||
409 | * Returns a valid unix shell. |
||
410 | * |
||
411 | * @return string|bool The valid shell name, false in case no valid shell is found |
||
412 | */ |
||
413 | private function getShell() |
||
434 | |||
435 | /** |
||
436 | * Reads user input. |
||
437 | * |
||
438 | * @param resource $stream The input stream |
||
439 | * |
||
440 | * @return string User input |
||
441 | * |
||
442 | * @throws RuntimeException |
||
443 | */ |
||
444 | private function readFromInput($stream) |
||
458 | |||
459 | /** |
||
460 | * Returns whether Stty is available or not. |
||
461 | * |
||
462 | * @return bool |
||
463 | */ |
||
464 | private function hasSttyAvailable() |
||
474 | } |
||
475 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.