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 |
||
| 51 | class Simple |
||
| 52 | { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The default style to write messages to the symfony console. |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | const DEFAULT_STYLE = 'info'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The TechDivision company name as ANSI art. |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $ansiArt = ' _______ _ _____ _ _ _ |
||
| 67 | |__ __| | | | __ \(_) (_) (_) |
||
| 68 | | | ___ ___| |__ | | | |___ ___ ___ _ ___ _ __ |
||
| 69 | | |/ _ \/ __| \'_ \| | | | \ \ / / / __| |/ _ \| \'_ \ |
||
| 70 | | | __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | | |
||
| 71 | |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_| |
||
| 72 | '; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The log level => console style mapping. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $logLevelStyleMapping = array( |
||
| 80 | LogLevel::INFO => 'info', |
||
| 81 | LogLevel::DEBUG => 'comment', |
||
| 82 | LogLevel::ERROR => 'error', |
||
| 83 | LogLevel::ALERT => 'error', |
||
| 84 | LogLevel::CRITICAL => 'error', |
||
| 85 | LogLevel::EMERGENCY => 'error', |
||
| 86 | LogLevel::WARNING => 'error', |
||
| 87 | LogLevel::NOTICE => 'info' |
||
| 88 | ); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The actions unique serial. |
||
| 92 | * |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | protected $serial; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The system logger implementation. |
||
| 99 | * |
||
| 100 | * @var \Psr\Log\LoggerInterface |
||
| 101 | */ |
||
| 102 | protected $systemLogger; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The RegistryProcessor instance to handle running threads. |
||
| 106 | * |
||
| 107 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 108 | */ |
||
| 109 | protected $registryProcessor; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * The processor to read/write the necessary import data. |
||
| 113 | * |
||
| 114 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
| 115 | */ |
||
| 116 | protected $importProcessor; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The system configuration. |
||
| 120 | * |
||
| 121 | * @var \TechDivision\Import\ConfigurationInterface |
||
| 122 | */ |
||
| 123 | protected $configuration; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * The input stream to read console information from. |
||
| 127 | * |
||
| 128 | * @var \Symfony\Component\Console\Input\InputInterface |
||
| 129 | */ |
||
| 130 | protected $input; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * The output stream to write console information to. |
||
| 134 | * |
||
| 135 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 136 | */ |
||
| 137 | protected $output; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * The matches for the last processed CSV filename. |
||
| 141 | * |
||
| 142 | * @var array |
||
| 143 | */ |
||
| 144 | protected $matches = array(); |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Set's the unique serial for this import process. |
||
| 148 | * |
||
| 149 | * @param string $serial The unique serial |
||
| 150 | * |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | public function setSerial($serial) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Return's the unique serial for this import process. |
||
| 160 | * |
||
| 161 | * @return string The unique serial |
||
| 162 | */ |
||
| 163 | public function getSerial() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Set's the system logger. |
||
| 170 | * |
||
| 171 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
| 172 | * |
||
| 173 | * @return void |
||
| 174 | */ |
||
| 175 | public function setSystemLogger(LoggerInterface $systemLogger) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Return's the system logger. |
||
| 182 | * |
||
| 183 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
| 184 | */ |
||
| 185 | public function getSystemLogger() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Sets's the RegistryProcessor instance to handle the running threads. |
||
| 192 | * |
||
| 193 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 194 | * |
||
| 195 | * @return void |
||
| 196 | */ |
||
| 197 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 204 | * |
||
| 205 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 206 | */ |
||
| 207 | public function getRegistryProcessor() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Set's the import processor instance. |
||
| 214 | * |
||
| 215 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 216 | * |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | 1 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Return's the import processor instance. |
||
| 226 | * |
||
| 227 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 228 | */ |
||
| 229 | 1 | public function getImportProcessor() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Set's the system configuration. |
||
| 236 | * |
||
| 237 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 238 | * |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | public function setConfiguration(ConfigurationInterface $configuration) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Return's the system configuration. |
||
| 248 | * |
||
| 249 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
| 250 | */ |
||
| 251 | public function getConfiguration() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set's the input stream to read console information from. |
||
| 258 | * |
||
| 259 | * @param \Symfony\Component\Console\Input\InputInterface $input An IutputInterface instance |
||
| 260 | * |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | public function setInput(InputInterface $input) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Return's the input stream to read console information from. |
||
| 270 | * |
||
| 271 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
| 272 | */ |
||
| 273 | protected function getInput() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Set's the output stream to write console information to. |
||
| 280 | * |
||
| 281 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
| 282 | * |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | public function setOutput(OutputInterface $output) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Return's the output stream to write console information to. |
||
| 292 | * |
||
| 293 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
| 294 | */ |
||
| 295 | protected function getOutput() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Return's the source directory that has to be watched for new files. |
||
| 302 | * |
||
| 303 | * @return string The source directory |
||
| 304 | */ |
||
| 305 | protected function getSourceDir() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Parse the temporary upload directory for new files to be imported. |
||
| 312 | * |
||
| 313 | * @return void |
||
| 314 | * @throws \Exception Is thrown if the import can't be finished successfully |
||
| 315 | */ |
||
| 316 | public function import() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * This method start's the import process by initializing |
||
| 361 | * the status and appends it to the registry. |
||
| 362 | * |
||
| 363 | * @return void |
||
| 364 | */ |
||
| 365 | protected function start() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Prepares the global data for the import process. |
||
| 406 | * |
||
| 407 | * @return void |
||
| 408 | */ |
||
| 409 | protected function setUp() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Process all the subjects defined in the system configuration. |
||
| 478 | * |
||
| 479 | * @return void |
||
| 480 | * @throws \Exception Is thrown, if one of the subjects can't be processed |
||
| 481 | */ |
||
| 482 | protected function processSubjects() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Process the subject with the passed name/identifier. |
||
| 514 | * |
||
| 515 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
| 516 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
| 517 | * environment. |
||
| 518 | * |
||
| 519 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
| 520 | * |
||
| 521 | * @return void |
||
| 522 | * @throws \Exception Is thrown, if the subject can't be processed |
||
| 523 | */ |
||
| 524 | protected function processSubject($subject) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Queries whether or not, the passed filename is part of a bunch or not. |
||
| 568 | * |
||
| 569 | * @param string $prefix The prefix to query for |
||
| 570 | * @param string $filename The filename to query for |
||
| 571 | * |
||
| 572 | * @return boolean TRUE if the filename is part, else FALSE |
||
| 573 | */ |
||
| 574 | 2 | public function isPartOfBunch($prefix, $filename) |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Factory method to create new handler instances. |
||
| 617 | * |
||
| 618 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
| 619 | * |
||
| 620 | * @return object The handler instance |
||
| 621 | */ |
||
| 622 | public function subjectFactory($subject) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Lifecycle callback that will be inovked after the |
||
| 655 | * import process has been finished. |
||
| 656 | * |
||
| 657 | * @return void |
||
| 658 | * @throws \Exception Is thrown, if the |
||
| 659 | */ |
||
| 660 | protected function archive() |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Removes the passed directory recursively. |
||
| 724 | * |
||
| 725 | * @param string $src Name of the directory to remove |
||
| 726 | * |
||
| 727 | * @return void |
||
| 728 | * @throws \Exception Is thrown, if the directory can not be removed |
||
| 729 | */ |
||
| 730 | protected function removeDir($src) |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Simple method that writes the passed method the the console and the |
||
| 759 | * system logger, if configured and a log level has been passed. |
||
| 760 | * |
||
| 761 | * @param string $msg The message to log |
||
| 762 | * @param string $logLevel The log level to use |
||
| 763 | * |
||
| 764 | * @return void |
||
| 765 | */ |
||
| 766 | protected function log($msg, $logLevel = null) |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Map's the passed log level to a valid symfony console style. |
||
| 786 | * |
||
| 787 | * @param string $logLevel The log level to map |
||
| 788 | * |
||
| 789 | * @return string The apropriate symfony console style |
||
| 790 | */ |
||
| 791 | protected function mapLogLevelToStyle($logLevel) |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Lifecycle callback that will be inovked after the |
||
| 805 | * import process has been finished. |
||
| 806 | * |
||
| 807 | * @return void |
||
| 808 | * @throws \Exception Is thrown, if the |
||
| 809 | */ |
||
| 810 | protected function tearDown() |
||
| 813 | |||
| 814 | /** |
||
| 815 | * This method finishes the import process and cleans the registry. |
||
| 816 | * |
||
| 817 | * @return void |
||
| 818 | */ |
||
| 819 | protected function finish() |
||
| 823 | } |
||
| 824 |