Complex classes like FormatterOptions 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 FormatterOptions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class FormatterOptions |
||
32 | { |
||
33 | /** var array */ |
||
34 | protected $configurationData = []; |
||
35 | /** var array */ |
||
36 | protected $options = []; |
||
37 | /** var InputInterface */ |
||
38 | protected $input; |
||
39 | |||
40 | const FORMAT = 'format'; |
||
41 | const DEFAULT_FORMAT = 'default-format'; |
||
42 | const TABLE_STYLE = 'table-style'; |
||
43 | const LIST_ORIENTATION = 'list-orientation'; |
||
44 | const FIELDS = 'fields'; |
||
45 | const FIELD = 'field'; |
||
46 | const INCLUDE_FIELD_LABELS = 'include-field-labels'; |
||
47 | const ROW_LABELS = 'row-labels'; |
||
48 | const FIELD_LABELS = 'field-labels'; |
||
49 | const DEFAULT_FIELDS = 'default-fields'; |
||
50 | const DEFAULT_STRING_FIELD = 'default-string-field'; |
||
51 | const DELIMITER = 'delimiter'; |
||
52 | const LIST_DELIMITER = 'list-delimiter'; |
||
53 | const TERMINAL_WIDTH = 'width'; |
||
54 | const METADATA_TEMPLATE = 'metadata-template'; |
||
55 | |||
56 | /** |
||
57 | * Create a new FormatterOptions with the configuration data and the |
||
58 | * user-specified options for this request. |
||
59 | * |
||
60 | * @see FormatterOptions::setInput() |
||
61 | * @param array $configurationData |
||
62 | * @param array $options |
||
63 | */ |
||
64 | public function __construct($configurationData = [], $options = []) |
||
69 | |||
70 | /** |
||
71 | * Create a new FormatterOptions object with new configuration data (provided), |
||
72 | * and the same options data as this instance. |
||
73 | * |
||
74 | * @param array $configurationData |
||
75 | * @return FormatterOptions |
||
76 | */ |
||
77 | public function override($configurationData) |
||
85 | |||
86 | public function setTableStyle($style) |
||
90 | |||
91 | public function setDelimiter($delimiter) |
||
95 | |||
96 | public function setListDelimiter($listDelimiter) |
||
100 | |||
101 | public function setIncludeFieldLables($includFieldLables) |
||
105 | |||
106 | public function setListOrientation($listOrientation) |
||
110 | |||
111 | public function setRowLabels($rowLabels) |
||
115 | |||
116 | public function setDefaultFields($fields) |
||
120 | |||
121 | public function setFieldLabels($fieldLabels) |
||
125 | |||
126 | public function setDefaultStringField($defaultStringField) |
||
130 | |||
131 | public function setWidth($width) |
||
135 | |||
136 | /** |
||
137 | * Get a formatter option |
||
138 | * |
||
139 | * @param string $key |
||
140 | * @param array $defaults |
||
141 | * @param mixed $default |
||
142 | * @return mixed |
||
143 | */ |
||
144 | public function get($key, $defaults = [], $default = false) |
||
149 | |||
150 | /** |
||
151 | * Return the XmlSchema to use with --format=xml for data types that support |
||
152 | * that. This is used when an array needs to be converted into xml. |
||
153 | * |
||
154 | * @return XmlSchema |
||
155 | */ |
||
156 | public function getXmlSchema() |
||
160 | |||
161 | /** |
||
162 | * Determine the format that was requested by the caller. |
||
163 | * |
||
164 | * @param array $defaults |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getFormat($defaults = []) |
||
171 | |||
172 | /** |
||
173 | * Look up a key, and return its raw value. |
||
174 | * |
||
175 | * @param string $key |
||
176 | * @param array $defaults |
||
177 | * @param mixed $default |
||
178 | * @return mixed |
||
179 | */ |
||
180 | protected function fetch($key, $defaults = [], $default = false) |
||
186 | |||
187 | /** |
||
188 | * Reduce provided defaults to the single item identified by '$key', |
||
189 | * if it exists, or an empty array otherwise. |
||
190 | * |
||
191 | * @param string $key |
||
192 | * @param array $defaults |
||
193 | * @return array |
||
194 | */ |
||
195 | protected function defaultsForKey($key, $defaults, $default = false) |
||
202 | |||
203 | /** |
||
204 | * Look up all of the items associated with the provided defaults. |
||
205 | * |
||
206 | * @param array $defaults |
||
207 | * @return array |
||
208 | */ |
||
209 | protected function fetchRawValues($defaults = []) |
||
218 | |||
219 | /** |
||
220 | * Given the raw value for a specific key, do any type conversion |
||
221 | * (e.g. from a textual list to an array) needed for the data. |
||
222 | * |
||
223 | * @param string $key |
||
224 | * @param mixed $value |
||
225 | * @return mixed |
||
226 | */ |
||
227 | protected function parse($key, $value) |
||
235 | |||
236 | /** |
||
237 | * Convert from a textual list to an array |
||
238 | * |
||
239 | * @param string $value |
||
240 | * @return array |
||
241 | */ |
||
242 | public function parsePropertyList($value) |
||
246 | |||
247 | /** |
||
248 | * Given a specific key, return the class method name of the |
||
249 | * parsing method for data stored under this key. |
||
250 | * |
||
251 | * @param string $key |
||
252 | * @return string |
||
253 | */ |
||
254 | protected function getOptionFormat($key) |
||
265 | |||
266 | /** |
||
267 | * Change the configuration data for this formatter options object. |
||
268 | * |
||
269 | * @param array $configurationData |
||
270 | * @return FormatterOptions |
||
271 | */ |
||
272 | public function setConfigurationData($configurationData) |
||
277 | |||
278 | /** |
||
279 | * Change one configuration value for this formatter option. |
||
280 | * |
||
281 | * @param string $key |
||
282 | * @param mixed $value |
||
283 | * @return FormetterOptions |
||
284 | */ |
||
285 | protected function setConfigurationValue($key, $value) |
||
290 | |||
291 | /** |
||
292 | * Change one configuration value for this formatter option, but only |
||
293 | * if it does not already have a value set. |
||
294 | * |
||
295 | * @param string $key |
||
296 | * @param mixed $value |
||
297 | * @return FormetterOptions |
||
298 | */ |
||
299 | public function setConfigurationDefault($key, $value) |
||
306 | |||
307 | /** |
||
308 | * Return a reference to the configuration data for this object. |
||
309 | * |
||
310 | * @return array |
||
311 | */ |
||
312 | public function getConfigurationData() |
||
316 | |||
317 | /** |
||
318 | * Set all of the options that were specified by the user for this request. |
||
319 | * |
||
320 | * @param array $options |
||
321 | * @return FormatterOptions |
||
322 | */ |
||
323 | public function setOptions($options) |
||
328 | |||
329 | /** |
||
330 | * Remove an option. |
||
331 | */ |
||
332 | public function removeOption($key) |
||
336 | |||
337 | /** |
||
338 | * Change one option value specified by the user for this request. |
||
339 | * |
||
340 | * @param string $key |
||
341 | * @param mixed $value |
||
342 | * @return FormatterOptions |
||
343 | */ |
||
344 | public function setOption($key, $value) |
||
349 | |||
350 | /** |
||
351 | * Return a reference to the user-specified options for this request. |
||
352 | * |
||
353 | * @return array |
||
354 | */ |
||
355 | public function getOptions() |
||
359 | |||
360 | /** |
||
361 | * Provide a Symfony Console InputInterface containing the user-specified |
||
362 | * options for this request. |
||
363 | * |
||
364 | * @param InputInterface $input |
||
365 | * @return type |
||
366 | */ |
||
367 | public function setInput(InputInterface $input) |
||
371 | |||
372 | /** |
||
373 | * Return all of the options from the provided $defaults array that |
||
374 | * exist in our InputInterface object. |
||
375 | * |
||
376 | * @param array $defaults |
||
377 | * @return array |
||
378 | */ |
||
379 | public function getInputOptions($defaults) |
||
395 | |||
396 | public function shouldAppendNewline(OutputInterface $output) |
||
400 | |||
401 | protected function defaultAppendEol(OutputInterface $output) |
||
424 | |||
425 | /** |
||
426 | * Check to see if we are in interactive mode. If we were never |
||
427 | * given a reference to the input object, then assume not. |
||
428 | */ |
||
429 | public function isInteractive() |
||
436 | } |
||
437 |