Complex classes like Dialect 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 Dialect, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Dialect |
||
11 | { |
||
12 | const ENCLOSING_ALL = 'all'; |
||
13 | const ENCLOSING_MINIMAL = 'minimal'; |
||
14 | const ENCLOSING_NONNUMERIC = 'nonnumeric'; |
||
15 | |||
16 | protected static $defaultOptions = array( |
||
17 | 'excel' => array( |
||
18 | 'delimiter' => ';', |
||
19 | 'enclosure' => '"', |
||
20 | 'enclosing_mode' => 'minimal', |
||
21 | 'encoding' => 'CP1252', |
||
22 | 'eol' => "\r\n", |
||
23 | 'escape' => "\\", |
||
24 | 'escape_double' => true, |
||
25 | 'bom' => false, |
||
26 | 'translit' => 'translit', |
||
27 | 'force_encoding_detect' => false, |
||
28 | 'first_row_header' => false, |
||
29 | 'skip_empty' => false, |
||
30 | 'trim' => false, |
||
31 | ), |
||
32 | 'unix' => array( |
||
33 | 'delimiter' => ',', |
||
34 | 'enclosure' => '"', |
||
35 | 'enclosing_mode' => 'minimal', |
||
36 | 'encoding' => 'UTF-8', |
||
37 | 'eol' => "\n", |
||
38 | 'escape' => "\\", |
||
39 | 'escape_double' => true, |
||
40 | 'bom' => false, |
||
41 | 'translit' => 'translit', |
||
42 | 'force_encoding_detect' => false, |
||
43 | 'first_row_header' => false, |
||
44 | 'skip_empty' => false, |
||
45 | 'trim' => false, |
||
46 | ), |
||
47 | ); |
||
48 | |||
49 | /** |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $translit; |
||
54 | |||
55 | /** |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $eol; |
||
60 | |||
61 | /** |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $encoding; |
||
66 | |||
67 | /** |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $enclosingMode; |
||
72 | |||
73 | /** |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $enclosure; |
||
78 | |||
79 | /** |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $escape; |
||
84 | |||
85 | /** |
||
86 | * |
||
87 | * @var bool |
||
88 | */ |
||
89 | protected $escapeDouble; |
||
90 | |||
91 | /** |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | protected $delimiter; |
||
96 | |||
97 | /** |
||
98 | * |
||
99 | * @var bool |
||
100 | */ |
||
101 | protected $useBom = false; |
||
102 | |||
103 | /** |
||
104 | * |
||
105 | * @var bool |
||
106 | */ |
||
107 | protected $firstRowHeader = false; |
||
108 | |||
109 | /** |
||
110 | * |
||
111 | * @var bool |
||
112 | */ |
||
113 | protected $trim = false; |
||
114 | |||
115 | /** |
||
116 | * |
||
117 | * @var bool |
||
118 | */ |
||
119 | protected $forceEncodingDetection; |
||
120 | |||
121 | /** |
||
122 | * |
||
123 | * @var bool |
||
124 | */ |
||
125 | protected $skipEmptyLines; |
||
126 | |||
127 | /** |
||
128 | * available options : |
||
129 | * - delimiter : (default = ';') |
||
130 | * - enclosure : (default = '"') |
||
131 | * - encoding : (default = 'CP1252') |
||
132 | * - eol : (default = "\r\n") |
||
133 | * - escape : (default = "\\") |
||
134 | * - first_row_header : (default = false) use the first CSV row as header |
||
135 | * - bom : (default = false) add UTF8 BOM marker |
||
136 | * - translit : (default = 'translit') iconv translit option possible values : 'translit', 'ignore', null |
||
137 | * - force_encoding_detect : (default = false) |
||
138 | * - skip_empty : (default = false) remove lines with empty values |
||
139 | * - trim : (default = false) trim each values on each line |
||
140 | * |
||
141 | * N.B. : Be careful, the options 'force_encoding_detect', 'skip_empty' and 'trim' |
||
142 | * decrease significantly the performances |
||
143 | * |
||
144 | * @param array $options Dialect Options to describe CSV file parameters |
||
145 | */ |
||
146 | 139 | public function __construct($options = []) |
|
172 | |||
173 | /** |
||
174 | * get Default CSV options for a specific CSV reader application like Excel |
||
175 | * |
||
176 | * @param string $CSVType default = excel |
||
177 | * |
||
178 | * @return array |
||
179 | */ |
||
180 | 139 | public static function getDialectDefaultOptions($CSVType = 'excel') |
|
184 | |||
185 | /** |
||
186 | * return a CSV Dialect for Excel |
||
187 | * |
||
188 | * @return Dialect |
||
189 | */ |
||
190 | 1 | public static function createExcelDialect() |
|
194 | |||
195 | /** |
||
196 | * return a standard CSV Dialect for unix with UTF-8 |
||
197 | * |
||
198 | * @return Dialect |
||
199 | */ |
||
200 | 1 | public static function createUnixDialect() |
|
204 | |||
205 | /** |
||
206 | * |
||
207 | * @param string $eol |
||
208 | * @return Dialect |
||
209 | */ |
||
210 | 139 | public function setLineEndings($eol) |
|
235 | |||
236 | /** |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 36 | public function getTranslit() |
|
244 | |||
245 | /** |
||
246 | * |
||
247 | * @param string $translit default = "translit" (iconv option : 'translit', 'ignore', null) |
||
248 | * @return Dialect |
||
249 | */ |
||
250 | 139 | public function setTranslit($translit) |
|
257 | |||
258 | /** |
||
259 | * |
||
260 | * @param string $encoding |
||
261 | * @return Dialect |
||
262 | */ |
||
263 | 139 | public function setEncoding($encoding) |
|
269 | |||
270 | /** |
||
271 | * |
||
272 | * @param string $enclosure |
||
273 | * @return Dialect |
||
274 | */ |
||
275 | 139 | public function setEnclosure($enclosure) |
|
281 | |||
282 | /** |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | 19 | public function getEnclosingMode() |
|
290 | |||
291 | /** |
||
292 | * |
||
293 | * @param string $enclosingMode |
||
294 | * @return Dialect |
||
295 | */ |
||
296 | 139 | public function setEnclosingMode($enclosingMode) |
|
306 | |||
307 | /** |
||
308 | * |
||
309 | * @return bool |
||
310 | */ |
||
311 | 21 | public function getEscapeDouble() |
|
315 | |||
316 | /** |
||
317 | * |
||
318 | * @param bool $escapeDouble |
||
319 | * @return Dialect |
||
320 | */ |
||
321 | 139 | public function setEscapeDouble($escapeDouble) |
|
327 | |||
328 | /** |
||
329 | * |
||
330 | * @param string $escape |
||
331 | * @return Dialect |
||
332 | */ |
||
333 | 139 | public function setEscape($escape) |
|
339 | |||
340 | /** |
||
341 | * |
||
342 | * @param string $delimiter |
||
343 | * @return Dialect |
||
344 | */ |
||
345 | 139 | public function setDelimiter($delimiter) |
|
351 | |||
352 | /** |
||
353 | * |
||
354 | * @param bool $asLabel get EOL as a label string like 'windows', 'unix', 'mac' |
||
355 | * @return string |
||
356 | */ |
||
357 | 28 | public function getLineEndings($asLabel = false) |
|
379 | |||
380 | /** |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | 11 | public function getLineEndingsAsLabel() |
|
388 | |||
389 | /** |
||
390 | * |
||
391 | * @return string |
||
392 | */ |
||
393 | 69 | public function getEncoding() |
|
397 | |||
398 | /** |
||
399 | * |
||
400 | * @return string |
||
401 | */ |
||
402 | 44 | public function getEnclosure() |
|
406 | |||
407 | /** |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | 44 | public function getEscape() |
|
415 | |||
416 | /** |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | 44 | public function getDelimiter() |
|
424 | |||
425 | /** |
||
426 | * |
||
427 | * @return bool |
||
428 | */ |
||
429 | 37 | public function getUseBom() |
|
433 | |||
434 | /** |
||
435 | * |
||
436 | * @param bool $useBom (BOM will be writed when opening the file) |
||
437 | * |
||
438 | * @return Dialect |
||
439 | */ |
||
440 | 139 | public function setUseBom($useBom) |
|
446 | |||
447 | /** |
||
448 | * |
||
449 | * @return bool |
||
450 | */ |
||
451 | 38 | public function getTrim() |
|
455 | |||
456 | /** |
||
457 | * |
||
458 | * @param bool $trim (trim all values) |
||
459 | * |
||
460 | * @return Dialect |
||
461 | */ |
||
462 | 139 | public function setTrim($trim) |
|
468 | |||
469 | /** |
||
470 | * use first row as header |
||
471 | * |
||
472 | * @return bool |
||
473 | */ |
||
474 | 47 | public function getFirstRowHeader() |
|
478 | |||
479 | /** |
||
480 | * use first row as header |
||
481 | * |
||
482 | * @param bool $firstRowHeader |
||
483 | * |
||
484 | * @return Dialect |
||
485 | */ |
||
486 | 139 | public function setFirstRowHeader($firstRowHeader) |
|
492 | |||
493 | /** |
||
494 | * |
||
495 | * @return bool |
||
496 | */ |
||
497 | 25 | public function getForceEncodingDetection() |
|
501 | |||
502 | /** |
||
503 | * |
||
504 | * @param bool $forceEncodingDetection |
||
505 | * @return Dialect |
||
506 | */ |
||
507 | 139 | public function setForceEncodingDetection($forceEncodingDetection) |
|
513 | |||
514 | /** |
||
515 | * |
||
516 | * @return bool |
||
517 | */ |
||
518 | 35 | public function getSkipEmptyLines() |
|
522 | |||
523 | /** |
||
524 | * |
||
525 | * @param bool $skipEmptyLines |
||
526 | * @return Dialect |
||
527 | */ |
||
528 | 139 | public function setSkipEmptyLines($skipEmptyLines) |
|
534 | } |
||
535 |