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 | protected $formatters = []; |
|
25 | 28 | protected $arraySimplifiers = []; |
|
26 | 28 | ||
27 | 28 | public function __construct() |
|
50 | 27 | ||
51 | /** |
||
52 | * Add a formatter |
||
53 | * |
||
54 | 27 | * @param string $key the identifier of the formatter to add |
|
55 | 27 | * @param string $formatterClassname the class name of the formatter to add |
|
56 | 4 | * @return FormatterManager |
|
57 | */ |
||
58 | public function addFormatter($key, $formatterClassname) |
||
63 | 27 | ||
64 | /** |
||
65 | * Add a simplifier |
||
66 | * |
||
67 | 24 | * @param SimplifyToArrayInterface $simplifier the array simplifier to add |
|
68 | * @return FormatterManager |
||
69 | 24 | */ |
|
70 | public function addSimplifier(SimplifyToArrayInterface $simplifier) |
||
75 | |||
76 | /** |
||
77 | * Return a set of InputOption based on the annotations of a command. |
||
78 | * @param FormatterOptions $options |
||
79 | 28 | * @return InputOption[] |
|
80 | */ |
||
81 | 28 | public function automaticOptions(FormatterOptions $options, $dataType) |
|
113 | |||
114 | /** |
||
115 | * Given a list of available fields, return a list of field descriptions. |
||
116 | * @return string[] |
||
117 | */ |
||
118 | protected function availableFieldsList($availableFields) |
||
127 | 16 | ||
128 | /** |
||
129 | * Return the identifiers for all valid data types that have been registered. |
||
130 | * |
||
131 | * @param mixed $dataType \ReflectionObject or other description of the produced data type |
||
132 | 15 | * @return array |
|
133 | 4 | */ |
|
134 | public function validFormats($dataType) |
||
146 | |||
147 | 27 | public function isValidFormat(FormatterInterface $formatter, $dataType) |
|
160 | |||
161 | public function isValidDataType(FormatterInterface $formatter, \ReflectionClass $dataType) |
||
175 | |||
176 | /** |
||
177 | * Format and write output |
||
178 | * |
||
179 | * @param OutputInterface $output Output stream to write to |
||
180 | * @param string $format Data format to output in |
||
181 | * @param mixed $structuredOutput Data to output |
||
182 | * @param FormatterOptions $options Formatting options |
||
183 | */ |
||
184 | public function write(OutputInterface $output, $format, $structuredOutput, FormatterOptions $options) |
||
196 | |||
197 | protected function validateAndRestructure(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
218 | |||
219 | /** |
||
220 | * Fetch the requested formatter. |
||
221 | * |
||
222 | * @param string $format Identifier for requested formatter |
||
223 | * @return FormatterInterface |
||
224 | */ |
||
225 | public function getFormatter($format) |
||
233 | |||
234 | /** |
||
235 | * Test to see if the stipulated format exists |
||
236 | */ |
||
237 | public function hasFormatter($format) |
||
241 | |||
242 | /** |
||
243 | * Render the data as necessary (e.g. to select or reorder fields). |
||
244 | * |
||
245 | * @param FormatterInterface $formatter |
||
246 | * @param mixed $originalData |
||
247 | * @param mixed $restructuredData |
||
248 | * @param FormatterOptions $options Formatting options |
||
249 | * @return mixed |
||
250 | */ |
||
251 | public function renderData(FormatterInterface $formatter, $originalData, $restructuredData, FormatterOptions $options) |
||
258 | |||
259 | /** |
||
260 | * Determine if the provided data is compatible with the formatter being used. |
||
261 | * |
||
262 | * @param FormatterInterface $formatter Formatter being used |
||
263 | * @param mixed $structuredOutput Data to validate |
||
264 | * @return mixed |
||
265 | */ |
||
266 | public function validateData(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
289 | |||
290 | protected function simplifyToArray($structuredOutput, FormatterOptions $options) |
||
310 | |||
311 | protected function canSimplifyToArray(\ReflectionClass $structuredOutput) |
||
320 | |||
321 | /** |
||
322 | * Restructure the data as necessary (e.g. to select or reorder fields). |
||
323 | * |
||
324 | * @param mixed $structuredOutput |
||
325 | * @param FormatterOptions $options |
||
326 | * @return mixed |
||
327 | */ |
||
328 | public function restructureData($structuredOutput, FormatterOptions $options) |
||
335 | |||
336 | /** |
||
337 | * Allow the formatter access to the raw structured data prior |
||
338 | * to restructuring. For example, the 'list' formatter may wish |
||
339 | * to display the row keys when provided table output. If this |
||
340 | * function returns a result that does not evaluate to 'false', |
||
341 | * then that result will be used as-is, and restructuring and |
||
342 | * validation will not occur. |
||
343 | * |
||
344 | * @param mixed $structuredOutput |
||
345 | * @param FormatterOptions $options |
||
346 | * @return mixed |
||
347 | */ |
||
348 | public function overrideRestructure(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
354 | |||
355 | /** |
||
356 | * Allow the formatter to mess with the configuration options before any |
||
357 | * transformations et. al. get underway. |
||
358 | * @param FormatterInterface $formatter |
||
359 | * @param mixed $structuredOutput |
||
360 | * @param FormatterOptions $options |
||
361 | * @return FormatterOptions |
||
362 | */ |
||
363 | public function overrideOptions(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
370 | } |
||
371 |