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 PID filename to use. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | const PID_FILENAME = 'importer.pid'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The TechDivision company name as ANSI art. |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $ansiArt = ' _______ _ _____ _ _ _ |
||
| 75 | |__ __| | | | __ \(_) (_) (_) |
||
| 76 | | | ___ ___| |__ | | | |___ ___ ___ _ ___ _ __ |
||
| 77 | | |/ _ \/ __| \'_ \| | | | \ \ / / / __| |/ _ \| \'_ \ |
||
| 78 | | | __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | | |
||
| 79 | |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_| |
||
| 80 | '; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The log level => console style mapping. |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected $logLevelStyleMapping = array( |
||
| 88 | LogLevel::INFO => 'info', |
||
| 89 | LogLevel::DEBUG => 'comment', |
||
| 90 | LogLevel::ERROR => 'error', |
||
| 91 | LogLevel::ALERT => 'error', |
||
| 92 | LogLevel::CRITICAL => 'error', |
||
| 93 | LogLevel::EMERGENCY => 'error', |
||
| 94 | LogLevel::WARNING => 'error', |
||
| 95 | LogLevel::NOTICE => 'info' |
||
| 96 | ); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The actions unique serial. |
||
| 100 | * |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | protected $serial; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The system logger implementation. |
||
| 107 | * |
||
| 108 | * @var \Psr\Log\LoggerInterface |
||
| 109 | */ |
||
| 110 | protected $systemLogger; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The RegistryProcessor instance to handle running threads. |
||
| 114 | * |
||
| 115 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 116 | */ |
||
| 117 | protected $registryProcessor; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * The processor to read/write the necessary import data. |
||
| 121 | * |
||
| 122 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
| 123 | */ |
||
| 124 | protected $importProcessor; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The system configuration. |
||
| 128 | * |
||
| 129 | * @var \TechDivision\Import\ConfigurationInterface |
||
| 130 | */ |
||
| 131 | protected $configuration; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * The input stream to read console information from. |
||
| 135 | * |
||
| 136 | * @var \Symfony\Component\Console\Input\InputInterface |
||
| 137 | */ |
||
| 138 | protected $input; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * The output stream to write console information to. |
||
| 142 | * |
||
| 143 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 144 | */ |
||
| 145 | protected $output; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * The matches for the last processed CSV filename. |
||
| 149 | * |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | protected $matches = array(); |
||
| 153 | |||
| 154 | /** |
||
| 155 | * The number of imported bunches. |
||
| 156 | * |
||
| 157 | * @var integer |
||
| 158 | */ |
||
| 159 | protected $bunches = 0; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * The PID for the running processes. |
||
| 163 | * |
||
| 164 | * @var array |
||
| 165 | */ |
||
| 166 | protected $pid = null; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Set's the unique serial for this import process. |
||
| 170 | * |
||
| 171 | * @param string $serial The unique serial |
||
| 172 | * |
||
| 173 | * @return void |
||
| 174 | */ |
||
| 175 | public function setSerial($serial) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Return's the unique serial for this import process. |
||
| 182 | * |
||
| 183 | * @return string The unique serial |
||
| 184 | */ |
||
| 185 | public function getSerial() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Set's the system logger. |
||
| 192 | * |
||
| 193 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
| 194 | * |
||
| 195 | * @return void |
||
| 196 | */ |
||
| 197 | public function setSystemLogger(LoggerInterface $systemLogger) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Return's the system logger. |
||
| 204 | * |
||
| 205 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
| 206 | */ |
||
| 207 | public function getSystemLogger() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Sets's the RegistryProcessor instance to handle the running threads. |
||
| 214 | * |
||
| 215 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 216 | * |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 226 | * |
||
| 227 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 228 | */ |
||
| 229 | public function getRegistryProcessor() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Set's the import processor instance. |
||
| 236 | * |
||
| 237 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 238 | * |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | 1 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Return's the import processor instance. |
||
| 248 | * |
||
| 249 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 250 | */ |
||
| 251 | 1 | public function getImportProcessor() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Set's the system configuration. |
||
| 258 | * |
||
| 259 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 260 | * |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | public function setConfiguration(ConfigurationInterface $configuration) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Return's the system configuration. |
||
| 270 | * |
||
| 271 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
| 272 | */ |
||
| 273 | public function getConfiguration() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Set's the input stream to read console information from. |
||
| 280 | * |
||
| 281 | * @param \Symfony\Component\Console\Input\InputInterface $input An IutputInterface instance |
||
| 282 | * |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | public function setInput(InputInterface $input) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Return's the input stream to read console information from. |
||
| 292 | * |
||
| 293 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
| 294 | */ |
||
| 295 | protected function getInput() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Set's the output stream to write console information to. |
||
| 302 | * |
||
| 303 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
| 304 | * |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | public function setOutput(OutputInterface $output) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Return's the output stream to write console information to. |
||
| 314 | * |
||
| 315 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
| 316 | */ |
||
| 317 | protected function getOutput() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Return's the source directory that has to be watched for new files. |
||
| 324 | * |
||
| 325 | * @return string The source directory |
||
| 326 | */ |
||
| 327 | protected function getSourceDir() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Parse the temporary upload directory for new files to be imported. |
||
| 334 | * |
||
| 335 | * @return void |
||
| 336 | * @throws \Exception Is thrown if the import can't be finished successfully |
||
| 337 | */ |
||
| 338 | public function import() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * This method start's the import process by initializing |
||
| 383 | * the status and appends it to the registry. |
||
| 384 | * |
||
| 385 | * @return void |
||
| 386 | * @throws \Exception Is thrown, an import process is already running |
||
| 387 | */ |
||
| 388 | protected function start() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Prepares the global data for the import process. |
||
| 439 | * |
||
| 440 | * @return void |
||
| 441 | */ |
||
| 442 | protected function setUp() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Process all the subjects defined in the system configuration. |
||
| 511 | * |
||
| 512 | * @return void |
||
| 513 | * @throws \Exception Is thrown, if one of the subjects can't be processed |
||
| 514 | */ |
||
| 515 | protected function processSubjects() |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Process the subject with the passed name/identifier. |
||
| 547 | * |
||
| 548 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
| 549 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
| 550 | * environment. |
||
| 551 | * |
||
| 552 | * @param \TechDivision\Import\Configuration\SubjectInterface $subject The subject configuration |
||
| 553 | * |
||
| 554 | * @return void |
||
| 555 | * @throws \Exception Is thrown, if the subject can't be processed |
||
| 556 | */ |
||
| 557 | protected function processSubject(\TechDivision\Import\Configuration\SubjectInterface $subject) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Queries whether or not, the passed filename is part of a bunch or not. |
||
| 619 | * |
||
| 620 | * @param string $prefix The prefix to query for |
||
| 621 | * @param string $filename The filename to query for |
||
| 622 | * |
||
| 623 | * @return boolean TRUE if the filename is part, else FALSE |
||
| 624 | */ |
||
| 625 | 2 | public function isPartOfBunch($prefix, $filename) |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Return's an array with the names of the expected OK files for the actual subject. |
||
| 668 | * |
||
| 669 | * @return array The array with the expected OK filenames |
||
| 670 | */ |
||
| 671 | protected function getOkFilenames() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Query whether or not, the passed CSV filename is in the OK file. If the filename was found, |
||
| 701 | * it'll be returned and the method return TRUE. |
||
| 702 | * |
||
| 703 | * If the filename is NOT in the OK file, the method return's FALSE and the CSV should NOT be |
||
| 704 | * imported/moved. |
||
| 705 | * |
||
| 706 | * @param string $filename The CSV filename to query for |
||
| 707 | * |
||
| 708 | * @return void |
||
| 709 | * @throws \Exception Is thrown, if the passed filename is NOT in the OK file or it can NOT be removed from it |
||
| 710 | */ |
||
| 711 | protected function removeFromOkFile($filename) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Factory method to create new handler instances. |
||
| 733 | * |
||
| 734 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
| 735 | * |
||
| 736 | * @return object The handler instance |
||
| 737 | */ |
||
| 738 | public function subjectFactory($subject) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Lifecycle callback that will be inovked after the |
||
| 767 | * import process has been finished. |
||
| 768 | * |
||
| 769 | * @return void |
||
| 770 | * @throws \Exception Is thrown, if the |
||
| 771 | */ |
||
| 772 | protected function archive() |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Removes the passed directory recursively. |
||
| 833 | * |
||
| 834 | * @param string $src Name of the directory to remove |
||
| 835 | * |
||
| 836 | * @return void |
||
| 837 | * @throws \Exception Is thrown, if the directory can not be removed |
||
| 838 | */ |
||
| 839 | protected function removeDir($src) |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Simple method that writes the passed method the the console and the |
||
| 868 | * system logger, if configured and a log level has been passed. |
||
| 869 | * |
||
| 870 | * @param string $msg The message to log |
||
| 871 | * @param string $logLevel The log level to use |
||
| 872 | * |
||
| 873 | * @return void |
||
| 874 | */ |
||
| 875 | protected function log($msg, $logLevel = null) |
||
| 892 | |||
| 893 | /** |
||
| 894 | * Map's the passed log level to a valid symfony console style. |
||
| 895 | * |
||
| 896 | * @param string $logLevel The log level to map |
||
| 897 | * |
||
| 898 | * @return string The apropriate symfony console style |
||
| 899 | */ |
||
| 900 | protected function mapLogLevelToStyle($logLevel) |
||
| 911 | |||
| 912 | /** |
||
| 913 | * Return's the PID filename to use. |
||
| 914 | * |
||
| 915 | * @return string The PID filename |
||
| 916 | */ |
||
| 917 | protected function getPidFilename() |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Persist the passed PID to PID filename. |
||
| 924 | * |
||
| 925 | * @param string $pid The PID of the actual import process to added |
||
| 926 | * |
||
| 927 | * @return void |
||
| 928 | * @throws \Exception Is thrown, if the PID can not be added |
||
| 929 | */ |
||
| 930 | protected function addPid($pid) |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Remove's the actual PID from the PID file. |
||
| 947 | * |
||
| 948 | * @param string $pid The PID of the actual import process to be removed |
||
| 949 | * |
||
| 950 | * @return void |
||
| 951 | * @throws \Exception Is thrown, if the PID can not be removed |
||
| 952 | */ |
||
| 953 | protected function removePid($pid) |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Lifecycle callback that will be inovked after the |
||
| 967 | * import process has been finished. |
||
| 968 | * |
||
| 969 | * @return void |
||
| 970 | */ |
||
| 971 | protected function tearDown() |
||
| 976 | |||
| 977 | /** |
||
| 978 | * This method finishes the import process and cleans the registry. |
||
| 979 | * |
||
| 980 | * @return void |
||
| 981 | */ |
||
| 982 | protected function finish() |
||
| 987 | |||
| 988 | |||
| 989 | /** |
||
| 990 | * Remove's the passed line from the file with the passed name. |
||
| 991 | * |
||
| 992 | * @param string $line The line to be removed |
||
| 993 | * @param string $filename The name of the file the line has to be removed |
||
| 994 | * |
||
| 995 | * @return void |
||
| 996 | * @throws \Exception Is thrown, if the line is not found or can not be removed |
||
| 997 | */ |
||
| 998 | protected function removeLineFromFile($line, $filename) |
||
| 1050 | } |
||
| 1051 |