Complex classes like DiffConsoleOutput 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 DiffConsoleOutput, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class DiffConsoleOutput implements OutputInterface |
||
29 | { |
||
30 | /** @var string[] */ |
||
31 | private $buffer = []; |
||
32 | /** @var ConsoleDiff */ |
||
33 | private $diff; |
||
34 | /** @var TerminalInterface */ |
||
35 | private $terminal; |
||
36 | /** @var OutputInterface */ |
||
37 | private $output; |
||
38 | /** @var Wrapper */ |
||
39 | private $wrapper; |
||
40 | /** @var bool */ |
||
41 | private $trim = false; |
||
42 | |||
43 | /** |
||
44 | * Constructor. |
||
45 | * |
||
46 | * @param OutputInterface $output |
||
47 | * @param TerminalInterface $terminal |
||
48 | * @param Wrapper $wrapper |
||
49 | */ |
||
50 | public function __construct( |
||
60 | |||
61 | /** |
||
62 | * @param string|array $messages The message as an array of lines or a single string |
||
63 | * @param bool $newline Whether to add a newline |
||
64 | * @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered |
||
65 | * the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL |
||
66 | */ |
||
67 | public function write($messages, $newline = false, $options = 0) |
||
72 | |||
73 | /** |
||
74 | * @param string|string[] $messages The message as an array of lines or a single string |
||
75 | * @param bool $newline Whether to add a newline |
||
76 | * @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered |
||
77 | * the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL |
||
78 | */ |
||
79 | public function reWrite($messages, $newline = false, $options = 0) |
||
109 | |||
110 | /** |
||
111 | * @param string|string[] $messages The message as an array of lines or a single string |
||
112 | * @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered |
||
113 | * the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL |
||
114 | * |
||
115 | * @return \string[] |
||
116 | */ |
||
117 | private function format($messages, $options = 0) |
||
145 | |||
146 | /** |
||
147 | * @param string|string[] $messages |
||
148 | * |
||
149 | * @return string[] |
||
150 | */ |
||
151 | private function splitNewLines($messages) |
||
162 | |||
163 | /** |
||
164 | * @return TerminalInterface |
||
165 | */ |
||
166 | public function getTerminal() |
||
170 | |||
171 | /** |
||
172 | * @param array $diff |
||
173 | * @param bool $newline |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | private function buildOutput(array $diff, $newline = false) |
||
207 | |||
208 | /** |
||
209 | * Writes a message to the output and adds a newline at the end. |
||
210 | * |
||
211 | * @param string|array $messages The message as an array of lines of a single string |
||
212 | * @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered |
||
213 | * the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL |
||
214 | */ |
||
215 | public function writeln($messages, $options = 0) |
||
219 | |||
220 | /** |
||
221 | * Sets the verbosity of the output. |
||
222 | * |
||
223 | * @param int $level The level of verbosity (one of the VERBOSITY constants) |
||
224 | */ |
||
225 | public function setVerbosity($level) |
||
229 | |||
230 | /** |
||
231 | * Gets the current verbosity of the output. |
||
232 | * |
||
233 | * @return int The current level of verbosity (one of the VERBOSITY constants) |
||
234 | */ |
||
235 | public function getVerbosity() |
||
239 | |||
240 | /** |
||
241 | * Returns whether verbosity is quiet (-q). |
||
242 | * |
||
243 | * @return bool true if verbosity is set to VERBOSITY_QUIET, false otherwise |
||
244 | */ |
||
245 | public function isQuiet() |
||
249 | |||
250 | /** |
||
251 | * Returns whether verbosity is verbose (-v). |
||
252 | * |
||
253 | * @return bool true if verbosity is set to VERBOSITY_VERBOSE, false otherwise |
||
254 | */ |
||
255 | public function isVerbose() |
||
259 | |||
260 | /** |
||
261 | * Returns whether verbosity is very verbose (-vv). |
||
262 | * |
||
263 | * @return bool true if verbosity is set to VERBOSITY_VERY_VERBOSE, false otherwise |
||
264 | */ |
||
265 | public function isVeryVerbose() |
||
269 | |||
270 | /** |
||
271 | * Returns whether verbosity is debug (-vvv). |
||
272 | * |
||
273 | * @return bool true if verbosity is set to VERBOSITY_DEBUG, false otherwise |
||
274 | */ |
||
275 | public function isDebug() |
||
279 | |||
280 | /** |
||
281 | * Sets the decorated flag. |
||
282 | * |
||
283 | * @param bool $decorated Whether to decorate the messages |
||
284 | */ |
||
285 | public function setDecorated($decorated) |
||
289 | |||
290 | /** |
||
291 | * Gets the decorated flag. |
||
292 | * |
||
293 | * @return bool true if the output will decorate messages, false otherwise |
||
294 | */ |
||
295 | public function isDecorated() |
||
299 | |||
300 | /** |
||
301 | * Sets output formatter. |
||
302 | * |
||
303 | * @param OutputFormatterInterface $formatter |
||
304 | */ |
||
305 | public function setFormatter(OutputFormatterInterface $formatter) |
||
309 | |||
310 | /** |
||
311 | * Returns current output formatter instance. |
||
312 | * |
||
313 | * @return OutputFormatterInterface |
||
314 | */ |
||
315 | public function getFormatter() |
||
319 | |||
320 | /** |
||
321 | * @return bool |
||
322 | */ |
||
323 | public function isTrim() |
||
327 | |||
328 | /** |
||
329 | * Should we wrap the input or not, if this is set to false, it will trim each line |
||
330 | * |
||
331 | * @param bool $trim |
||
332 | * |
||
333 | * @return $this |
||
334 | */ |
||
335 | public function setTrim($trim) |
||
340 | } |
||
341 |