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_STRING_FIELD = 'default-string-field'; |
||
48 | const DELIMITER = 'delimiter'; |
||
49 | const CSV_ENCLOSURE = 'csv-enclosure'; |
||
50 | const CSV_ESCAPE_CHAR = 'csv-escape-char'; |
||
51 | const LIST_DELIMITER = 'list-delimiter'; |
||
52 | const TERMINAL_WIDTH = 'width'; |
||
53 | const METADATA_TEMPLATE = 'metadata-template'; |
||
54 | |||
55 | /** |
||
56 | * Create a new FormatterOptions with the configuration data and the |
||
57 | * user-specified options for this request. |
||
58 | * |
||
59 | * @see FormatterOptions::setInput() |
||
60 | * @param array $configurationData |
||
61 | * @param array $options |
||
62 | */ |
||
63 | public function __construct($configurationData = [], $options = []) |
||
68 | |||
69 | /** |
||
70 | * Create a new FormatterOptions object with new configuration data (provided), |
||
71 | * and the same options data as this instance. |
||
72 | * |
||
73 | * @param array $configurationData |
||
74 | * @return FormatterOptions |
||
75 | */ |
||
76 | public function override($configurationData) |
||
84 | |||
85 | public function setTableStyle($style) |
||
89 | |||
90 | public function setDelimiter($delimiter) |
||
94 | |||
95 | public function setCsvEnclosure($enclosure) |
||
99 | |||
100 | public function setCsvEscapeChar($escapeChar) |
||
104 | |||
105 | public function setListDelimiter($listDelimiter) |
||
109 | |||
110 | |||
111 | |||
112 | public function setIncludeFieldLables($includFieldLables) |
||
116 | |||
117 | public function setListOrientation($listOrientation) |
||
121 | |||
122 | public function setRowLabels($rowLabels) |
||
126 | |||
127 | public function setDefaultFields($fields) |
||
131 | |||
132 | public function setFieldLabels($fieldLabels) |
||
136 | |||
137 | public function setDefaultStringField($defaultStringField) |
||
141 | |||
142 | public function setWidth($width) |
||
146 | |||
147 | /** |
||
148 | * Get a formatter option |
||
149 | * |
||
150 | * @param string $key |
||
151 | * @param array $defaults |
||
152 | * @param mixed $default |
||
153 | * @return mixed |
||
154 | */ |
||
155 | public function get($key, $defaults = [], $default = false) |
||
160 | |||
161 | /** |
||
162 | * Return the XmlSchema to use with --format=xml for data types that support |
||
163 | * that. This is used when an array needs to be converted into xml. |
||
164 | * |
||
165 | * @return XmlSchema |
||
166 | */ |
||
167 | public function getXmlSchema() |
||
171 | |||
172 | /** |
||
173 | * Determine the format that was requested by the caller. |
||
174 | * |
||
175 | * @param array $defaults |
||
176 | * @return string |
||
177 | */ |
||
178 | public function getFormat($defaults = []) |
||
182 | |||
183 | /** |
||
184 | * Look up a key, and return its raw value. |
||
185 | * |
||
186 | * @param string $key |
||
187 | * @param array $defaults |
||
188 | * @param mixed $default |
||
189 | * @return mixed |
||
190 | */ |
||
191 | protected function fetch($key, $defaults = [], $default = false) |
||
197 | |||
198 | /** |
||
199 | * Reduce provided defaults to the single item identified by '$key', |
||
200 | * if it exists, or an empty array otherwise. |
||
201 | * |
||
202 | * @param string $key |
||
203 | * @param array $defaults |
||
204 | * @return array |
||
205 | */ |
||
206 | protected function defaultsForKey($key, $defaults, $default = false) |
||
213 | |||
214 | /** |
||
215 | * Look up all of the items associated with the provided defaults. |
||
216 | * |
||
217 | * @param array $defaults |
||
218 | * @return array |
||
219 | */ |
||
220 | protected function fetchRawValues($defaults = []) |
||
229 | |||
230 | /** |
||
231 | * Given the raw value for a specific key, do any type conversion |
||
232 | * (e.g. from a textual list to an array) needed for the data. |
||
233 | * |
||
234 | * @param string $key |
||
235 | * @param mixed $value |
||
236 | * @return mixed |
||
237 | */ |
||
238 | protected function parse($key, $value) |
||
246 | |||
247 | /** |
||
248 | * Convert from a textual list to an array |
||
249 | * |
||
250 | * @param string $value |
||
251 | * @return array |
||
252 | */ |
||
253 | public function parsePropertyList($value) |
||
257 | |||
258 | /** |
||
259 | * Given a specific key, return the class method name of the |
||
260 | * parsing method for data stored under this key. |
||
261 | * |
||
262 | * @param string $key |
||
263 | * @return string |
||
264 | */ |
||
265 | protected function getOptionFormat($key) |
||
276 | |||
277 | /** |
||
278 | * Change the configuration data for this formatter options object. |
||
279 | * |
||
280 | * @param array $configurationData |
||
281 | * @return FormatterOptions |
||
282 | */ |
||
283 | public function setConfigurationData($configurationData) |
||
288 | |||
289 | /** |
||
290 | * Change one configuration value for this formatter option. |
||
291 | * |
||
292 | * @param string $key |
||
293 | * @param mixed $value |
||
294 | * @return FormetterOptions |
||
295 | */ |
||
296 | protected function setConfigurationValue($key, $value) |
||
301 | |||
302 | /** |
||
303 | * Change one configuration value for this formatter option, but only |
||
304 | * if it does not already have a value set. |
||
305 | * |
||
306 | * @param string $key |
||
307 | * @param mixed $value |
||
308 | * @return FormetterOptions |
||
309 | */ |
||
310 | public function setConfigurationDefault($key, $value) |
||
317 | |||
318 | /** |
||
319 | * Return a reference to the configuration data for this object. |
||
320 | * |
||
321 | * @return array |
||
322 | */ |
||
323 | public function getConfigurationData() |
||
327 | |||
328 | /** |
||
329 | * Set all of the options that were specified by the user for this request. |
||
330 | * |
||
331 | * @param array $options |
||
332 | * @return FormatterOptions |
||
333 | */ |
||
334 | public function setOptions($options) |
||
339 | |||
340 | /** |
||
341 | * Change one option value specified by the user for this request. |
||
342 | * |
||
343 | * @param string $key |
||
344 | * @param mixed $value |
||
345 | * @return FormatterOptions |
||
346 | */ |
||
347 | public function setOption($key, $value) |
||
352 | |||
353 | /** |
||
354 | * Return a reference to the user-specified options for this request. |
||
355 | * |
||
356 | * @return array |
||
357 | */ |
||
358 | public function getOptions() |
||
362 | |||
363 | /** |
||
364 | * Provide a Symfony Console InputInterface containing the user-specified |
||
365 | * options for this request. |
||
366 | * |
||
367 | * @param InputInterface $input |
||
368 | * @return type |
||
369 | */ |
||
370 | public function setInput(InputInterface $input) |
||
374 | |||
375 | /** |
||
376 | * Return all of the options from the provided $defaults array that |
||
377 | * exist in our InputInterface object. |
||
378 | * |
||
379 | * @param array $defaults |
||
380 | * @return array |
||
381 | */ |
||
382 | public function getInputOptions($defaults) |
||
398 | } |
||
399 |