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 | * The number of imported bunches. |
||
| 149 | * |
||
| 150 | * @var integer |
||
| 151 | */ |
||
| 152 | protected $bunches = 0; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set's the unique serial for this import process. |
||
| 156 | * |
||
| 157 | * @param string $serial The unique serial |
||
| 158 | * |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | public function setSerial($serial) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Return's the unique serial for this import process. |
||
| 168 | * |
||
| 169 | * @return string The unique serial |
||
| 170 | */ |
||
| 171 | public function getSerial() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Set's the system logger. |
||
| 178 | * |
||
| 179 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
| 180 | * |
||
| 181 | * @return void |
||
| 182 | */ |
||
| 183 | public function setSystemLogger(LoggerInterface $systemLogger) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Return's the system logger. |
||
| 190 | * |
||
| 191 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
| 192 | */ |
||
| 193 | public function getSystemLogger() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Sets's the RegistryProcessor instance to handle the running threads. |
||
| 200 | * |
||
| 201 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 202 | * |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 212 | * |
||
| 213 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 214 | */ |
||
| 215 | public function getRegistryProcessor() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set's the import processor instance. |
||
| 222 | * |
||
| 223 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 224 | * |
||
| 225 | * @return void |
||
| 226 | */ |
||
| 227 | 1 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Return's the import processor instance. |
||
| 234 | * |
||
| 235 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 236 | */ |
||
| 237 | 1 | public function getImportProcessor() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Set's the system configuration. |
||
| 244 | * |
||
| 245 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | public function setConfiguration(ConfigurationInterface $configuration) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Return's the system configuration. |
||
| 256 | * |
||
| 257 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
| 258 | */ |
||
| 259 | public function getConfiguration() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Set's the input stream to read console information from. |
||
| 266 | * |
||
| 267 | * @param \Symfony\Component\Console\Input\InputInterface $input An IutputInterface instance |
||
| 268 | * |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | public function setInput(InputInterface $input) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Return's the input stream to read console information from. |
||
| 278 | * |
||
| 279 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
| 280 | */ |
||
| 281 | protected function getInput() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Set's the output stream to write console information to. |
||
| 288 | * |
||
| 289 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
| 290 | * |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | public function setOutput(OutputInterface $output) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Return's the output stream to write console information to. |
||
| 300 | * |
||
| 301 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
| 302 | */ |
||
| 303 | protected function getOutput() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Return's the source directory that has to be watched for new files. |
||
| 310 | * |
||
| 311 | * @return string The source directory |
||
| 312 | */ |
||
| 313 | protected function getSourceDir() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Parse the temporary upload directory for new files to be imported. |
||
| 320 | * |
||
| 321 | * @return void |
||
| 322 | * @throws \Exception Is thrown if the import can't be finished successfully |
||
| 323 | */ |
||
| 324 | public function import() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * This method start's the import process by initializing |
||
| 369 | * the status and appends it to the registry. |
||
| 370 | * |
||
| 371 | * @return void |
||
| 372 | * @throws \Exception Is thrown, an import process is already running |
||
| 373 | */ |
||
| 374 | protected function start() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Prepares the global data for the import process. |
||
| 423 | * |
||
| 424 | * @return void |
||
| 425 | */ |
||
| 426 | protected function setUp() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Process all the subjects defined in the system configuration. |
||
| 495 | * |
||
| 496 | * @return void |
||
| 497 | * @throws \Exception Is thrown, if one of the subjects can't be processed |
||
| 498 | */ |
||
| 499 | protected function processSubjects() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Process the subject with the passed name/identifier. |
||
| 531 | * |
||
| 532 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
| 533 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
| 534 | * environment. |
||
| 535 | * |
||
| 536 | * @param \TechDivision\Import\Configuration\SubjectInterface $subject The subject configuration |
||
| 537 | * |
||
| 538 | * @return void |
||
| 539 | * @throws \Exception Is thrown, if the subject can't be processed |
||
| 540 | */ |
||
| 541 | protected function processSubject(\TechDivision\Import\Configuration\SubjectInterface $subject) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Queries whether or not, the passed filename is part of a bunch or not. |
||
| 592 | * |
||
| 593 | * @param string $prefix The prefix to query for |
||
| 594 | * @param string $filename The filename to query for |
||
| 595 | * |
||
| 596 | * @return boolean TRUE if the filename is part, else FALSE |
||
| 597 | */ |
||
| 598 | 2 | public function isPartOfBunch($prefix, $filename) |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Factory method to create new handler instances. |
||
| 641 | * |
||
| 642 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
| 643 | * |
||
| 644 | * @return object The handler instance |
||
| 645 | */ |
||
| 646 | public function subjectFactory($subject) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Lifecycle callback that will be inovked after the |
||
| 679 | * import process has been finished. |
||
| 680 | * |
||
| 681 | * @return void |
||
| 682 | * @throws \Exception Is thrown, if the |
||
| 683 | */ |
||
| 684 | protected function archive() |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Removes the passed directory recursively. |
||
| 745 | * |
||
| 746 | * @param string $src Name of the directory to remove |
||
| 747 | * |
||
| 748 | * @return void |
||
| 749 | * @throws \Exception Is thrown, if the directory can not be removed |
||
| 750 | */ |
||
| 751 | protected function removeDir($src) |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Simple method that writes the passed method the the console and the |
||
| 780 | * system logger, if configured and a log level has been passed. |
||
| 781 | * |
||
| 782 | * @param string $msg The message to log |
||
| 783 | * @param string $logLevel The log level to use |
||
| 784 | * |
||
| 785 | * @return void |
||
| 786 | */ |
||
| 787 | protected function log($msg, $logLevel = null) |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Map's the passed log level to a valid symfony console style. |
||
| 807 | * |
||
| 808 | * @param string $logLevel The log level to map |
||
| 809 | * |
||
| 810 | * @return string The apropriate symfony console style |
||
| 811 | */ |
||
| 812 | protected function mapLogLevelToStyle($logLevel) |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Lifecycle callback that will be inovked after the |
||
| 826 | * import process has been finished. |
||
| 827 | * |
||
| 828 | * @return void |
||
| 829 | * @throws \Exception Is thrown, if the |
||
| 830 | */ |
||
| 831 | protected function tearDown() |
||
| 834 | |||
| 835 | /** |
||
| 836 | * This method finishes the import process and cleans the registry. |
||
| 837 | * |
||
| 838 | * @return void |
||
| 839 | */ |
||
| 840 | protected function finish() |
||
| 849 | } |
||
| 850 |