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) |
||
117 | |||
118 | /** |
||
119 | * @param string|string[] $messages The message as an array of lines or a single string |
||
120 | * @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered |
||
121 | * the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL |
||
122 | * |
||
123 | * @return \string[] |
||
124 | */ |
||
125 | private function format($messages, $options = 0) |
||
153 | |||
154 | /** |
||
155 | * @param string|string[] $messages |
||
156 | * |
||
157 | * @return string[] |
||
158 | */ |
||
159 | private function splitNewLines($messages) |
||
170 | |||
171 | /** |
||
172 | * @return TerminalInterface |
||
173 | */ |
||
174 | public function getTerminal() |
||
178 | |||
179 | /** |
||
180 | * @param array $diff |
||
181 | * @param bool $newline |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | private function buildOutput(array $diff, $newline = false) |
||
215 | |||
216 | /** |
||
217 | * Writes a message to the output and adds a newline at the end. |
||
218 | * |
||
219 | * @param string|array $messages The message as an array of lines of a single string |
||
220 | * @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered |
||
221 | * the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL |
||
222 | */ |
||
223 | public function writeln($messages, $options = 0) |
||
227 | |||
228 | /** |
||
229 | * Sets the verbosity of the output. |
||
230 | * |
||
231 | * @param int $level The level of verbosity (one of the VERBOSITY constants) |
||
232 | */ |
||
233 | public function setVerbosity($level) |
||
237 | |||
238 | /** |
||
239 | * Gets the current verbosity of the output. |
||
240 | * |
||
241 | * @return int The current level of verbosity (one of the VERBOSITY constants) |
||
242 | */ |
||
243 | public function getVerbosity() |
||
247 | |||
248 | /** |
||
249 | * Returns whether verbosity is quiet (-q). |
||
250 | * |
||
251 | * @return bool true if verbosity is set to VERBOSITY_QUIET, false otherwise |
||
252 | */ |
||
253 | public function isQuiet() |
||
257 | |||
258 | /** |
||
259 | * Returns whether verbosity is verbose (-v). |
||
260 | * |
||
261 | * @return bool true if verbosity is set to VERBOSITY_VERBOSE, false otherwise |
||
262 | */ |
||
263 | public function isVerbose() |
||
267 | |||
268 | /** |
||
269 | * Returns whether verbosity is very verbose (-vv). |
||
270 | * |
||
271 | * @return bool true if verbosity is set to VERBOSITY_VERY_VERBOSE, false otherwise |
||
272 | */ |
||
273 | public function isVeryVerbose() |
||
277 | |||
278 | /** |
||
279 | * Returns whether verbosity is debug (-vvv). |
||
280 | * |
||
281 | * @return bool true if verbosity is set to VERBOSITY_DEBUG, false otherwise |
||
282 | */ |
||
283 | public function isDebug() |
||
287 | |||
288 | /** |
||
289 | * Sets the decorated flag. |
||
290 | * |
||
291 | * @param bool $decorated Whether to decorate the messages |
||
292 | */ |
||
293 | public function setDecorated($decorated) |
||
297 | |||
298 | /** |
||
299 | * Gets the decorated flag. |
||
300 | * |
||
301 | * @return bool true if the output will decorate messages, false otherwise |
||
302 | */ |
||
303 | public function isDecorated() |
||
307 | |||
308 | /** |
||
309 | * Sets output formatter. |
||
310 | * |
||
311 | * @param OutputFormatterInterface $formatter |
||
312 | */ |
||
313 | public function setFormatter(OutputFormatterInterface $formatter) |
||
317 | |||
318 | /** |
||
319 | * Returns current output formatter instance. |
||
320 | * |
||
321 | * @return OutputFormatterInterface |
||
322 | */ |
||
323 | public function getFormatter() |
||
327 | |||
328 | /** |
||
329 | * @return bool |
||
330 | */ |
||
331 | public function isTrim() |
||
335 | |||
336 | /** |
||
337 | * Should we wrap the input or not, if this is set to false, it will trim each line |
||
338 | * |
||
339 | * @param bool $trim |
||
340 | * |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function setTrim($trim) |
||
348 | } |
||
349 |