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 | * The default PID filename to use. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | const PID_FILENAME = 'importer.pid'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Mapping for boolean values passed on the console. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $booleanMapping = array( |
||
| 55 | 'true' => true, |
||
| 56 | 'false' => false, |
||
| 57 | '1' => true, |
||
| 58 | '0' => false, |
||
| 59 | 'on' => true, |
||
| 60 | 'off' => false |
||
| 61 | ); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The operation name to use. |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | * @Type("string") |
||
| 68 | * @SerializedName("operation-name") |
||
| 69 | */ |
||
| 70 | protected $operationName; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The Magento edition, EE or CE. |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | * @Type("string") |
||
| 77 | * @SerializedName("magento-edition") |
||
| 78 | */ |
||
| 79 | protected $magentoEdition = 'CE'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The Magento version, e. g. 2.1.0. |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | * @Type("string") |
||
| 86 | * @SerializedName("magento-version") |
||
| 87 | */ |
||
| 88 | protected $magentoVersion = '2.1.2'; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The Magento installation directory. |
||
| 92 | * |
||
| 93 | * @var string |
||
| 94 | * @Type("string") |
||
| 95 | * @SerializedName("installation-dir") |
||
| 96 | */ |
||
| 97 | protected $installationDir; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * The source directory that has to be watched for new files. |
||
| 101 | * |
||
| 102 | * @var string |
||
| 103 | * @Type("string") |
||
| 104 | * @SerializedName("source-dir") |
||
| 105 | */ |
||
| 106 | protected $sourceDir; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The target directory with the files that has been imported. |
||
| 110 | * |
||
| 111 | * @var string |
||
| 112 | * @Type("string") |
||
| 113 | * @SerializedName("target-dir") |
||
| 114 | */ |
||
| 115 | protected $targetDir; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * ArrayCollection with the information of the configured databases. |
||
| 119 | * |
||
| 120 | * @var \Doctrine\Common\Collections\ArrayCollection |
||
| 121 | * @Type("ArrayCollection<TechDivision\Import\Cli\Configuration\Database>") |
||
| 122 | */ |
||
| 123 | protected $databases; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * ArrayCollection with the information of the configured loggers. |
||
| 127 | * |
||
| 128 | * @var \Doctrine\Common\Collections\ArrayCollection |
||
| 129 | * @Type("ArrayCollection<TechDivision\Import\Cli\Configuration\Logger>") |
||
| 130 | */ |
||
| 131 | protected $loggers = array(); |
||
| 132 | |||
| 133 | /** |
||
| 134 | * ArrayCollection with the information of the configured operations. |
||
| 135 | * |
||
| 136 | * @var \Doctrine\Common\Collections\ArrayCollection |
||
| 137 | * @Type("ArrayCollection<TechDivision\Import\Cli\Configuration\Operation>") |
||
| 138 | */ |
||
| 139 | protected $operations; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * The subject's utility class with the SQL statements to use. |
||
| 143 | * |
||
| 144 | * @var string |
||
| 145 | * @Type("string") |
||
| 146 | * @SerializedName("utility-class-name") |
||
| 147 | */ |
||
| 148 | protected $utilityClassName; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * The source date format to use in the subject. |
||
| 152 | * |
||
| 153 | * @var string |
||
| 154 | * @Type("string") |
||
| 155 | * @SerializedName("source-date-format") |
||
| 156 | */ |
||
| 157 | protected $sourceDateFormat = 'n/d/y, g:i A'; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * The subject's multiple field delimiter character for fields with multiple values, defaults to (,). |
||
| 161 | * |
||
| 162 | * @var string |
||
| 163 | * @Type("string") |
||
| 164 | * @SerializedName("multiple-field-delimiter") |
||
| 165 | */ |
||
| 166 | protected $multipleFieldDelimiter = ','; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * The subject's delimiter character for CSV files. |
||
| 170 | * |
||
| 171 | * @var string |
||
| 172 | * @Type("string") |
||
| 173 | */ |
||
| 174 | protected $delimiter; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * The subject's enclosure character for CSV files. |
||
| 178 | * |
||
| 179 | * @var string |
||
| 180 | * @Type("string") |
||
| 181 | */ |
||
| 182 | protected $enclosure; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * The subject's escape character for CSV files. |
||
| 186 | * |
||
| 187 | * @var string |
||
| 188 | * @Type("string") |
||
| 189 | */ |
||
| 190 | protected $escape; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * The subject's source charset for the CSV file. |
||
| 194 | * |
||
| 195 | * @var string |
||
| 196 | * @Type("string") |
||
| 197 | * @SerializedName("from-charset") |
||
| 198 | */ |
||
| 199 | protected $fromCharset; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * The subject's target charset for a CSV file. |
||
| 203 | * |
||
| 204 | * @var string |
||
| 205 | * @Type("string") |
||
| 206 | * @SerializedName("to-charset") |
||
| 207 | */ |
||
| 208 | protected $toCharset; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * The subject's file mode for a CSV target file. |
||
| 212 | * |
||
| 213 | * @var string |
||
| 214 | * @Type("string") |
||
| 215 | * @SerializedName("file-mode") |
||
| 216 | */ |
||
| 217 | protected $fileMode; |
||
| 218 | |||
| 219 | /** |
||
| 220 | * The flag to signal that the subject has to use the strict mode or not. |
||
| 221 | * |
||
| 222 | * @var boolean |
||
| 223 | * @Type("boolean") |
||
| 224 | * @SerializedName("strict-mode") |
||
| 225 | */ |
||
| 226 | protected $strictMode; |
||
| 227 | |||
| 228 | /** |
||
| 229 | * The flag whether or not the import artefacts have to be archived. |
||
| 230 | * |
||
| 231 | * @var boolean |
||
| 232 | * @Type("boolean") |
||
| 233 | * @SerializedName("archive-artefacts") |
||
| 234 | */ |
||
| 235 | protected $archiveArtefacts; |
||
| 236 | |||
| 237 | /** |
||
| 238 | * The directory where the archives will be stored. |
||
| 239 | * |
||
| 240 | * @var string |
||
| 241 | * @Type("string") |
||
| 242 | * @SerializedName("archive-dir") |
||
| 243 | */ |
||
| 244 | protected $archiveDir; |
||
| 245 | |||
| 246 | /** |
||
| 247 | * The flag to signal that the subject has to use the debug mode or not. |
||
| 248 | * |
||
| 249 | * @var boolean |
||
| 250 | * @Type("boolean") |
||
| 251 | * @SerializedName("debug-mode") |
||
| 252 | */ |
||
| 253 | protected $debugMode = false; |
||
| 254 | |||
| 255 | /** |
||
| 256 | * The log level to use (see Monolog documentation). |
||
| 257 | * |
||
| 258 | * @var string |
||
| 259 | * @Type("string") |
||
| 260 | * @SerializedName("log-level") |
||
| 261 | */ |
||
| 262 | protected $logLevel = LogLevel::INFO; |
||
| 263 | |||
| 264 | /** |
||
| 265 | * The explicit DB ID to use. |
||
| 266 | * |
||
| 267 | * @var string |
||
| 268 | * @Type("string") |
||
| 269 | * @SerializedName("use-db-id") |
||
| 270 | */ |
||
| 271 | protected $useDbId; |
||
| 272 | |||
| 273 | /** |
||
| 274 | * The explicit PID filename to use. |
||
| 275 | * |
||
| 276 | * @var string |
||
| 277 | * @Type("string") |
||
| 278 | * @SerializedName("pid-filename") |
||
| 279 | */ |
||
| 280 | protected $pidFilename; |
||
| 281 | |||
| 282 | /** |
||
| 283 | * The entity type code to use. |
||
| 284 | * |
||
| 285 | * @var string |
||
| 286 | * @Type("string") |
||
| 287 | * @SerializedName("entity-type-code") |
||
| 288 | */ |
||
| 289 | protected $entityTypeCode; |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Return's the array with the plugins of the operation to use. |
||
| 293 | * |
||
| 294 | * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the plugins |
||
| 295 | * @throws \Exception Is thrown, if no plugins are available for the actual operation |
||
| 296 | */ |
||
| 297 | public function getPlugins() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Map's the passed value to a boolean. |
||
| 314 | * |
||
| 315 | * @param string $value The value to map |
||
| 316 | * |
||
| 317 | * @return boolean The mapped value |
||
| 318 | * @throws \Exception Is thrown, if the value can't be mapped |
||
| 319 | */ |
||
| 320 | public function mapBoolean($value) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Return's the operation, initialize from the actual operation name. |
||
| 334 | * |
||
| 335 | * @return \TechDivision\Import\Configuration\OperationInterface The operation instance |
||
| 336 | */ |
||
| 337 | protected function getOperation() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Return's the operation name that has to be used. |
||
| 344 | * |
||
| 345 | * @param string $operationName The operation name that has to be used |
||
| 346 | * |
||
| 347 | * @return void |
||
| 348 | */ |
||
| 349 | public function setOperationName($operationName) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Return's the operation name that has to be used. |
||
| 356 | * |
||
| 357 | * @return string The operation name that has to be used |
||
| 358 | */ |
||
| 359 | public function getOperationName() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Set's the Magento installation directory. |
||
| 366 | * |
||
| 367 | * @param string $installationDir The Magento installation directory |
||
| 368 | * |
||
| 369 | * @return void |
||
| 370 | */ |
||
| 371 | public function setInstallationDir($installationDir) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Return's the Magento installation directory. |
||
| 378 | * |
||
| 379 | * @return string The Magento installation directory |
||
| 380 | */ |
||
| 381 | public function getInstallationDir() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Set's the source directory that has to be watched for new files. |
||
| 388 | * |
||
| 389 | * @param string $sourceDir The source directory |
||
| 390 | * |
||
| 391 | * @return void |
||
| 392 | */ |
||
| 393 | public function setSourceDir($sourceDir) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Return's the source directory that has to be watched for new files. |
||
| 400 | * |
||
| 401 | * @return string The source directory |
||
| 402 | */ |
||
| 403 | public function getSourceDir() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Return's the target directory with the files that has been imported. |
||
| 410 | * |
||
| 411 | * @return string The target directory |
||
| 412 | */ |
||
| 413 | public function getTargetDir() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Set's the target directory with the files that has been imported. |
||
| 420 | * |
||
| 421 | * @param string $targetDir The target directory |
||
| 422 | * |
||
| 423 | * @return void |
||
| 424 | */ |
||
| 425 | public function setTargetDir($targetDir) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Return's the utility class with the SQL statements to use. |
||
| 432 | * |
||
| 433 | * @param string $utilityClassName The utility class name |
||
| 434 | * |
||
| 435 | * @return void |
||
| 436 | */ |
||
| 437 | public function setUtilityClassName($utilityClassName) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Return's the utility class with the SQL statements to use. |
||
| 444 | * |
||
| 445 | * @return string The utility class name |
||
| 446 | */ |
||
| 447 | public function getUtilityClassName() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Set's the Magento edition, EE or CE. |
||
| 454 | * |
||
| 455 | * @param string $magentoEdition The Magento edition |
||
| 456 | * |
||
| 457 | * @return void |
||
| 458 | */ |
||
| 459 | public function setMagentoEdition($magentoEdition) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Return's the Magento edition, EE or CE. |
||
| 466 | * |
||
| 467 | * @return string The Magento edition |
||
| 468 | */ |
||
| 469 | public function getMagentoEdition() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Return's the Magento version, e. g. 2.1.0. |
||
| 476 | * |
||
| 477 | * @param string $magentoVersion The Magento version |
||
| 478 | * |
||
| 479 | * @return void |
||
| 480 | */ |
||
| 481 | public function setMagentoVersion($magentoVersion) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Return's the Magento version, e. g. 2.1.0. |
||
| 488 | * |
||
| 489 | * @return string The Magento version |
||
| 490 | */ |
||
| 491 | public function getMagentoVersion() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Return's the subject's source date format to use. |
||
| 498 | * |
||
| 499 | * @return string The source date format |
||
| 500 | */ |
||
| 501 | public function getSourceDateFormat() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Set's the subject's source date format to use. |
||
| 508 | * |
||
| 509 | * @param string $sourceDateFormat The source date format |
||
| 510 | * |
||
| 511 | * @return void |
||
| 512 | */ |
||
| 513 | public function setSourceDateFormat($sourceDateFormat) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Return's the multiple field delimiter character to use, default value is comma (,). |
||
| 520 | * |
||
| 521 | * @return string The multiple field delimiter character |
||
| 522 | */ |
||
| 523 | public function getMultipleFieldDelimiter() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Return's the delimiter character to use, default value is comma (,). |
||
| 530 | * |
||
| 531 | * @return string The delimiter character |
||
| 532 | */ |
||
| 533 | public function getDelimiter() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * The enclosure character to use, default value is double quotation ("). |
||
| 540 | * |
||
| 541 | * @return string The enclosure character |
||
| 542 | */ |
||
| 543 | public function getEnclosure() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * The escape character to use, default value is backslash (\). |
||
| 550 | * |
||
| 551 | * @return string The escape character |
||
| 552 | */ |
||
| 553 | public function getEscape() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * The file encoding of the CSV source file, default value is UTF-8. |
||
| 560 | * |
||
| 561 | * @return string The charset used by the CSV source file |
||
| 562 | */ |
||
| 563 | public function getFromCharset() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * The file encoding of the CSV targetfile, default value is UTF-8. |
||
| 570 | * |
||
| 571 | * @return string The charset used by the CSV target file |
||
| 572 | */ |
||
| 573 | public function getToCharset() |
||
| 577 | |||
| 578 | /** |
||
| 579 | * The file mode of the CSV target file, either one of write or append, default is write. |
||
| 580 | * |
||
| 581 | * @return string The file mode of the CSV target file |
||
| 582 | */ |
||
| 583 | public function getFileMode() |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Queries whether or not strict mode is enabled or not, default is TRUE. |
||
| 590 | * |
||
| 591 | * @return boolean TRUE if strict mode is enabled, else FALSE |
||
| 592 | */ |
||
| 593 | public function isStrictMode() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Return's the entity type code to be used. |
||
| 600 | * |
||
| 601 | * @return string The entity type code to be used |
||
| 602 | */ |
||
| 603 | public function getEntityTypeCode() |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Remove's all configured database configuration. |
||
| 610 | * |
||
| 611 | * @return void |
||
| 612 | */ |
||
| 613 | public function clearDatabases() |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Add's the passed database configuration. |
||
| 620 | * |
||
| 621 | * @param \TechDivision\Import\Configuration\DatabaseConfigurationInterface $database The database configuration |
||
| 622 | * |
||
| 623 | * @return void |
||
| 624 | */ |
||
| 625 | public function addDatabase(DatabaseConfigurationInterface $database) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Return's the database configuration. |
||
| 632 | * |
||
| 633 | * If an explicit DB ID is specified, the method tries to return the database with this ID. If |
||
| 634 | * the database configuration is NOT available, an execption is thrown. |
||
| 635 | * |
||
| 636 | * If no explicit DB ID is specified, the method tries to return the default database configuration, |
||
| 637 | * if not available the first one. |
||
| 638 | * |
||
| 639 | * @return \TechDivision\Import\Cli\Configuration\Database The database configuration |
||
| 640 | * @throws \Exception Is thrown, if no database configuration is available |
||
| 641 | */ |
||
| 642 | public function getDatabase() |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Return's the ArrayCollection with the configured operations. |
||
| 678 | * |
||
| 679 | * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the operations |
||
| 680 | */ |
||
| 681 | public function getOperations() |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Return's the ArrayCollection with the configured loggers. |
||
| 688 | * |
||
| 689 | * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the loggers |
||
| 690 | */ |
||
| 691 | public function getLoggers() |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Return's the TRUE if the import artefacts have to be archived. |
||
| 698 | * |
||
| 699 | * @return boolean TRUE if the import artefacts have to be archived |
||
| 700 | */ |
||
| 701 | public function haveArchiveArtefacts() |
||
| 705 | |||
| 706 | /** |
||
| 707 | * The directory where the archives will be stored. |
||
| 708 | * |
||
| 709 | * @return string The archive directory |
||
| 710 | */ |
||
| 711 | public function getArchiveDir() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Set's the debug mode. |
||
| 718 | * |
||
| 719 | * @param boolean $debugMode TRUE if debug mode is enabled, else FALSE |
||
| 720 | * |
||
| 721 | * @return void |
||
| 722 | */ |
||
| 723 | public function setDebugMode($debugMode) |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Queries whether or not debug mode is enabled or not, default is TRUE. |
||
| 730 | * |
||
| 731 | * @return boolean TRUE if debug mode is enabled, else FALSE |
||
| 732 | */ |
||
| 733 | public function isDebugMode() |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Set's the log level to use. |
||
| 740 | * |
||
| 741 | * @param string $logLevel The log level to use |
||
| 742 | * |
||
| 743 | * @return void |
||
| 744 | */ |
||
| 745 | public function setLogLevel($logLevel) |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Return's the log level to use. |
||
| 752 | * |
||
| 753 | * @return string The log level to use |
||
| 754 | */ |
||
| 755 | public function getLogLevel() |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Set's the explicit DB ID to use. |
||
| 762 | * |
||
| 763 | * @param string $useDbId The explicit DB ID to use |
||
| 764 | * |
||
| 765 | * @return void |
||
| 766 | */ |
||
| 767 | public function setUseDbId($useDbId) |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Return's the explicit DB ID to use. |
||
| 774 | * |
||
| 775 | * @return string The explicit DB ID to use |
||
| 776 | */ |
||
| 777 | public function getUseDbId() |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Set's the PID filename to use. |
||
| 784 | * |
||
| 785 | * @param string $pidFilename The PID filename to use |
||
| 786 | * |
||
| 787 | * @return void |
||
| 788 | */ |
||
| 789 | public function setPidFilename($pidFilename) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Return's the PID filename to use. |
||
| 796 | * |
||
| 797 | * @return string The PID filename to use |
||
| 798 | */ |
||
| 799 | public function getPidFilename() |
||
| 803 | } |
||
| 804 |