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 PID filename to use. |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | const PID_FILENAME = 'importer.pid'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The TechDivision company name as ANSI art. |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $ansiArt = ' _______ _ _____ _ _ _ |
||
| 74 | |__ __| | | | __ \(_) (_) (_) |
||
| 75 | | | ___ ___| |__ | | | |___ ___ ___ _ ___ _ __ |
||
| 76 | | |/ _ \/ __| \'_ \| | | | \ \ / / / __| |/ _ \| \'_ \ |
||
| 77 | | | __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | | |
||
| 78 | |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_| |
||
| 79 | '; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The log level => console style mapping. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $logLevelStyleMapping = array( |
||
| 87 | LogLevel::INFO => 'info', |
||
| 88 | LogLevel::DEBUG => 'comment', |
||
| 89 | LogLevel::ERROR => 'error', |
||
| 90 | LogLevel::ALERT => 'error', |
||
| 91 | LogLevel::CRITICAL => 'error', |
||
| 92 | LogLevel::EMERGENCY => 'error', |
||
| 93 | LogLevel::WARNING => 'error', |
||
| 94 | LogLevel::NOTICE => 'info' |
||
| 95 | ); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The actions unique serial. |
||
| 99 | * |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $serial; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The system logger implementation. |
||
| 106 | * |
||
| 107 | * @var \Psr\Log\LoggerInterface |
||
| 108 | */ |
||
| 109 | protected $systemLogger; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * The RegistryProcessor instance to handle running threads. |
||
| 113 | * |
||
| 114 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 115 | */ |
||
| 116 | protected $registryProcessor; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The processor to read/write the necessary import data. |
||
| 120 | * |
||
| 121 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
| 122 | */ |
||
| 123 | protected $importProcessor; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * The system configuration. |
||
| 127 | * |
||
| 128 | * @var \TechDivision\Import\ConfigurationInterface |
||
| 129 | */ |
||
| 130 | protected $configuration; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * The input stream to read console information from. |
||
| 134 | * |
||
| 135 | * @var \Symfony\Component\Console\Input\InputInterface |
||
| 136 | */ |
||
| 137 | protected $input; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * The output stream to write console information to. |
||
| 141 | * |
||
| 142 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 143 | */ |
||
| 144 | protected $output; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * The matches for the last processed CSV filename. |
||
| 148 | * |
||
| 149 | * @var array |
||
| 150 | */ |
||
| 151 | protected $matches = array(); |
||
| 152 | |||
| 153 | /** |
||
| 154 | * The number of imported bunches. |
||
| 155 | * |
||
| 156 | * @var integer |
||
| 157 | */ |
||
| 158 | protected $bunches = 0; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * The PID for the running processes. |
||
| 162 | * |
||
| 163 | * @var array |
||
| 164 | */ |
||
| 165 | protected $pid = null; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Set's the unique serial for this import process. |
||
| 169 | * |
||
| 170 | * @param string $serial The unique serial |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | public function setSerial($serial) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Return's the unique serial for this import process. |
||
| 181 | * |
||
| 182 | * @return string The unique serial |
||
| 183 | */ |
||
| 184 | public function getSerial() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set's the system logger. |
||
| 191 | * |
||
| 192 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
| 193 | * |
||
| 194 | * @return void |
||
| 195 | */ |
||
| 196 | public function setSystemLogger(LoggerInterface $systemLogger) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Return's the system logger. |
||
| 203 | * |
||
| 204 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
| 205 | */ |
||
| 206 | public function getSystemLogger() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Sets's the RegistryProcessor instance to handle the running threads. |
||
| 213 | * |
||
| 214 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 215 | * |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 225 | * |
||
| 226 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 227 | */ |
||
| 228 | public function getRegistryProcessor() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Set's the import processor instance. |
||
| 235 | * |
||
| 236 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | 1 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Return's the import processor instance. |
||
| 247 | * |
||
| 248 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 249 | */ |
||
| 250 | 1 | public function getImportProcessor() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Set's the system configuration. |
||
| 257 | * |
||
| 258 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | public function setConfiguration(ConfigurationInterface $configuration) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Return's the system configuration. |
||
| 269 | * |
||
| 270 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
| 271 | */ |
||
| 272 | public function getConfiguration() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Set's the input stream to read console information from. |
||
| 279 | * |
||
| 280 | * @param \Symfony\Component\Console\Input\InputInterface $input An IutputInterface instance |
||
| 281 | * |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | public function setInput(InputInterface $input) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Return's the input stream to read console information from. |
||
| 291 | * |
||
| 292 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
| 293 | */ |
||
| 294 | protected function getInput() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set's the output stream to write console information to. |
||
| 301 | * |
||
| 302 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
| 303 | * |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | public function setOutput(OutputInterface $output) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Return's the output stream to write console information to. |
||
| 313 | * |
||
| 314 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
| 315 | */ |
||
| 316 | protected function getOutput() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Return's the source directory that has to be watched for new files. |
||
| 323 | * |
||
| 324 | * @return string The source directory |
||
| 325 | */ |
||
| 326 | protected function getSourceDir() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Parse the temporary upload directory for new files to be imported. |
||
| 333 | * |
||
| 334 | * @return void |
||
| 335 | * @throws \Exception Is thrown if the import can't be finished successfully |
||
| 336 | */ |
||
| 337 | public function import() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * This method start's the import process by initializing |
||
| 382 | * the status and appends it to the registry. |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | * @throws \Exception Is thrown, an import process is already running |
||
| 386 | */ |
||
| 387 | protected function start() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Prepares the global data for the import process. |
||
| 438 | * |
||
| 439 | * @return void |
||
| 440 | */ |
||
| 441 | protected function setUp() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Process all the subjects defined in the system configuration. |
||
| 510 | * |
||
| 511 | * @return void |
||
| 512 | * @throws \Exception Is thrown, if one of the subjects can't be processed |
||
| 513 | */ |
||
| 514 | protected function processSubjects() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Process the subject with the passed name/identifier. |
||
| 546 | * |
||
| 547 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
| 548 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
| 549 | * environment. |
||
| 550 | * |
||
| 551 | * @param \TechDivision\Import\Configuration\SubjectInterface $subject The subject configuration |
||
| 552 | * |
||
| 553 | * @return void |
||
| 554 | * @throws \Exception Is thrown, if the subject can't be processed |
||
| 555 | */ |
||
| 556 | protected function processSubject(\TechDivision\Import\Configuration\SubjectInterface $subject) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Queries whether or not, the passed filename is part of a bunch or not. |
||
| 609 | * |
||
| 610 | * @param string $prefix The prefix to query for |
||
| 611 | * @param string $filename The filename to query for |
||
| 612 | * |
||
| 613 | * @return boolean TRUE if the filename is part, else FALSE |
||
| 614 | */ |
||
| 615 | 2 | public function isPartOfBunch($prefix, $filename) |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Factory method to create new handler instances. |
||
| 658 | * |
||
| 659 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
| 660 | * |
||
| 661 | * @return object The handler instance |
||
| 662 | */ |
||
| 663 | public function subjectFactory($subject) |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Lifecycle callback that will be inovked after the |
||
| 692 | * import process has been finished. |
||
| 693 | * |
||
| 694 | * @return void |
||
| 695 | * @throws \Exception Is thrown, if the |
||
| 696 | */ |
||
| 697 | protected function archive() |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Removes the passed directory recursively. |
||
| 758 | * |
||
| 759 | * @param string $src Name of the directory to remove |
||
| 760 | * |
||
| 761 | * @return void |
||
| 762 | * @throws \Exception Is thrown, if the directory can not be removed |
||
| 763 | */ |
||
| 764 | protected function removeDir($src) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Simple method that writes the passed method the the console and the |
||
| 793 | * system logger, if configured and a log level has been passed. |
||
| 794 | * |
||
| 795 | * @param string $msg The message to log |
||
| 796 | * @param string $logLevel The log level to use |
||
| 797 | * |
||
| 798 | * @return void |
||
| 799 | */ |
||
| 800 | protected function log($msg, $logLevel = null) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Map's the passed log level to a valid symfony console style. |
||
| 820 | * |
||
| 821 | * @param string $logLevel The log level to map |
||
| 822 | * |
||
| 823 | * @return string The apropriate symfony console style |
||
| 824 | */ |
||
| 825 | protected function mapLogLevelToStyle($logLevel) |
||
| 836 | |||
| 837 | /** |
||
| 838 | * Return's the PID filename to use. |
||
| 839 | * |
||
| 840 | * @return string The PID filename |
||
| 841 | */ |
||
| 842 | protected function getPidFilename() |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Persist the passed PID to PID filename. |
||
| 849 | * |
||
| 850 | * @param string $pid The PID of the actual import process to added |
||
| 851 | * |
||
| 852 | * @return void |
||
| 853 | * @throws \Exception Is thrown, if the PID can not be added |
||
| 854 | */ |
||
| 855 | protected function addPid($pid) |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Remove's the actual PID from the PID file. |
||
| 872 | * |
||
| 873 | * @param string $pid The PID of the actual import process to be removed |
||
| 874 | * |
||
| 875 | * @return void |
||
| 876 | * @throws \Exception Is thrown, if the PID can not be removed |
||
| 877 | */ |
||
| 878 | protected function removePid($pid) |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Lifecycle callback that will be inovked after the |
||
| 925 | * import process has been finished. |
||
| 926 | * |
||
| 927 | * @return void |
||
| 928 | * @throws \Exception Is thrown, if the |
||
| 929 | */ |
||
| 930 | protected function tearDown() |
||
| 935 | |||
| 936 | /** |
||
| 937 | * This method finishes the import process and cleans the registry. |
||
| 938 | * |
||
| 939 | * @return void |
||
| 940 | */ |
||
| 941 | protected function finish() |
||
| 947 | } |
||
| 948 |