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) |
|
| 157 | |||
| 158 | public function isValidDataType(FormatterInterface $formatter, \ReflectionClass $dataType) |
||
| 172 | |||
| 173 | 26 | /** |
|
| 174 | * Format and write output |
||
| 175 | * |
||
| 176 | * @param OutputInterface $output Output stream to write to |
||
| 177 | * @param string $format Data format to output in |
||
| 178 | * @param mixed $structuredOutput Data to output |
||
| 179 | * @param FormatterOptions $options Formatting options |
||
| 180 | */ |
||
| 181 | public function write(OutputInterface $output, $format, $structuredOutput, FormatterOptions $options) |
||
| 193 | |||
| 194 | protected function validateAndRestructure(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Fetch the requested formatter. |
||
| 218 | * |
||
| 219 | * @param string $format Identifier for requested formatter |
||
| 220 | * @return FormatterInterface |
||
| 221 | */ |
||
| 222 | public function getFormatter($format) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Test to see if the stipulated format exists |
||
| 233 | */ |
||
| 234 | public function hasFormatter($format) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Render the data as necessary (e.g. to select or reorder fields). |
||
| 241 | * |
||
| 242 | * @param FormatterInterface $formatter |
||
| 243 | * @param mixed $originalData |
||
| 244 | * @param mixed $restructuredData |
||
| 245 | * @param FormatterOptions $options Formatting options |
||
| 246 | * @return mixed |
||
| 247 | */ |
||
| 248 | public function renderData(FormatterInterface $formatter, $originalData, $restructuredData, FormatterOptions $options) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Determine if the provided data is compatible with the formatter being used. |
||
| 258 | * |
||
| 259 | * @param FormatterInterface $formatter Formatter being used |
||
| 260 | * @param mixed $structuredOutput Data to validate |
||
| 261 | * @return mixed |
||
| 262 | */ |
||
| 263 | public function validateData(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
| 286 | |||
| 287 | protected function simplifyToArray($structuredOutput, FormatterOptions $options) |
||
| 307 | |||
| 308 | protected function canSimplifyToArray(\ReflectionClass $structuredOutput) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Restructure the data as necessary (e.g. to select or reorder fields). |
||
| 320 | * |
||
| 321 | * @param mixed $structuredOutput |
||
| 322 | * @param FormatterOptions $options |
||
| 323 | * @return mixed |
||
| 324 | */ |
||
| 325 | public function restructureData($structuredOutput, FormatterOptions $options) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Allow the formatter access to the raw structured data prior |
||
| 335 | * to restructuring. For example, the 'list' formatter may wish |
||
| 336 | * to display the row keys when provided table output. If this |
||
| 337 | * function returns a result that does not evaluate to 'false', |
||
| 338 | * then that result will be used as-is, and restructuring and |
||
| 339 | * validation will not occur. |
||
| 340 | * |
||
| 341 | * @param mixed $structuredOutput |
||
| 342 | * @param FormatterOptions $options |
||
| 343 | * @return mixed |
||
| 344 | */ |
||
| 345 | public function overrideRestructure(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Allow the formatter to mess with the configuration options before any |
||
| 354 | * transformations et. al. get underway. |
||
| 355 | * @param FormatterInterface $formatter |
||
| 356 | * @param mixed $structuredOutput |
||
| 357 | * @param FormatterOptions $options |
||
| 358 | * @return FormatterOptions |
||
| 359 | */ |
||
| 360 | public function overrideOptions(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
| 367 | } |
||
| 368 |