Complex classes like FormatterManager 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 FormatterManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | 28 | class FormatterManager |
|
23 | 28 | { |
|
24 | 28 | /** var FormatterInterface[] */ |
|
25 | 28 | protected $formatters = []; |
|
26 | 28 | /** var SimplifyToArrayInterface[] */ |
|
27 | 28 | protected $arraySimplifiers = []; |
|
28 | |||
29 | public function __construct() |
||
32 | 28 | ||
33 | public function addDefaultFormatters() |
||
55 | 27 | ||
56 | 4 | public function addDefaultSimplifiers() |
|
61 | |||
62 | /** |
||
63 | 27 | * Add a formatter |
|
64 | * |
||
65 | * @param string $key the identifier of the formatter to add |
||
66 | * @param string $formatter the class name of the formatter to add |
||
67 | 24 | * @return FormatterManager |
|
68 | */ |
||
69 | 24 | public function addFormatter($key, FormatterInterface $formatter) |
|
74 | |||
75 | /** |
||
76 | * Add a simplifier |
||
77 | * |
||
78 | * @param SimplifyToArrayInterface $simplifier the array simplifier to add |
||
79 | 28 | * @return FormatterManager |
|
80 | */ |
||
81 | 28 | public function addSimplifier(SimplifyToArrayInterface $simplifier) |
|
86 | 27 | ||
87 | 9 | /** |
|
88 | 9 | * Return a set of InputOption based on the annotations of a command. |
|
89 | 27 | * @param FormatterOptions $options |
|
90 | * @return InputOption[] |
||
91 | */ |
||
92 | 28 | public function automaticOptions(FormatterOptions $options, $dataType) |
|
126 | 27 | ||
127 | 16 | /** |
|
128 | * Given a list of available fields, return a list of field descriptions. |
||
129 | * @return string[] |
||
130 | */ |
||
131 | protected function availableFieldsList($availableFields) |
||
140 | |||
141 | /** |
||
142 | * Return the identifiers for all valid data types that have been registered. |
||
143 | * |
||
144 | * @param mixed $dataType \ReflectionObject or other description of the produced data type |
||
145 | * @return array |
||
146 | */ |
||
147 | 27 | public function validFormats($dataType) |
|
159 | |||
160 | public function isValidFormat(FormatterInterface $formatter, $dataType) |
||
173 | 26 | ||
174 | public function isValidDataType(FormatterInterface $formatter, \ReflectionClass $dataType) |
||
188 | |||
189 | /** |
||
190 | * Format and write output |
||
191 | * |
||
192 | * @param OutputInterface $output Output stream to write to |
||
193 | * @param string $format Data format to output in |
||
194 | * @param mixed $structuredOutput Data to output |
||
195 | * @param FormatterOptions $options Formatting options |
||
196 | */ |
||
197 | public function write(OutputInterface $output, $format, $structuredOutput, FormatterOptions $options) |
||
209 | |||
210 | protected function validateAndRestructure(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
231 | |||
232 | /** |
||
233 | * Fetch the requested formatter. |
||
234 | * |
||
235 | * @param string $format Identifier for requested formatter |
||
236 | * @return FormatterInterface |
||
237 | */ |
||
238 | public function getFormatter($format) |
||
253 | |||
254 | /** |
||
255 | * Test to see if the stipulated format exists |
||
256 | */ |
||
257 | public function hasFormatter($format) |
||
261 | |||
262 | /** |
||
263 | * Render the data as necessary (e.g. to select or reorder fields). |
||
264 | * |
||
265 | * @param FormatterInterface $formatter |
||
266 | * @param mixed $originalData |
||
267 | * @param mixed $restructuredData |
||
268 | * @param FormatterOptions $options Formatting options |
||
269 | * @return mixed |
||
270 | */ |
||
271 | public function renderData(FormatterInterface $formatter, $originalData, $restructuredData, FormatterOptions $options) |
||
278 | |||
279 | /** |
||
280 | * Determine if the provided data is compatible with the formatter being used. |
||
281 | * |
||
282 | * @param FormatterInterface $formatter Formatter being used |
||
283 | * @param mixed $structuredOutput Data to validate |
||
284 | * @return mixed |
||
285 | */ |
||
286 | public function validateData(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
309 | |||
310 | protected function simplifyToArray($structuredOutput, FormatterOptions $options) |
||
330 | |||
331 | protected function canSimplifyToArray(\ReflectionClass $structuredOutput) |
||
340 | |||
341 | /** |
||
342 | * Restructure the data as necessary (e.g. to select or reorder fields). |
||
343 | * |
||
344 | * @param mixed $structuredOutput |
||
345 | * @param FormatterOptions $options |
||
346 | * @return mixed |
||
347 | */ |
||
348 | public function restructureData($structuredOutput, FormatterOptions $options) |
||
355 | |||
356 | /** |
||
357 | * Allow the formatter access to the raw structured data prior |
||
358 | * to restructuring. For example, the 'list' formatter may wish |
||
359 | * to display the row keys when provided table output. If this |
||
360 | * function returns a result that does not evaluate to 'false', |
||
361 | * then that result will be used as-is, and restructuring and |
||
362 | * validation will not occur. |
||
363 | * |
||
364 | * @param mixed $structuredOutput |
||
365 | * @param FormatterOptions $options |
||
366 | * @return mixed |
||
367 | */ |
||
368 | public function overrideRestructure(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
374 | |||
375 | /** |
||
376 | * Allow the formatter to mess with the configuration options before any |
||
377 | * transformations et. al. get underway. |
||
378 | * @param FormatterInterface $formatter |
||
379 | * @param mixed $structuredOutput |
||
380 | * @param FormatterOptions $options |
||
381 | * @return FormatterOptions |
||
382 | */ |
||
383 | public function overrideOptions(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
390 | } |
||
391 |