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 |
||
| 52 | class Simple |
||
| 53 | { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The default style to write messages to the symfony console. |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | const DEFAULT_STYLE = 'info'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The TechDivision company name as ANSI art. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $ansiArt = ' _______ _ _____ _ _ _ |
||
| 68 | |__ __| | | | __ \(_) (_) (_) |
||
| 69 | | | ___ ___| |__ | | | |___ ___ ___ _ ___ _ __ |
||
| 70 | | |/ _ \/ __| \'_ \| | | | \ \ / / / __| |/ _ \| \'_ \ |
||
| 71 | | | __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | | |
||
| 72 | |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_| |
||
| 73 | '; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The log level => console style mapping. |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $logLevelStyleMapping = array( |
||
| 81 | LogLevel::INFO => 'info', |
||
| 82 | LogLevel::DEBUG => 'comment', |
||
| 83 | LogLevel::ERROR => 'error', |
||
| 84 | LogLevel::ALERT => 'error', |
||
| 85 | LogLevel::CRITICAL => 'error', |
||
| 86 | LogLevel::EMERGENCY => 'error', |
||
| 87 | LogLevel::WARNING => 'error', |
||
| 88 | LogLevel::NOTICE => 'info' |
||
| 89 | ); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The actions unique serial. |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | protected $serial; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The system logger implementation. |
||
| 100 | * |
||
| 101 | * @var \Psr\Log\LoggerInterface |
||
| 102 | */ |
||
| 103 | protected $systemLogger; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The RegistryProcessor instance to handle running threads. |
||
| 107 | * |
||
| 108 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 109 | */ |
||
| 110 | protected $registryProcessor; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The processor to read/write the necessary import data. |
||
| 114 | * |
||
| 115 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
| 116 | */ |
||
| 117 | protected $importProcessor; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * The system configuration. |
||
| 121 | * |
||
| 122 | * @var \TechDivision\Import\ConfigurationInterface |
||
| 123 | */ |
||
| 124 | protected $configuration; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The input stream to read console information from. |
||
| 128 | * |
||
| 129 | * @var \Symfony\Component\Console\Input\InputInterface |
||
| 130 | */ |
||
| 131 | protected $input; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * The output stream to write console information to. |
||
| 135 | * |
||
| 136 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 137 | */ |
||
| 138 | protected $output; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * The matches for the last processed CSV filename. |
||
| 142 | * |
||
| 143 | * @var array |
||
| 144 | */ |
||
| 145 | protected $matches = array(); |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Set's the unique serial for this import process. |
||
| 149 | * |
||
| 150 | * @param string $serial The unique serial |
||
| 151 | * |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | public function setSerial($serial) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Return's the unique serial for this import process. |
||
| 161 | * |
||
| 162 | * @return string The unique serial |
||
| 163 | */ |
||
| 164 | public function getSerial() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Set's the system logger. |
||
| 171 | * |
||
| 172 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
| 173 | * |
||
| 174 | * @return void |
||
| 175 | */ |
||
| 176 | public function setSystemLogger(LoggerInterface $systemLogger) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Return's the system logger. |
||
| 183 | * |
||
| 184 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
| 185 | */ |
||
| 186 | public function getSystemLogger() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Sets's the RegistryProcessor instance to handle the running threads. |
||
| 193 | * |
||
| 194 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 195 | * |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 205 | * |
||
| 206 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 207 | */ |
||
| 208 | public function getRegistryProcessor() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Set's the import processor instance. |
||
| 215 | * |
||
| 216 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 217 | * |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | 1 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Return's the import processor instance. |
||
| 227 | * |
||
| 228 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 229 | */ |
||
| 230 | 1 | public function getImportProcessor() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Set's the system configuration. |
||
| 237 | * |
||
| 238 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 239 | * |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | public function setConfiguration(ConfigurationInterface $configuration) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Return's the system configuration. |
||
| 249 | * |
||
| 250 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
| 251 | */ |
||
| 252 | public function getConfiguration() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Set's the input stream to read console information from. |
||
| 259 | * |
||
| 260 | * @param \Symfony\Component\Console\Input\InputInterface $input An IutputInterface instance |
||
| 261 | * |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | public function setInput(InputInterface $input) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Return's the input stream to read console information from. |
||
| 271 | * |
||
| 272 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
| 273 | */ |
||
| 274 | protected function getInput() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Set's the output stream to write console information to. |
||
| 281 | * |
||
| 282 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
| 283 | * |
||
| 284 | * @return void |
||
| 285 | */ |
||
| 286 | public function setOutput(OutputInterface $output) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Return's the output stream to write console information to. |
||
| 293 | * |
||
| 294 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
| 295 | */ |
||
| 296 | protected function getOutput() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Return's the source directory that has to be watched for new files. |
||
| 303 | * |
||
| 304 | * @return string The source directory |
||
| 305 | */ |
||
| 306 | protected function getSourceDir() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Parse the temporary upload directory for new files to be imported. |
||
| 313 | * |
||
| 314 | * @return void |
||
| 315 | * @throws \Exception Is thrown if the import can't be finished successfully |
||
| 316 | */ |
||
| 317 | public function import() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * This method start's the import process by initializing |
||
| 362 | * the status and appends it to the registry. |
||
| 363 | * |
||
| 364 | * @return void |
||
| 365 | */ |
||
| 366 | protected function start() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Prepares the global data for the import process. |
||
| 407 | * |
||
| 408 | * @return void |
||
| 409 | */ |
||
| 410 | protected function setUp() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Process all the subjects defined in the system configuration. |
||
| 479 | * |
||
| 480 | * @return void |
||
| 481 | * @throws \Exception Is thrown, if one of the subjects can't be processed |
||
| 482 | */ |
||
| 483 | protected function processSubjects() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Process the subject with the passed name/identifier. |
||
| 515 | * |
||
| 516 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
| 517 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
| 518 | * environment. |
||
| 519 | * |
||
| 520 | * @param \TechDivision\Import\Configuration\SubjectInterface $subject The subject configuration |
||
| 521 | * |
||
| 522 | * @return void |
||
| 523 | * @throws \Exception Is thrown, if the subject can't be processed |
||
| 524 | */ |
||
| 525 | protected function processSubject(\TechDivision\Import\Configuration\SubjectInterface $subject) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Queries whether or not, the passed filename is part of a bunch or not. |
||
| 579 | * |
||
| 580 | * @param string $prefix The prefix to query for |
||
| 581 | * @param string $filename The filename to query for |
||
| 582 | * |
||
| 583 | * @return boolean TRUE if the filename is part, else FALSE |
||
| 584 | */ |
||
| 585 | 2 | public function isPartOfBunch($prefix, $filename) |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Factory method to create new handler instances. |
||
| 628 | * |
||
| 629 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
| 630 | * |
||
| 631 | * @return object The handler instance |
||
| 632 | */ |
||
| 633 | public function subjectFactory($subject) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Lifecycle callback that will be inovked after the |
||
| 666 | * import process has been finished. |
||
| 667 | * |
||
| 668 | * @return void |
||
| 669 | * @throws \Exception Is thrown, if the |
||
| 670 | */ |
||
| 671 | protected function archive() |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Removes the passed directory recursively. |
||
| 735 | * |
||
| 736 | * @param string $src Name of the directory to remove |
||
| 737 | * |
||
| 738 | * @return void |
||
| 739 | * @throws \Exception Is thrown, if the directory can not be removed |
||
| 740 | */ |
||
| 741 | protected function removeDir($src) |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Simple method that writes the passed method the the console and the |
||
| 770 | * system logger, if configured and a log level has been passed. |
||
| 771 | * |
||
| 772 | * @param string $msg The message to log |
||
| 773 | * @param string $logLevel The log level to use |
||
| 774 | * |
||
| 775 | * @return void |
||
| 776 | */ |
||
| 777 | protected function log($msg, $logLevel = null) |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Map's the passed log level to a valid symfony console style. |
||
| 797 | * |
||
| 798 | * @param string $logLevel The log level to map |
||
| 799 | * |
||
| 800 | * @return string The apropriate symfony console style |
||
| 801 | */ |
||
| 802 | protected function mapLogLevelToStyle($logLevel) |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Lifecycle callback that will be inovked after the |
||
| 816 | * import process has been finished. |
||
| 817 | * |
||
| 818 | * @return void |
||
| 819 | * @throws \Exception Is thrown, if the |
||
| 820 | */ |
||
| 821 | protected function tearDown() |
||
| 824 | |||
| 825 | /** |
||
| 826 | * This method finishes the import process and cleans the registry. |
||
| 827 | * |
||
| 828 | * @return void |
||
| 829 | */ |
||
| 830 | protected function finish() |
||
| 834 | } |
||
| 835 |