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 |
||
| 16 | 28 | class FormatterManager |
|
| 17 | { |
||
| 18 | 28 | protected $formatters = []; |
|
| 19 | 28 | protected $arraySimplifiers = []; |
|
| 20 | 28 | ||
| 21 | 28 | public function __construct() |
|
| 44 | |||
| 45 | 28 | /** |
|
| 46 | 27 | * Add a formatter |
|
| 47 | 24 | * |
|
| 48 | 24 | * @param string $key the identifier of the formatter to add |
|
| 49 | * @param string $formatterClassname the class name of the formatter to add |
||
| 50 | 27 | * @return FormatterManager |
|
| 51 | */ |
||
| 52 | public function addFormatter($key, $formatterClassname) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Add a simplifier |
||
| 60 | 27 | * |
|
| 61 | * @param SimplifyToArrayInterface $simplifier the array simplifier to add |
||
| 62 | * @return FormatterManager |
||
| 63 | 27 | */ |
|
| 64 | public function addSimplifier(SimplifyToArrayInterface $simplifier) |
||
| 69 | 24 | ||
| 70 | /** |
||
| 71 | * Return a set of InputOption based on the annotations of a command. |
||
| 72 | * @param FormatterOptions $options |
||
| 73 | * @return InputOption[] |
||
| 74 | */ |
||
| 75 | public function automaticOptions(FormatterOptions $options, $dataType) |
||
| 107 | 24 | ||
| 108 | /** |
||
| 109 | 24 | * Given a list of available fields, return a list of field descriptions. |
|
| 110 | 14 | * @return string[] |
|
| 111 | */ |
||
| 112 | 14 | protected function availableFieldsList($availableFields) |
|
| 121 | |||
| 122 | 27 | /** |
|
| 123 | * Return the identifiers for all valid data types that have been registered. |
||
| 124 | * |
||
| 125 | * @param mixed $dataType \ReflectionObject or other description of the produced data type |
||
| 126 | 27 | * @return array |
|
| 127 | 16 | */ |
|
| 128 | public function validFormats($dataType) |
||
| 140 | |||
| 141 | public function isValidFormat(FormatterInterface $formatter, $dataType) |
||
| 151 | |||
| 152 | 20 | public function isValidDataType(FormatterInterface $formatter, \ReflectionClass $dataType) |
|
| 166 | |||
| 167 | /** |
||
| 168 | 27 | * Format and write output |
|
| 169 | * |
||
| 170 | 27 | * @param OutputInterface $output Output stream to write to |
|
| 171 | 5 | * @param string $format Data format to output in |
|
| 172 | * @param mixed $structuredOutput Data to output |
||
| 173 | 26 | * @param FormatterOptions $options Formatting options |
|
| 174 | */ |
||
| 175 | public function write(OutputInterface $output, $format, $structuredOutput, FormatterOptions $options) |
||
| 187 | |||
| 188 | protected function validateAndRestructure(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Fetch the requested formatter. |
||
| 212 | * |
||
| 213 | * @param string $format Identifier for requested formatter |
||
| 214 | * @return FormatterInterface |
||
| 215 | */ |
||
| 216 | public function getFormatter($format) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Test to see if the stipulated format exists |
||
| 227 | */ |
||
| 228 | public function hasFormatter($format) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Render the data as necessary (e.g. to select or reorder fields). |
||
| 235 | * |
||
| 236 | * @param FormatterInterface $formatter |
||
| 237 | * @param mixed $originalData |
||
| 238 | * @param mixed $restructuredData |
||
| 239 | * @param FormatterOptions $options Formatting options |
||
| 240 | * @return mixed |
||
| 241 | */ |
||
| 242 | public function renderData(FormatterInterface $formatter, $originalData, $restructuredData, FormatterOptions $options) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Determine if the provided data is compatible with the formatter being used. |
||
| 252 | * |
||
| 253 | * @param FormatterInterface $formatter Formatter being used |
||
| 254 | * @param mixed $structuredOutput Data to validate |
||
| 255 | * @return mixed |
||
| 256 | */ |
||
| 257 | public function validateData(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
| 280 | |||
| 281 | protected function simplifyToArray($structuredOutput, FormatterOptions $options) |
||
| 301 | |||
| 302 | protected function canSimplifyToArray(\ReflectionClass $structuredOutput) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Restructure the data as necessary (e.g. to select or reorder fields). |
||
| 314 | * |
||
| 315 | * @param mixed $structuredOutput |
||
| 316 | * @param FormatterOptions $options |
||
| 317 | * @return mixed |
||
| 318 | */ |
||
| 319 | public function restructureData($structuredOutput, FormatterOptions $options) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Allow the formatter access to the raw structured data prior |
||
| 329 | * to restructuring. For example, the 'list' formatter may wish |
||
| 330 | * to display the row keys when provided table output. If this |
||
| 331 | * function returns a result that does not evaluate to 'false', |
||
| 332 | * then that result will be used as-is, and restructuring and |
||
| 333 | * validation will not occur. |
||
| 334 | * |
||
| 335 | * @param mixed $structuredOutput |
||
| 336 | * @param FormatterOptions $options |
||
| 337 | * @return mixed |
||
| 338 | */ |
||
| 339 | public function overrideRestructure(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Allow the formatter to mess with the configuration options before any |
||
| 348 | * transformations et. al. get underway. |
||
| 349 | * @param FormatterInterface $formatter |
||
| 350 | * @param mixed $structuredOutput |
||
| 351 | * @param FormatterOptions $options |
||
| 352 | * @return FormatterOptions |
||
| 353 | */ |
||
| 354 | public function overrideOptions(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
| 361 | } |
||
| 362 |