Complex classes like Configuration 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 Configuration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class Configuration implements ConfigurationInterface |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * Mapping for boolean values passed on the console. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $booleanMapping = array( |
||
48 | 'true' => true, |
||
49 | 'false' => false, |
||
50 | '1' => true, |
||
51 | '0' => false, |
||
52 | 'on' => true, |
||
53 | 'off' => false |
||
54 | ); |
||
55 | |||
56 | /** |
||
57 | * The operation name to use. |
||
58 | * |
||
59 | * @var string |
||
60 | * @Type("string") |
||
61 | * @SerializedName("operation-name") |
||
62 | */ |
||
63 | protected $operationName; |
||
64 | |||
65 | /** |
||
66 | * The Magento edition, EE or CE. |
||
67 | * |
||
68 | * @var string |
||
69 | * @Type("string") |
||
70 | * @SerializedName("magento-edition") |
||
71 | */ |
||
72 | protected $magentoEdition = 'CE'; |
||
73 | |||
74 | /** |
||
75 | * The Magento version, e. g. 2.1.0. |
||
76 | * |
||
77 | * @var string |
||
78 | * @Type("string") |
||
79 | * @SerializedName("magento-version") |
||
80 | */ |
||
81 | protected $magentoVersion = '2.1.2'; |
||
82 | |||
83 | /** |
||
84 | * The Magento installation directory. |
||
85 | * |
||
86 | * @var string |
||
87 | * @Type("string") |
||
88 | * @SerializedName("installation-dir") |
||
89 | */ |
||
90 | protected $installationDir; |
||
91 | |||
92 | /** |
||
93 | * The source directory that has to be watched for new files. |
||
94 | * |
||
95 | * @var string |
||
96 | * @Type("string") |
||
97 | * @SerializedName("source-dir") |
||
98 | */ |
||
99 | protected $sourceDir; |
||
100 | |||
101 | /** |
||
102 | * The target directory with the files that has been imported. |
||
103 | * |
||
104 | * @var string |
||
105 | * @Type("string") |
||
106 | * @SerializedName("target-dir") |
||
107 | */ |
||
108 | protected $targetDir; |
||
109 | |||
110 | /** |
||
111 | * ArrayCollection with the information of the configured databases. |
||
112 | * |
||
113 | * @var \Doctrine\Common\Collections\ArrayCollection |
||
114 | * @Type("ArrayCollection<TechDivision\Import\Cli\Configuration\Database>") |
||
115 | */ |
||
116 | protected $databases; |
||
117 | |||
118 | /** |
||
119 | * ArrayCollection with the information of the configured operations. |
||
120 | * |
||
121 | * @var \Doctrine\Common\Collections\ArrayCollection |
||
122 | * @Type("ArrayCollection<TechDivision\Import\Cli\Configuration\Operation>") |
||
123 | */ |
||
124 | protected $operations; |
||
125 | |||
126 | /** |
||
127 | * The subject's utility class with the SQL statements to use. |
||
128 | * |
||
129 | * @var string |
||
130 | * @Type("string") |
||
131 | * @SerializedName("utility-class-name") |
||
132 | */ |
||
133 | protected $utilityClassName; |
||
134 | |||
135 | /** |
||
136 | * The source date format to use in the subject. |
||
137 | * |
||
138 | * @var string |
||
139 | * @Type("string") |
||
140 | * @SerializedName("source-date-format") |
||
141 | */ |
||
142 | protected $sourceDateFormat = 'n/d/y, g:i A'; |
||
143 | |||
144 | /** |
||
145 | * The subject's multiple field delimiter character for fields with multiple values, defaults to (,). |
||
146 | * |
||
147 | * @var string |
||
148 | * @Type("string") |
||
149 | * @SerializedName("multiple-field-delimiter") |
||
150 | */ |
||
151 | protected $multipleFieldDelimiter = ','; |
||
152 | |||
153 | /** |
||
154 | * The subject's delimiter character for CSV files. |
||
155 | * |
||
156 | * @var string |
||
157 | * @Type("string") |
||
158 | */ |
||
159 | protected $delimiter; |
||
160 | |||
161 | /** |
||
162 | * The subject's enclosure character for CSV files. |
||
163 | * |
||
164 | * @var string |
||
165 | * @Type("string") |
||
166 | */ |
||
167 | protected $enclosure; |
||
168 | |||
169 | /** |
||
170 | * The subject's escape character for CSV files. |
||
171 | * |
||
172 | * @var string |
||
173 | * @Type("string") |
||
174 | */ |
||
175 | protected $escape; |
||
176 | |||
177 | /** |
||
178 | * The subject's source charset for the CSV file. |
||
179 | * |
||
180 | * @var string |
||
181 | * @Type("string") |
||
182 | * @SerializedName("from-charset") |
||
183 | */ |
||
184 | protected $fromCharset; |
||
185 | |||
186 | /** |
||
187 | * The subject's target charset for a CSV file. |
||
188 | * |
||
189 | * @var string |
||
190 | * @Type("string") |
||
191 | * @SerializedName("to-charset") |
||
192 | */ |
||
193 | protected $toCharset; |
||
194 | |||
195 | /** |
||
196 | * The subject's file mode for a CSV target file. |
||
197 | * |
||
198 | * @var string |
||
199 | * @Type("string") |
||
200 | * @SerializedName("file-mode") |
||
201 | */ |
||
202 | protected $fileMode; |
||
203 | |||
204 | /** |
||
205 | * The flag to signal that the subject has to use the strict mode or not. |
||
206 | * |
||
207 | * @var boolean |
||
208 | * @Type("boolean") |
||
209 | * @SerializedName("strict-mode") |
||
210 | */ |
||
211 | protected $strictMode; |
||
212 | |||
213 | /** |
||
214 | * The flag whether or not the import artefacts have to be archived. |
||
215 | * |
||
216 | * @var boolean |
||
217 | * @Type("boolean") |
||
218 | * @SerializedName("archive-artefacts") |
||
219 | */ |
||
220 | protected $archiveArtefacts; |
||
221 | |||
222 | /** |
||
223 | * The directory where the archives will be stored. |
||
224 | * |
||
225 | * @var string |
||
226 | * @Type("string") |
||
227 | * @SerializedName("archive-dir") |
||
228 | */ |
||
229 | protected $archiveDir; |
||
230 | |||
231 | /** |
||
232 | * The flag to signal that the subject has to use the debug mode or not. |
||
233 | * |
||
234 | * @var boolean |
||
235 | * @Type("boolean") |
||
236 | * @SerializedName("debug-mode") |
||
237 | */ |
||
238 | protected $debugMode = false; |
||
239 | |||
240 | /** |
||
241 | * The flag to signal that the an existing PID should be ignored, whether or import process is running or not. |
||
242 | * |
||
243 | * @var boolean |
||
244 | * @Type("boolean") |
||
245 | * @SerializedName("ignore-pid") |
||
246 | */ |
||
247 | protected $ignorePid = false; |
||
248 | |||
249 | /** |
||
250 | * The log level to use (see Monolog documentation). |
||
251 | * |
||
252 | * @var string |
||
253 | * @Type("string") |
||
254 | * @SerializedName("log-level") |
||
255 | */ |
||
256 | protected $logLevel = LogLevel::INFO; |
||
257 | |||
258 | /** |
||
259 | * The explicit DB ID to use. |
||
260 | * |
||
261 | * @var string |
||
262 | * @Type("string") |
||
263 | * @SerializedName("use-db-id") |
||
264 | */ |
||
265 | protected $useDbId; |
||
266 | |||
267 | /** |
||
268 | * Return's the array with the subjects of the operation to use. |
||
269 | * |
||
270 | * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the subjects |
||
271 | * @throws \Exception Is thrown, if no subjects are available for the actual operation |
||
272 | */ |
||
273 | public function getSubjects() |
||
287 | |||
288 | /** |
||
289 | * Map's the passed value to a boolean. |
||
290 | * |
||
291 | * @param string $value The value to map |
||
292 | * |
||
293 | * @return boolean The mapped value |
||
294 | * @throws \Exception Is thrown, if the value can't be mapped |
||
295 | */ |
||
296 | public function mapBoolean($value) |
||
307 | |||
308 | /** |
||
309 | * Return's the operation, initialize from the actual operation name. |
||
310 | * |
||
311 | * @return \TechDivision\Import\Configuration\OperationInterface The operation instance |
||
312 | */ |
||
313 | protected function getOperation() |
||
317 | |||
318 | /** |
||
319 | * Return's the operation name that has to be used. |
||
320 | * |
||
321 | * @param string $operationName The operation name that has to be used |
||
322 | * |
||
323 | * @return void |
||
324 | */ |
||
325 | public function setOperationName($operationName) |
||
329 | |||
330 | /** |
||
331 | * Return's the operation name that has to be used. |
||
332 | * |
||
333 | * @return string The operation name that has to be used |
||
334 | */ |
||
335 | public function getOperationName() |
||
339 | |||
340 | /** |
||
341 | * Set's the Magento installation directory. |
||
342 | * |
||
343 | * @param string $installationDir The Magento installation directory |
||
344 | * |
||
345 | * @return void |
||
346 | */ |
||
347 | public function setInstallationDir($installationDir) |
||
351 | |||
352 | /** |
||
353 | * Return's the Magento installation directory. |
||
354 | * |
||
355 | * @return string The Magento installation directory |
||
356 | */ |
||
357 | public function getInstallationDir() |
||
361 | |||
362 | /** |
||
363 | * Set's the source directory that has to be watched for new files. |
||
364 | * |
||
365 | * @param string $sourceDir The source directory |
||
366 | * |
||
367 | * @return void |
||
368 | */ |
||
369 | public function setSourceDir($sourceDir) |
||
373 | |||
374 | /** |
||
375 | * Return's the source directory that has to be watched for new files. |
||
376 | * |
||
377 | * @return string The source directory |
||
378 | */ |
||
379 | public function getSourceDir() |
||
383 | |||
384 | /** |
||
385 | * Return's the target directory with the files that has been imported. |
||
386 | * |
||
387 | * @return string The target directory |
||
388 | */ |
||
389 | public function getTargetDir() |
||
393 | |||
394 | /** |
||
395 | * Set's the target directory with the files that has been imported. |
||
396 | * |
||
397 | * @param string $targetDir The target directory |
||
398 | * |
||
399 | * @return void |
||
400 | */ |
||
401 | public function setTargetDir($targetDir) |
||
405 | |||
406 | /** |
||
407 | * Return's the utility class with the SQL statements to use. |
||
408 | * |
||
409 | * @param string $utilityClassName The utility class name |
||
410 | * |
||
411 | * @return void |
||
412 | */ |
||
413 | public function setUtilityClassName($utilityClassName) |
||
417 | |||
418 | /** |
||
419 | * Return's the utility class with the SQL statements to use. |
||
420 | * |
||
421 | * @return string The utility class name |
||
422 | */ |
||
423 | public function getUtilityClassName() |
||
427 | |||
428 | /** |
||
429 | * Set's the Magento edition, EE or CE. |
||
430 | * |
||
431 | * @param string $magentoEdition The Magento edition |
||
432 | * |
||
433 | * @return void |
||
434 | */ |
||
435 | public function setMagentoEdition($magentoEdition) |
||
439 | |||
440 | /** |
||
441 | * Return's the Magento edition, EE or CE. |
||
442 | * |
||
443 | * @return string The Magento edition |
||
444 | */ |
||
445 | public function getMagentoEdition() |
||
449 | |||
450 | /** |
||
451 | * Return's the Magento version, e. g. 2.1.0. |
||
452 | * |
||
453 | * @param string $magentoVersion The Magento version |
||
454 | * |
||
455 | * @return void |
||
456 | */ |
||
457 | public function setMagentoVersion($magentoVersion) |
||
461 | |||
462 | /** |
||
463 | * Return's the Magento version, e. g. 2.1.0. |
||
464 | * |
||
465 | * @return string The Magento version |
||
466 | */ |
||
467 | public function getMagentoVersion() |
||
471 | |||
472 | /** |
||
473 | * Return's the subject's source date format to use. |
||
474 | * |
||
475 | * @return string The source date format |
||
476 | */ |
||
477 | public function getSourceDateFormat() |
||
481 | |||
482 | /** |
||
483 | * Set's the subject's source date format to use. |
||
484 | * |
||
485 | * @param string $sourceDateFormat The source date format |
||
486 | * |
||
487 | * @return void |
||
488 | */ |
||
489 | public function setSourceDateFormat($sourceDateFormat) |
||
493 | |||
494 | /** |
||
495 | * Return's the multiple field delimiter character to use, default value is comma (,). |
||
496 | * |
||
497 | * @return string The multiple field delimiter character |
||
498 | */ |
||
499 | public function getMultipleFieldDelimiter() |
||
503 | |||
504 | /** |
||
505 | * Return's the delimiter character to use, default value is comma (,). |
||
506 | * |
||
507 | * @return string The delimiter character |
||
508 | */ |
||
509 | public function getDelimiter() |
||
513 | |||
514 | /** |
||
515 | * The enclosure character to use, default value is double quotation ("). |
||
516 | * |
||
517 | * @return string The enclosure character |
||
518 | */ |
||
519 | public function getEnclosure() |
||
523 | |||
524 | /** |
||
525 | * The escape character to use, default value is backslash (\). |
||
526 | * |
||
527 | * @return string The escape character |
||
528 | */ |
||
529 | public function getEscape() |
||
533 | |||
534 | /** |
||
535 | * The file encoding of the CSV source file, default value is UTF-8. |
||
536 | * |
||
537 | * @return string The charset used by the CSV source file |
||
538 | */ |
||
539 | public function getFromCharset() |
||
543 | |||
544 | /** |
||
545 | * The file encoding of the CSV targetfile, default value is UTF-8. |
||
546 | * |
||
547 | * @return string The charset used by the CSV target file |
||
548 | */ |
||
549 | public function getToCharset() |
||
553 | |||
554 | /** |
||
555 | * The file mode of the CSV target file, either one of write or append, default is write. |
||
556 | * |
||
557 | * @return string The file mode of the CSV target file |
||
558 | */ |
||
559 | public function getFileMode() |
||
563 | |||
564 | /** |
||
565 | * Queries whether or not strict mode is enabled or not, default is TRUE. |
||
566 | * |
||
567 | * @return boolean TRUE if strict mode is enabled, else FALSE |
||
568 | */ |
||
569 | public function isStrictMode() |
||
573 | |||
574 | /** |
||
575 | * Remove's all configured database configuration. |
||
576 | * |
||
577 | * @return void |
||
578 | */ |
||
579 | public function clearDatabases() |
||
583 | |||
584 | /** |
||
585 | * Add's the passed database configuration. |
||
586 | * |
||
587 | * @param \TechDivision\Import\Configuration\DatabaseInterface $database The database configuration |
||
588 | * |
||
589 | * @return void |
||
590 | */ |
||
591 | public function addDatabase(DatabaseInterface $database) |
||
595 | |||
596 | /** |
||
597 | * Return's the database configuration. |
||
598 | * |
||
599 | * If an explicit DB ID is specified, the method tries to return the database with this ID. If |
||
600 | * the database configuration is NOT available, an execption is thrown. |
||
601 | * |
||
602 | * If no explicit DB ID is specified, the method tries to return the default database configuration, |
||
603 | * if not available the first one. |
||
604 | * |
||
605 | * @return \TechDivision\Import\Cli\Configuration\Database The database configuration |
||
606 | * @throws \Exception Is thrown, if no database configuration is available |
||
607 | */ |
||
608 | public function getDatabase() |
||
641 | |||
642 | /** |
||
643 | * Return's the ArrayCollection with the configured operations. |
||
644 | * |
||
645 | * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the operations |
||
646 | */ |
||
647 | public function getOperations() |
||
651 | |||
652 | /** |
||
653 | * Return's the TRUE if the import artefacts have to be archived. |
||
654 | * |
||
655 | * @return boolean TRUE if the import artefacts have to be archived |
||
656 | */ |
||
657 | public function haveArchiveArtefacts() |
||
661 | |||
662 | /** |
||
663 | * The directory where the archives will be stored. |
||
664 | * |
||
665 | * @return string The archive directory |
||
666 | */ |
||
667 | public function getArchiveDir() |
||
671 | |||
672 | /** |
||
673 | * Set's the debug mode. |
||
674 | * |
||
675 | * @param boolean $debugMode TRUE if debug mode is enabled, else FALSE |
||
676 | * |
||
677 | * @return void |
||
678 | */ |
||
679 | public function setDebugMode($debugMode) |
||
683 | |||
684 | /** |
||
685 | * Queries whether or not debug mode is enabled or not, default is TRUE. |
||
686 | * |
||
687 | * @return boolean TRUE if debug mode is enabled, else FALSE |
||
688 | */ |
||
689 | public function isDebugMode() |
||
693 | |||
694 | /** |
||
695 | * Set's the flag to signal that the an existing PID hast to be ignored, whether a |
||
696 | * import process is running or not. |
||
697 | * |
||
698 | * @param boolean $ignorePid TRUE if the PID has to be ignored, else FALSE |
||
699 | * |
||
700 | * @return void |
||
701 | */ |
||
702 | public function setIgnorePid($ignorePid) |
||
706 | |||
707 | /** |
||
708 | * Queries whether or not that the an existing PID has to be ignored. |
||
709 | * |
||
710 | * @return boolean TRUE if an existing PID has to be ignored, else FALSE |
||
711 | */ |
||
712 | public function isIgnorePid() |
||
716 | |||
717 | /** |
||
718 | * Set's the log level to use. |
||
719 | * |
||
720 | * @param string $logLevel The log level to use |
||
721 | * |
||
722 | * @return void |
||
723 | */ |
||
724 | public function setLogLevel($logLevel) |
||
728 | |||
729 | /** |
||
730 | * Return's the log level to use. |
||
731 | * |
||
732 | * @return string The log level to use |
||
733 | */ |
||
734 | public function getLogLevel() |
||
738 | |||
739 | /** |
||
740 | * Set's the explicit DB ID to use. |
||
741 | * |
||
742 | * @param string $useDbId The explicit DB ID to use |
||
743 | * |
||
744 | * @return void |
||
745 | */ |
||
746 | public function setUseDbId($useDbId) |
||
750 | |||
751 | /** |
||
752 | * Return's the explicit DB ID to use. |
||
753 | * |
||
754 | * @return string The explicit DB ID to use |
||
755 | */ |
||
756 | public function getUseDbId() |
||
760 | } |
||
761 |