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 |
||
28 | class FormatterOptions |
||
29 | { |
||
30 | /** var array */ |
||
31 | protected $configurationData = []; |
||
32 | /** var array */ |
||
33 | protected $options = []; |
||
34 | /** var InputInterface */ |
||
35 | protected $input; |
||
36 | |||
37 | const FORMAT = 'format'; |
||
38 | const DEFAULT_FORMAT = 'default-format'; |
||
39 | const TABLE_STYLE = 'table-style'; |
||
40 | const LIST_ORIENTATION = 'list-orientation'; |
||
41 | const FIELDS = 'fields'; |
||
42 | const FIELD = 'field'; |
||
43 | const INCLUDE_FIELD_LABELS = 'include-field-labels'; |
||
44 | const ROW_LABELS = 'row-labels'; |
||
45 | const FIELD_LABELS = 'field-labels'; |
||
46 | const DEFAULT_FIELDS = 'default-fields'; |
||
47 | const DEFAULT_TABLE_FIELDS = 'default-table-fields'; |
||
48 | const DEFAULT_STRING_FIELD = 'default-string-field'; |
||
49 | const DELIMITER = 'delimiter'; |
||
50 | const CSV_ENCLOSURE = 'csv-enclosure'; |
||
51 | const CSV_ESCAPE_CHAR = 'csv-escape-char'; |
||
52 | const LIST_DELIMITER = 'list-delimiter'; |
||
53 | const TERMINAL_WIDTH = 'width'; |
||
54 | const METADATA_TEMPLATE = 'metadata-template'; |
||
55 | const HUMAN_READABLE = 'human-readable'; |
||
56 | |||
57 | /** |
||
58 | * Create a new FormatterOptions with the configuration data and the |
||
59 | * user-specified options for this request. |
||
60 | * |
||
61 | * @see FormatterOptions::setInput() |
||
62 | * @param array $configurationData |
||
63 | * @param array $options |
||
64 | */ |
||
65 | public function __construct($configurationData = [], $options = []) |
||
70 | |||
71 | /** |
||
72 | * Create a new FormatterOptions object with new configuration data (provided), |
||
73 | * and the same options data as this instance. |
||
74 | * |
||
75 | * @param array $configurationData |
||
76 | * @return FormatterOptions |
||
77 | */ |
||
78 | public function override($configurationData) |
||
86 | |||
87 | public function setTableStyle($style) |
||
91 | |||
92 | public function setDelimiter($delimiter) |
||
96 | |||
97 | public function setCsvEnclosure($enclosure) |
||
101 | |||
102 | public function setCsvEscapeChar($escapeChar) |
||
106 | |||
107 | public function setListDelimiter($listDelimiter) |
||
111 | |||
112 | |||
113 | |||
114 | public function setIncludeFieldLables($includFieldLables) |
||
118 | |||
119 | public function setListOrientation($listOrientation) |
||
123 | |||
124 | public function setRowLabels($rowLabels) |
||
128 | |||
129 | public function setDefaultFields($fields) |
||
133 | |||
134 | public function setFieldLabels($fieldLabels) |
||
138 | |||
139 | public function setDefaultStringField($defaultStringField) |
||
143 | |||
144 | public function setWidth($width) |
||
148 | |||
149 | public function setHumanReadable($isHumanReadable = true) |
||
153 | |||
154 | /** |
||
155 | * Get a formatter option |
||
156 | * |
||
157 | * @param string $key |
||
158 | * @param array $defaults |
||
159 | * @param mixed $default |
||
160 | * @return mixed |
||
161 | */ |
||
162 | public function get($key, $defaults = [], $default = false) |
||
167 | |||
168 | /** |
||
169 | * Return the XmlSchema to use with --format=xml for data types that support |
||
170 | * that. This is used when an array needs to be converted into xml. |
||
171 | * |
||
172 | * @return XmlSchema |
||
173 | */ |
||
174 | public function getXmlSchema() |
||
178 | |||
179 | /** |
||
180 | * Determine the format that was requested by the caller. |
||
181 | * |
||
182 | * @param array $defaults |
||
183 | * @return string |
||
184 | */ |
||
185 | public function getFormat($defaults = []) |
||
189 | |||
190 | /** |
||
191 | * Look up a key, and return its raw value. |
||
192 | * |
||
193 | * @param string $key |
||
194 | * @param array $defaults |
||
195 | * @param mixed $default |
||
196 | * @return mixed |
||
197 | */ |
||
198 | protected function fetch($key, $defaults = [], $default = false) |
||
204 | |||
205 | /** |
||
206 | * Reduce provided defaults to the single item identified by '$key', |
||
207 | * if it exists, or an empty array otherwise. |
||
208 | * |
||
209 | * @param string $key |
||
210 | * @param array $defaults |
||
211 | * @return array |
||
212 | */ |
||
213 | protected function defaultsForKey($key, $defaults, $default = false) |
||
220 | |||
221 | /** |
||
222 | * Look up all of the items associated with the provided defaults. |
||
223 | * |
||
224 | * @param array $defaults |
||
225 | * @return array |
||
226 | */ |
||
227 | protected function fetchRawValues($defaults = []) |
||
236 | |||
237 | /** |
||
238 | * Given the raw value for a specific key, do any type conversion |
||
239 | * (e.g. from a textual list to an array) needed for the data. |
||
240 | * |
||
241 | * @param string $key |
||
242 | * @param mixed $value |
||
243 | * @return mixed |
||
244 | */ |
||
245 | protected function parse($key, $value) |
||
253 | |||
254 | /** |
||
255 | * Convert from a textual list to an array |
||
256 | * |
||
257 | * @param string $value |
||
258 | * @return array |
||
259 | */ |
||
260 | public function parsePropertyList($value) |
||
264 | |||
265 | /** |
||
266 | * Given a specific key, return the class method name of the |
||
267 | * parsing method for data stored under this key. |
||
268 | * |
||
269 | * @param string $key |
||
270 | * @return string |
||
271 | */ |
||
272 | protected function getOptionFormat($key) |
||
283 | |||
284 | /** |
||
285 | * Change the configuration data for this formatter options object. |
||
286 | * |
||
287 | * @param array $configurationData |
||
288 | * @return FormatterOptions |
||
289 | */ |
||
290 | public function setConfigurationData($configurationData) |
||
295 | |||
296 | /** |
||
297 | * Change one configuration value for this formatter option. |
||
298 | * |
||
299 | * @param string $key |
||
300 | * @param mixed $value |
||
301 | * @return FormetterOptions |
||
302 | */ |
||
303 | protected function setConfigurationValue($key, $value) |
||
308 | |||
309 | /** |
||
310 | * Change one configuration value for this formatter option, but only |
||
311 | * if it does not already have a value set. |
||
312 | * |
||
313 | * @param string $key |
||
314 | * @param mixed $value |
||
315 | * @return FormetterOptions |
||
316 | */ |
||
317 | public function setConfigurationDefault($key, $value) |
||
324 | |||
325 | /** |
||
326 | * Return a reference to the configuration data for this object. |
||
327 | * |
||
328 | * @return array |
||
329 | */ |
||
330 | public function getConfigurationData() |
||
334 | |||
335 | /** |
||
336 | * Set all of the options that were specified by the user for this request. |
||
337 | * |
||
338 | * @param array $options |
||
339 | * @return FormatterOptions |
||
340 | */ |
||
341 | public function setOptions($options) |
||
346 | |||
347 | /** |
||
348 | * Change one option value specified by the user for this request. |
||
349 | * |
||
350 | * @param string $key |
||
351 | * @param mixed $value |
||
352 | * @return FormatterOptions |
||
353 | */ |
||
354 | public function setOption($key, $value) |
||
359 | |||
360 | /** |
||
361 | * Return a reference to the user-specified options for this request. |
||
362 | * |
||
363 | * @return array |
||
364 | */ |
||
365 | public function getOptions() |
||
369 | |||
370 | /** |
||
371 | * Provide a Symfony Console InputInterface containing the user-specified |
||
372 | * options for this request. |
||
373 | * |
||
374 | * @param InputInterface $input |
||
375 | * @return type |
||
376 | */ |
||
377 | public function setInput(InputInterface $input) |
||
381 | |||
382 | /** |
||
383 | * Return all of the options from the provided $defaults array that |
||
384 | * exist in our InputInterface object. |
||
385 | * |
||
386 | * @param array $defaults |
||
387 | * @return array |
||
388 | */ |
||
389 | public function getInputOptions($defaults) |
||
405 | } |
||
406 |