Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Simple 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 Simple, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class Simple |
||
| 49 | { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The default style to write messages to the symfony console. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | const DEFAULT_STYLE = 'info'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The TechDivision company name as ANSI art. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $ansiArt = ' _______ _ _____ _ _ _ |
||
| 64 | |__ __| | | | __ \(_) (_) (_) |
||
| 65 | | | ___ ___| |__ | | | |___ ___ ___ _ ___ _ __ |
||
| 66 | | |/ _ \/ __| \'_ \| | | | \ \ / / / __| |/ _ \| \'_ \ |
||
| 67 | | | __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | | |
||
| 68 | |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_| |
||
| 69 | '; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The log level => console style mapping. |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $logLevelStyleMapping = array( |
||
| 77 | LogLevel::INFO => 'info', |
||
| 78 | LogLevel::DEBUG => 'comment', |
||
| 79 | LogLevel::ERROR => 'error', |
||
| 80 | LogLevel::ALERT => 'error', |
||
| 81 | LogLevel::CRITICAL => 'error', |
||
| 82 | LogLevel::EMERGENCY => 'error', |
||
| 83 | LogLevel::WARNING => 'error', |
||
| 84 | LogLevel::NOTICE => 'info' |
||
| 85 | ); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The actions unique serial. |
||
| 89 | * |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $serial; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The system logger implementation. |
||
| 96 | * |
||
| 97 | * @var \Psr\Log\LoggerInterface |
||
| 98 | */ |
||
| 99 | protected $systemLogger; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The RegistryProcessor instance to handle running threads. |
||
| 103 | * |
||
| 104 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 105 | */ |
||
| 106 | protected $registryProcessor; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The processor to read/write the necessary import data. |
||
| 110 | * |
||
| 111 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
| 112 | */ |
||
| 113 | protected $importProcessor; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The system configuration. |
||
| 117 | * |
||
| 118 | * @var \TechDivision\Import\ConfigurationInterface |
||
| 119 | */ |
||
| 120 | protected $configuration; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * The input stream to read console information from. |
||
| 124 | * |
||
| 125 | * @var \Symfony\Component\Console\Input\InputInterface |
||
| 126 | */ |
||
| 127 | protected $input; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * The output stream to write console information to. |
||
| 131 | * |
||
| 132 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 133 | */ |
||
| 134 | protected $output; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * The matches for the last processed CSV filename. |
||
| 138 | * |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | protected $matches = array(); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Set's the unique serial for this import process. |
||
| 145 | * |
||
| 146 | * @param string $serial The unique serial |
||
| 147 | * |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | public function setSerial($serial) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Return's the unique serial for this import process. |
||
| 157 | * |
||
| 158 | * @return string The unique serial |
||
| 159 | */ |
||
| 160 | public function getSerial() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Set's the system logger. |
||
| 167 | * |
||
| 168 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
| 169 | * |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | public function setSystemLogger(LoggerInterface $systemLogger) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Return's the system logger. |
||
| 179 | * |
||
| 180 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
| 181 | */ |
||
| 182 | public function getSystemLogger() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Sets's the RegistryProcessor instance to handle the running threads. |
||
| 189 | * |
||
| 190 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 191 | * |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 201 | * |
||
| 202 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 203 | */ |
||
| 204 | public function getRegistryProcessor() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set's the import processor instance. |
||
| 211 | * |
||
| 212 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 213 | * |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | 1 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Return's the import processor instance. |
||
| 223 | * |
||
| 224 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 225 | */ |
||
| 226 | 1 | public function getImportProcessor() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Set's the system configuration. |
||
| 233 | * |
||
| 234 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 235 | * |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | public function setConfiguration(ConfigurationInterface $configuration) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Return's the system configuration. |
||
| 245 | * |
||
| 246 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
| 247 | */ |
||
| 248 | public function getConfiguration() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Set's the input stream to read console information from. |
||
| 255 | * |
||
| 256 | * @param \Symfony\Component\Console\Input\InputInterface $input An IutputInterface instance |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | public function setInput(InputInterface $input) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Return's the input stream to read console information from. |
||
| 267 | * |
||
| 268 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
| 269 | */ |
||
| 270 | protected function getInput() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Set's the output stream to write console information to. |
||
| 277 | * |
||
| 278 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
| 279 | * |
||
| 280 | * @return void |
||
| 281 | */ |
||
| 282 | public function setOutput(OutputInterface $output) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Return's the output stream to write console information to. |
||
| 289 | * |
||
| 290 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
| 291 | */ |
||
| 292 | protected function getOutput() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Return's the source directory that has to be watched for new files. |
||
| 299 | * |
||
| 300 | * @return string The source directory |
||
| 301 | */ |
||
| 302 | protected function getSourceDir() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Parse the temporary upload directory for new files to be imported. |
||
| 309 | * |
||
| 310 | * @return void |
||
| 311 | * @throws \Exception Is thrown if the import can't be finished successfully |
||
| 312 | */ |
||
| 313 | public function import() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * This method start's the import process by initializing |
||
| 358 | * the status and appends it to the registry. |
||
| 359 | * |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | protected function start() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Prepares the global data for the import process. |
||
| 403 | * |
||
| 404 | * @return void |
||
| 405 | */ |
||
| 406 | protected function setUp() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Process all the subjects defined in the system configuration. |
||
| 475 | * |
||
| 476 | * @return void |
||
| 477 | * @throws \Exception Is thrown, if one of the subjects can't be processed |
||
| 478 | */ |
||
| 479 | protected function processSubjects() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Process the subject with the passed name/identifier. |
||
| 511 | * |
||
| 512 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
| 513 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
| 514 | * environment. |
||
| 515 | * |
||
| 516 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
| 517 | * |
||
| 518 | * @return void |
||
| 519 | * @throws \Exception Is thrown, if the subject can't be processed |
||
| 520 | */ |
||
| 521 | protected function processSubject($subject) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Queries whether or not, the passed filename is part of a bunch or not. |
||
| 565 | * |
||
| 566 | * @param string $prefix The prefix of the files to import |
||
| 567 | * @param string $filename The filename to query |
||
| 568 | * |
||
| 569 | * @return boolean TRUE if the filename is part, else FALSE |
||
| 570 | */ |
||
| 571 | 2 | public function isPartOfBunch($prefix, $filename) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Factory method to create new handler instances. |
||
| 611 | * |
||
| 612 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
| 613 | * |
||
| 614 | * @return object The handler instance |
||
| 615 | */ |
||
| 616 | public function subjectFactory($subject) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Lifecycle callback that will be inovked after the |
||
| 649 | * import process has been finished. |
||
| 650 | * |
||
| 651 | * @return void |
||
| 652 | * @throws \Exception Is thrown, if the |
||
| 653 | */ |
||
| 654 | protected function archive() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Removes the passed directory recursively. |
||
| 718 | * |
||
| 719 | * @param string $src Name of the directory to remove |
||
| 720 | * |
||
| 721 | * @return void |
||
| 722 | */ |
||
| 723 | protected function removeDir($src) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Simple method that writes the passed method the the console and the |
||
| 748 | * system logger, if configured and a log level has been passed. |
||
| 749 | * |
||
| 750 | * @param string $msg The message to log |
||
| 751 | * @param string $logLevel The log level to use |
||
| 752 | * |
||
| 753 | * @return void |
||
| 754 | */ |
||
| 755 | protected function log($msg, $logLevel = null) |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Map's the passed log level to a valid symfony console style. |
||
| 775 | * |
||
| 776 | * @param string $logLevel The log level to map |
||
| 777 | * |
||
| 778 | * @return string The apropriate symfony console style |
||
| 779 | */ |
||
| 780 | protected function mapLogLevelToStyle($logLevel) |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Lifecycle callback that will be inovked after the |
||
| 794 | * import process has been finished. |
||
| 795 | * |
||
| 796 | * @return void |
||
| 797 | * @throws \Exception Is thrown, if the |
||
| 798 | */ |
||
| 799 | protected function tearDown() |
||
| 802 | |||
| 803 | /** |
||
| 804 | * This method finishes the import process and cleans the registry. |
||
| 805 | * |
||
| 806 | * @return void |
||
| 807 | */ |
||
| 808 | protected function finish() |
||
| 812 | } |
||
| 813 |